当前位置:首页 » 编程语言 » java图形界面代码

java图形界面代码

发布时间: 2023-03-05 08:23:26

‘壹’ java图形界面编程

执行java Applt小程序不是这样运行的。你必须建一个html文件,然后在输入
<applet code="ButtonDemo.class"
width=320 height=180>
</applet>
其中code是你编译ButtonDemo类是生成的class文件。然后在cmd中找到html文件所在的路径下输入appletviewer xxx.html
就可以运行了

‘贰’ java图形界面代码

importjava.awt.*;
importjava.awt.event.*;
importjava.io.*;
importjavax.swing.*;

{
JTextAreajta;
JTextFieldjtf;
JButtonjb;

publicReadBook(){
jta=newJTextArea();
jtf=newJTextField(30);
jtf.setText("文件保存路径如c:\ab.txt");
jb=newJButton("保存文字");
JPaneljp=newJPanel();
jp.add(jtf);
jp.add(jb);
add(jta);
add(jp,BorderLayout.SOUTH);
setBounds(500,100,500,380);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);

jb.addActionListener(newActionListener(){

@Override
publicvoidactionPerformed(ActionEvente){
//-------------核心代码---------
Stringpath=jtf.getText();
Filef=newFile(path);
Stringtxt=jta.getText().replaceAll(" "," ");
try{
BufferedWriterbw=newBufferedWriter(newFileWriter(f));
bw.write(txt);//写入文件中
bw.close();

}catch(Exceptione1){
e1.printStackTrace();
}
//-------------核心代码---------
}
});

}

publicstaticvoidmain(String[]args){
newReadBook();
}
}

‘叁’ 求一个简单的java代码:(图形界面)

直接写main函数里了
public static void main(String[] args) {

JFrame ck = new JFrame("title");
JPanel mb = new JPanel();
mb.setLayout(null);
String str = "test"; //自己定义要显示什么
JTextArea ta = new JTextArea(str);
ta.setBounds(0, 0, 100, 30); //自己定义文本区在窗口中的位置和大小
mb.add(ta);
ck.add(mb);
ck.setVisible(true);
ck.setBounds(200, 200, 500, 500); //自己定义窗口位置和大小

}

‘肆’ 怎样用java编写图形界面的Application程序

java编写图形界面需要用到swing等组件,可以在eclipse中安装windowbuilder来开发窗体,自动生成窗体代码,然后自己再根据需要修改,如:

package mainFrame;

import java.awt.EventQueue;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JCheckBox;

import javax.swing.JFrame。

‘伍’ JAVA的图形用户界面代码

package hao;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

public class ChatPanel extends JPanel {

private static final long serialVersionUID = 1L;
JButton send,record,saveRecord,image;
JTextArea inputArea;
JTextPane text;//注意用法****************************************************************************
JComboBox fontName = null, fontSize = null, fontStyle = null, fontColor = null,fontBackColor = null;
public StyledDocument doc = null; JScrollPane scrollPane;JPanel textChat;

JButton music;
public ChatPanel() {
setLayout(new BorderLayout());

text = new JTextPane();
text.setEditable(false);

doc = text.getStyledDocument();//跟踪文本和图片写到该区域的位置*************************************
scrollPane = new JScrollPane(text);
//注意下面对JComboBox的巧用***********************************************************************
String[] str_name = { "宋体", "黑体", "Dialog", "Gulim" };
String[] str_Size = { "12", "14", "18", "22", "30", "40" };
String[] str_Style = { "常规", "斜体", "粗体", "粗斜体" };
String[] str_Color = { "黑色", "红色", "蓝色", "黄色", "绿色" };
String[] str_BackColor = { "无色", "灰色", "淡红", "淡蓝", "淡黄", "淡绿" };
fontName = new JComboBox(str_name);
fontSize = new JComboBox(str_Size);
fontStyle = new JComboBox(str_Style);
fontColor = new JComboBox(str_Color);
fontBackColor = new JComboBox(str_BackColor);

fontName.setBackground(new Color(255,153,255));
fontSize.setBackground(new Color(255,153,255));
fontStyle.setBackground(new Color(255,153,255));
fontColor.setBackground(new Color(255,153,255));
fontBackColor.setBackground(new Color(255,153,255));
Box box = Box.createVerticalBox();//创建一个可以容纳多个Box组件的Box*******************************
Box box_1 = Box.createHorizontalBox();
Box box_2 = Box.createHorizontalBox();
Box box_4 = Box.createHorizontalBox();
box.add(box_1);
box.add(box_2);
box.add(box_4);
JLabel b1= new JLabel("字体~~"), b2 = new JLabel("样式~~"),b3 = new JLabel("字号~~"),b4 = new JLabel("颜色~~"),b5 = new JLabel("背景~~");
b1.setBackground(new Color(255,153,255));
b2.setBackground(new Color(255,153,255));
b3.setBackground(new Color(255,153,255));
b4.setBackground(new Color(255,153,255));
b5.setBackground(new Color(255,153,255));
box_1.add(b1);
box_1.add(fontName);
box_1.add(Box.createHorizontalStrut(8));
box_1.add(b2);
box_1.add(fontStyle);
box_1.add(Box.createHorizontalStrut(8));
box_1.add(b3);
box_1.add(fontSize);
box_2.add(Box.createHorizontalStrut(8));
box_2.add(b4);
box_2.add(fontColor);
box_2.add(Box.createHorizontalStrut(8));
box_4.add(b5);
box_4.add(fontBackColor);

textChat = new JPanel();
textChat.setLayout(new BorderLayout());
textChat.setBackground(new Color(255,153,255));

inputArea = new JTextArea(3, 20);
inputArea.setLineWrap(true); //设置文本区的换行策略。88888*********************************

send = new JButton("发送");
record=new JButton("显示记录");
saveRecord=new JButton("储存记录");
image=new JButton("表情");
send.setBackground(new Color(255,153,255));
record.setBackground(new Color(255,153,255));
saveRecord.setBackground(new Color(255,153,255));
image.setBackground(new Color(255,153,255));
Box box_3 = Box.createHorizontalBox();
box_3.add(send); box_3.add(Box.createHorizontalStrut(8));//设置按钮间距*************************888
box_3.add(record); box_3.add(Box.createHorizontalStrut(8)); //设置按钮间距*************************888
box_3.add(saveRecord); box_3.add(Box.createHorizontalStrut(8));//设置按钮间距*************************888
box_3.add(image);
box.setBorder(BorderFactory.createLineBorder(new Color(102,102,0),5));//设置Box的边框线********************
box_3.setBorder(BorderFactory.createLineBorder(new Color(102,102,0),5));
textChat.add(box,BorderLayout.NORTH);
textChat.add(inputArea,BorderLayout.CENTER);
textChat.add(box_3, BorderLayout.SOUTH);

inputArea.requestFocus(true);
inputArea.setBorder(BorderFactory.createLineBorder(new Color(102,102,0),5));//设置输入窗口边框线*******************
text.setBorder(BorderFactory.createLineBorder(new Color(102,102,0),8));//设置输入窗口边框线*******************

JPanel audioPanel = new JPanel();//最上面的边框************************************************************************
audioPanel.setBackground(new Color(255,153,255));
audioPanel.setLayout(new GridLayout(1,1));

music = new JButton("想听就听");
music.setPreferredSize(new Dimension(320,50));
music.setBorder(BorderFactory.createLineBorder(Color.BLACK,10));//设置输入窗口边框线*******************
audioPanel.add(music);

add(audioPanel, BorderLayout.NORTH);
add(scrollPane,BorderLayout.CENTER);
add(textChat, BorderLayout.SOUTH);
}

void insertIcon(ImageIcon image) {
text.setCaretPosition(doc.getLength());
text.insertIcon(image);
insert(new MessageStyle());//?????????????????????????????????????????????????????????????????????????????/
}

public void insert(MessageStyle attrib) {
try {

doc.insertString(doc.getLength(), attrib.getText() + "\n", attrib.getAttrSet());//写完后接着换行************

} catch (BadLocationException e) {
e.printStackTrace();
}
}

public MessageStyle getMessageStyle(String line) {

MessageStyle att = new MessageStyle();

att.setText(line);
att.setName((String) fontName.getSelectedItem());
att.setSize(Integer.parseInt((String) fontSize.getSelectedItem()));
String temp_style = (String) fontStyle.getSelectedItem();
if (temp_style.equals("常规")) {
att.setStyle(MessageStyle.GENERAL);
}
else if (temp_style.equals("粗体")) {
att.setStyle(MessageStyle.BOLD);
}
else if (temp_style.equals("斜体")) {
att.setStyle(MessageStyle.ITALIC);
}
else if (temp_style.equals("粗斜体")) {
att.setStyle(MessageStyle.BOLD_ITALIC);
}

String temp_color = (String) fontColor.getSelectedItem();
if (temp_color.equals("黑色")) {
att.setColor(new Color(0, 0, 0));
}
else if (temp_color.equals("红色")) {
att.setColor(new Color(255, 0, 0));
}
else if (temp_color.equals("蓝色")) {
att.setColor(new Color(0, 0, 255));
}
else if (temp_color.equals("黄色")) {
att.setColor(new Color(255, 255, 0));
}
else if (temp_color.equals("绿色")) {
att.setColor(new Color(0, 255, 0));
}

String temp_backColor = (String) fontBackColor.getSelectedItem();
if (!temp_backColor.equals("无色")) {
if (temp_backColor.equals("灰色")) {
att.setBackColor(new Color(200, 200, 200));
}
else if (temp_backColor.equals("淡红")) {
att.setBackColor(new Color(255, 200, 200));
}
else if (temp_backColor.equals("淡蓝")) {
att.setBackColor(new Color(200, 200, 255));
}
else if (temp_backColor.equals("淡黄")) {
att.setBackColor(new Color(255, 255, 200));
}
else if (temp_backColor.equals("淡绿")) {
att.setBackColor(new Color(200, 255, 200));
}
}
return att;
}

}

‘陆’ Java编写一个图形界面

rcp(SWT/JFace)开发的要不要,同样是java的

‘柒’ JAVA 一个可以打开图片的图形界面,求代码。

以前写的一个比较粗糙的例子。

importjavax.swing.*;
importjava.awt.event.*;
importjava.awt.*;
importjava.io.*;

{
JLabeljl=newJLabel("图片");
JMenuBarjmb=newJMenuBar();
JMenujm=newJMenu("文件");
JMenuItemjmi=newJMenuItem("选择图片");
JPaneljp=newJPanel(newFlowLayout(FlowLayout.CENTER));
JFileChooserchooser=newJFileChooser();
publicTupian(){
super("浏览图片");
jmb.add(jm);
jm.add(jmi);
jp.add(jl);
jmi.addActionListener(this);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setLayout(newBorderLayout());
this.add(jmb,BorderLayout.NORTH);
this.add(jp,BorderLayout.CENTER);
this.setSize(800,600);
this.setVisible(true);
}
publicvoidactionPerformed(ActionEvente){
inti=chooser.showOpenDialog(this);
if(i==chooser.APPROVE_OPTION){
Imageimage=newImageIcon(chooser.getSelectedFile().getPath()).getImage();
image=image.getScaledInstance(400,400,Image.SCALE_DEFAULT);//调整图像大小400,400
jl.setIcon(newImageIcon(image));
jl.setText("");
}
if(i==chooser.CANCEL_OPTION)return;
}
publicstaticvoidmain(String[]args){
newTupian();
}
}
热点内容
动迁三块砖算法 发布:2024-11-07 05:18:06 浏览:826
视窗压缩 发布:2024-11-07 04:45:06 浏览:887
fc2点此访问 发布:2024-11-07 04:45:04 浏览:760
上传吊牌图 发布:2024-11-07 04:38:48 浏览:919
密码学什么概念 发布:2024-11-07 04:38:48 浏览:848
linuxpdf转word 发布:2024-11-07 04:37:06 浏览:213
安卓手机为什么用ufs 发布:2024-11-07 04:15:09 浏览:559
数据库删除所有表 发布:2024-11-07 04:13:55 浏览:576
新建文件夹2链接 发布:2024-11-07 04:12:17 浏览:156
mapreduce源码分析 发布:2024-11-07 04:10:36 浏览:606