导航:首页 > 编程语言 > javagui图形界面编程实例

javagui图形界面编程实例

发布时间:2021-12-09 07:16:37

『壹』 java GUI编程的一个完整的实例

Java程序

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test35 extends JFrame implements ItemListener{
private static final long serialVersionUID = 1L;
JComboBox cmbGender;
JLabel lblMessage;

public Test35(){
("工业09");
String[] arr = new String[];
cmbGender = new JComboBox(arr);
lblMessage = new JLabel("请选择你的性别");
this.setLayout(new FlowLayout());
this.add(lblMessage);
this.add(cmbGender);
cmbGender.addItemListener(this);
this.setSize(200, 100);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
new Test35();
}

@Override
public void itemStateChanged(ItemEvent e) {
JComboBox cmb = (JComboBox)e.getSource();
if(cmb == cmbGender){
lblMessage.setText(cmb.getSelectedItem().toString());
}
}
}

运行测试:

『贰』 怎样用JAVA的GUI(图形用户界面)来设计一个程序!求程序和详解。

//正好以前注释过一个
importjava.awt.*;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.KeyAdapter;
importjava.awt.event.KeyEvent;
importjava.awt.image.MemoryImageSource;
importjava.util.Random;
importjavax.swing.JDialog;
importjavax.swing.JPanel;
importjavax.swing.Timer;
{
=1L;
privateRandomrandom=newRandom();
privateDimensionscreenSize;
privateJPanelgraphicsPanel;
privatefinalstaticintgap=20;
privateint[]posArr;
privateintlines;
privateintcolumns;
publicstaticvoidmain(String[]args){
newCharacterRain();
}
publicCharacterRain(){
initComponents();
}
privatevoidinitComponents(){
setLayout(newBorderLayout());
graphicsPanel=newGraphicsPanel();
add(graphicsPanel,BorderLayout.CENTER);
ToolkitdefaultToolkit=Toolkit.getDefaultToolkit();
Imageimage=defaultToolkit.createImage(newMemoryImageSource(0,0,
null,0,0));
CursorinvisibleCursor=defaultToolkit.createCustomCursor(image,
newPoint(0,0),"cursor");
setCursor(invisibleCursor);
=newKeyPressListener();
this.addKeyListener(keyPressListener);
this.setAlwaysOnTop(true);
this.setUndecorated(true);
this.getGraphicsConfiguration().getDevice().setFullScreenWindow(this);
this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setVisible(true);
screenSize=Toolkit.getDefaultToolkit().getScreenSize();
lines=screenSize.height/gap;//用gap来分行
columns=screenSize.width/gap;//用gap来分列
posArr=newint[columns+1];
random=newRandom();
for(inti=0;i<posArr.length;i++){
posArr[i]=random.nextInt(lines);//随机行数
}
newTimer(100,this).start();
}
privatechargetChr(){
return(char)(random.nextInt(94)+33);//返回随机字符
}
publicvoidactionPerformed(ActionEvente){
graphicsPanel.repaint();//Timer事件,graphicsPanel重绘
}
@SuppressWarnings("serial")
{
publicvoidpaint(Graphicsg){
Graphics2Dg2d=(Graphics2D)g;
g2d.setFont(getFont().deriveFont(Font.BOLD));
g2d.setColor(Color.BLACK);
g2d.fillRect(0,0,screenSize.width,screenSize.height);//设置背景色为黑色
intcurrentColumn=0;
for(intx=0;x<screenSize.width;x+=gap){
intendPos=posArr[currentColumn];//获得开始行位置
g2d.setColor(Color.GREEN);
g2d.drawString(String.valueOf(getChr()),x,endPos*gap);//画出随机开始行的字符
intcg=0;//初始黑色
intlength=25;//字符列的长度
for(intj=endPos-length;j<endPos;j++){//随机行逐列向上length行,为循环开始行
cg+=255/(length+1);//渐变色递增,0,255,0为green
if(cg>255){
cg=255;
}
g2d.setColor(newColor(0,cg,0));
g2d.drawString(String.valueOf(getChr()),x,j*gap);//画出随机行后的15行字符,颜色从黑色渐变成绿色,逐行增加
}
posArr[currentColumn]+=random.nextInt(5);//下落距离最快为4,会产生加速下落的感觉
if((posArr[currentColumn]-5)*gap>getHeight()){//如果行数位置大于屏幕高度,从新获取合适的行
posArr[currentColumn]=random.nextInt(lines);
}
currentColumn++;//下一列获取随机行
}
}
}
{
publicvoidkeyPressed(KeyEvente){
if(e.getKeyCode()==KeyEvent.VK_ESCAPE){//监听键盘事件,按ESC按键退出
System.exit(0);
}
}
}
}

『叁』 使用Java的GUI图形用户界面编程设计并编写一个计算器程序

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator extends JFrame {
JButton b0 = new JButton("0");
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("4");
JButton b5 = new JButton("5");
JButton b6 = new JButton("6");
JButton b7 = new JButton("7");
JButton b8 = new JButton("8");
JButton b9 = new JButton("9");
JButton jiaButton = new JButton("+");
JButton jianButton = new JButton("-");
JButton chengButton = new JButton("*");
JButton chuButton = new JButton("/");
JButton yuButton = new JButton("%");
JButton jjButton = new JButton("+/-");
JButton sqrtButton = new JButton("sqrt");
JButton dianButton = new JButton(".");
JButton dengButton = new JButton("=");
JButton Button = new JButton("1/x");
JButton backButton = new JButton("Backpace");
JButton cButton = new JButton("C");
public double op1;
public double op2;
public static final int JIA = 0;
public static final int JIAN = 1;
public static final int CHENG = 2;
public static final int CHU = 3;
public static final int JJ = 4;
public static final int DIAN = 5;
public int current0p = 0;
private boolean opEnd = false;

JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JTextField result = new JTextField(20);

public Calculator() {
initPanel2();
initPanel3();
panel2.setLayout(new GridLayout(5, 4));
panel1.setLayout(new BorderLayout());
panel1.add(panel3, BorderLayout.NORTH);// 设置位置
panel1.add(panel2, BorderLayout.CENTER);// 设置位置
getContentPane().add(panel1);
addActionListeners();
setSize(260, 260);
setLocation(500, 300);
setVisible(true);
setDefaultCloseOperation(Calculator.EXIT_ON_CLOSE);
this.setResizable(false);
this.setTitle("计算器");
}
private void initPanel2() {
// 把组件添加相应panel上

panel2.add(b7);
panel2.add(b8);
panel2.add(b9);
panel2.add(chuButton);

panel2.add(b4);
panel2.add(b5);
panel2.add(b6);
panel2.add(chengButton);

panel2.add(b1);
panel2.add(b2);
panel2.add(b3);
panel2.add(jianButton);

panel2.add(b0);
panel2.add(jjButton);
panel2.add(dianButton);
panel2.add(jiaButton);

panel2.add(Button);
panel2.add(yuButton);
panel2.add(sqrtButton);
panel2.add(dengButton);

}

private void addActionListeners() {
ActionHandler c = new ActionHandler();
b0.addActionListener(c);
b1.addActionListener(c);
b2.addActionListener(c);
b3.addActionListener(c);
b4.addActionListener(c);
b5.addActionListener(c);
b6.addActionListener(c);
b7.addActionListener(c);
b8.addActionListener(c);
b9.addActionListener(c);

jiaButton.addActionListener(c);
dengButton.addActionListener(c);
chengButton.addActionListener(c);
chuButton.addActionListener(c);
jianButton.addActionListener(c);
jjButton.addActionListener(c);
dianButton.addActionListener(c);
sqrtButton.addActionListener(c);
yuButton.addActionListener(c);
Button.addActionListener(c);
backButton.addActionListener(c);
cButton.addActionListener(c);
}

class ActionHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {

if (e.getSource() == b0) {
if (opEnd == false) {
result.setText("");

}
result.setText(result.getText() + "0");

}
if (e.getSource() == b1) {
if (opEnd == false) {
result.setText("");

}
result.setText(result.getText() + "1");
opEnd = true;
}

if (e.getSource() == b2) {

if (opEnd == false) {
result.setText("");

}
result.setText(result.getText() + "2");
opEnd = true;
}
if (e.getSource() == b3) {
if (opEnd == false) {
result.setText("");

}
result.setText(result.getText() + "3");
opEnd = true;

}
if (e.getSource() == b4) {
if (opEnd == false) {
result.setText("");

}
result.setText(result.getText() + "4");
opEnd = true;
}
if (e.getSource() == b5) {

if (opEnd == false) {
result.setText("");

}
result.setText(result.getText() + "5");
opEnd = true;
}
if (e.getSource() == b6) {
if (opEnd == false) {
result.setText("");

}
result.setText(result.getText() + "6");
opEnd = true;
}
if (e.getSource() == b7) {
if (opEnd == false) {
result.setText("");

}
result.setText(result.getText() + "7");
opEnd = true;
}
if (e.getSource() == b8) {
if (opEnd == false) {
result.setText("");

}
result.setText(result.getText() + "8");
opEnd = true;
}
if (e.getSource() == b9) {
if (opEnd == false) {
result.setText("");

}
result.setText(result.getText() + "9");
opEnd = true;
}
try {
if (e.getSource() == jiaButton) {
op1 = Double.parseDouble(result.getText());

// 2、说明操作数已经输入完毕
opEnd = false;

current0p = JIA;
}
if (e.getSource() == chengButton) {
op1 = Double.parseDouble(result.getText());

// 2、说明操作数已经输入完毕
opEnd = false;

current0p = CHENG;

}
if (e.getSource() == chuButton) {
op1 = Double.parseDouble(result.getText());

// 2、说明操作数已经输入完毕
opEnd = false;

current0p = CHU;

}
if (e.getSource() == jianButton) {
op1 = Double.parseDouble(result.getText());

// 2、说明操作数已经输入完毕
opEnd = false;

current0p = JIAN;

}

if (e.getSource() == jjButton) {
String tmp = result.getText();
if (tmp.equals("") || tmp.equals("0")) {
return;
}
if (tmp.charAt(0) == '-') {
tmp = tmp.substring(1);

} else {
tmp = '-' + tmp;
}
result.setText(tmp);
}
if (e.getSource() == dianButton) {
String tmp = result.getText();
if (tmp.equals("")) {
return;
}
if (tmp.indexOf(".") != -1) {
return;
}
tmp = tmp + ".";
result.setText(tmp);

}
if (e.getSource() == sqrtButton) {
String tmp = result.getText();
if (tmp.equals(" ")) {
return;
}
double d;
d = Double.parseDouble(tmp);// 先定义double类型d
if (d < 0) {
result.setText("不能对负数求平方根");
return;
}
op2 = Math.sqrt(d);
result.setText(op2 + "");
}

if (e.getSource() == backButton) {
String s = result.getText();
result.setText("");
for (int i = 0; i < s.length() - 1; i++) {
char a = s.charAt(i);
result.setText(result.getText() + a);
}

}
if (e.getSource() == cButton) {
result.setText("0");
opEnd = false;
}
if (e.getSource() == dengButton) {
op2 = Double.parseDouble(result.getText());

switch (current0p) {
case JIA:
result.setText(op1 + op2 + "");
break;
case JIAN:
result.setText(op1 - op2 + "");
break;
case CHENG:
result.setText(op1 * op2 + "");
break;
case CHU:
if (op2 == 0) {
result.setText("被除数不能为零");
break;
}
result.setText(op1 / op2 + "");
break;
}
opEnd = false;
}
} catch (Exception e1) {
result.setText("Wrong");
opEnd = false;
}
}

}

private void initPanel3() {

panel3.setLayout(new GridLayout(2, 1));
panel3.add(result);
panel3.add(panel4);
panel4.setLayout(new GridLayout(1, 2));

panel4.add(backButton);
panel4.add(cButton);
// panel3.setPreferredSize(new Dimension(260,65));
}

public static void main(String[] args) {
Calculator c = new Calculator();// 生成类实例

}

}

『肆』 Java编写一个图形界面

我觉得还是我的好些。。。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.FileOutputStream;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class TestSwing extends JFrame{
private JLabel label1, label2, label3, label4,label5,label6,label7;

private JPanel panel,panel1,panel2;
private JTextField tField1,tField2,tField3,tField4,tField5,tField6,tField7,tField8,tField9;

private JButton button;

private String filepath="c:\\info.dat";
public TestSwing(){
init();
this.setSize(300, 280);
this.setLocation(
(Toolkit.getDefaultToolkit().getScreenSize().width - this
.getSize().width) / 2,
(Toolkit.getDefaultToolkit().getScreenSize().height - this
.getSize().height) / 2);
this.setResizable(false);
this.setVisible(true);
this.setTitle("info");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void init(){
initPanel();
initLabel();
initTextField();
initButton();
initContent();
}

private void initContent(){
panel1.add(label1);
panel1.add(tField1);
panel1.add(label2);
panel1.add(tField2);
panel1.add(label3);
panel1.add(tField3);
panel1.add(label4);
panel1.add(tField4);
panel1.add(label5);
panel1.add(tField5);
panel1.add(label6);
panel1.add(tField6);
panel1.add(label7);
panel1.add(tField7);
panel1.add(tField8);
panel1.add(tField9);

panel2.add(button);

panel.add(panel1,BorderLayout.CENTER);
panel.add(panel2,BorderLayout.SOUTH);

getContentPane().add(panel);
}

private void initPanel(){
panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.setBackground(Color.WHITE);

panel1 = new JPanel();
GridLayout upLayout = new GridLayout(8, 2);
panel1.setLayout(upLayout);

panel2 = new JPanel();
panel2.setLayout(new FlowLayout());
}
private void initLabel(){
label1 = new JLabel("IP地址", SwingConstants.LEFT);
label2 = new JLabel("端口", SwingConstants.LEFT);
label3 = new JLabel("访问地址", SwingConstants.LEFT);
label4 = new JLabel("XXXXX", SwingConstants.LEFT);
label5 = new JLabel("XXXXX", SwingConstants.LEFT);
label6 = new JLabel("XXXXX", SwingConstants.LEFT);
label7 = new JLabel("XXXXX", SwingConstants.LEFT);
}

private void initTextField(){
tField1 = new JTextField(10);
tField2 = new JTextField(10);
tField3 = new JTextField(10);
tField4 = new JTextField(10);
tField5 = new JTextField(10);
tField6 = new JTextField(10);
tField7 = new JTextField(10);
tField8 = new JTextField(10);
tField9 = new JTextField(10);
}
private void initButton(){
button = new JButton("完成");
button.addMouseListener(new MouseAdapter(){
//左键点击
public void mouseClicked(MouseEvent e) {
String ipv = tField1.getText().trim();
String portv = tField2.getText().trim();
String addrv = tField3.getText().trim();
String xx1v = tField4.getText().trim();
String xx2v = tField5.getText().trim();
String xx3v = tField6.getText().trim();
String xx4v = tField7.getText().trim();
String inputx = tField8.getText().trim();
String inputxv = tField9.getText().trim();
createFile(ipv, portv, addrv, xx1v, xx2v, xx3v, xx4v, inputx, inputxv);
JOptionPane.showMessageDialog(null, "保存成功");
tField1.setText("");
tField2.setText("");
tField3.setText("");
tField4.setText("");
tField5.setText("");
tField6.setText("");
tField7.setText("");
tField8.setText("");
tField9.setText("");
}

});
}
public static void main(String[]args){
new TestSwing();
}

private void createFile(String ipv,String portv ,String addrv,String xx1v,String xx2v,String xx3v,String xx4v,String inputx,String inputxv){
StringBuffer context = new StringBuffer();
context.append("ip=").append(ipv).append("\n");
context.append("port=").append(portv).append("\n");
context.append("addr=").append(addrv).append("\n");
context.append("xx1=").append(xx1v).append("\n");
context.append("xx2=").append(xx2v).append("\n");
context.append("xx3=").append(xx3v).append("\n");
context.append("xx4=").append(xx4v).append("\n");
context.append(inputx).append("=").append(inputxv).append("");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(filepath);
fos.write(context.toString().getBytes());
} catch (Exception e) {
} finally {
if (fos != null)
try {
fos.close();
} catch (Exception e) {
}
}
}
}

『伍』 如何进行Java GUI图形用户界面编程

在远标做过直接使用javax.swing和java.awt两个包
一个简单的GUI程序如下:
packagesix;
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
publicclassMain extendsJFrame{ //类Main继承自JFrame
privateJPanel pane = null;
privateJPanel p = null;
privateCardLayout card = null;
privateJButton button_1 = null;
privateJButton button_2 = null;
privateJButton b1 = null,b2 = null,b3 = null;
privateJPanel p1 = null,p2 = null,p3 = null;
publicMain() //
{
super("卡片布局管理器测试");
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch(Exception ex){
ex.printStackTrace();
}
//创建新卡片布局
card = newCardLayout(5,5);
pane = newJPanel(card);
p = newJPanel();
button_1 = newJButton("< 上一步");
button_2 = newJButton("下一步 >");
b1 = newJButton("1");b2 = newJButton("2");b3 = newJButton("3");
b1.setMargin(newInsets(2,2,2,2));
b2.setMargin(newInsets(2,2,2,2));
b3.setMargin(newInsets(2,2,2,2));
p.add(button_1);p.add(b1);p.add(b2);p.add(b3);p.add(button_2);
p1 = newJPanel();
p2 = newJPanel();
p3 = newJPanel();
p1.setBackground(Color.RED);
p2.setBackground(Color.BLUE);
p3.setBackground(Color.GREEN);
p1.add(newJLabel("JPanel_1"));
p2.add(newJLabel("JPanel_2"));
p3.add(newJLabel("JPanel_3"));
pane.add(p1,"p1");pane.add(p2,"p2");pane.add(p3,"p3");
//翻转卡片布局动作
button_1.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvent e){
card.previous(pane);
}
});
button_2.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvent e){
card.next(pane);
}
});
b1.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvent e){
card.show(pane, "p1");
}
});
b2.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvent e){
card.show(pane,"p2");
}
});
b3.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvent e){
card.show(pane, "p3");
}
});
this.getContentPane().add(pane);
this.getContentPane().add(p,BorderLayout.SOUTH);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 200);
this.setVisible(true);
}
publicstaticvoidmain(String[]args)
{
newMain(); //
}
}

『陆』 java编程实现图形界面

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JButton;
public class mi
{
private static String username;
private static String password ;
private static JTextField []t={
new JTextField("账号:",8),new JTextField(10),
new JTextField("密码:",8),new JPasswordField(10)};

public static void main (String args[]){
JFrame app=new JFrame("账号密码演示程序");
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setSize(280,120);
Container c=app.getContentPane();
c.setLayout(new FlowLayout());

t[0].setEditable(false);
t[2].setEditable(false);
for(int i=0;i<4;i++)
c.add(t[i]);t[1].setText("");

JButton[]b={new JButton("确定"),new JButton("重置")};
c.add(b[0]);c.add(b[1]);
app.setLocationRelativeTo(null);
app.setVisible(true);

b[1].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
t[1].setText("");
t[3].setText("");
}
});
// 登录按钮加事件监听器
b[0].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
username = t[1].getText();
password = t[3].getText();
//判断用户名密码是否正确
if (username.equals("数字") && password.equals("123")) {
JOptionPane.showMessageDialog(null, "登陆成功!", "消息",
JOptionPane.INFORMATION_MESSAGE);

} else {
JOptionPane.showMessageDialog(null, "用户名或密码错误!", "错误",
JOptionPane.ERROR_MESSAGE);
}
}
}); }

}

『柒』 Java实现GUI编程基本方法都有那些

其实无论在什么平台下,GUI应用程序的基本开发方法都是相似的。一般都包括下面这样四个步骤:

①创建容器

首先要创建一个GUI应用程序,需要创建一个用于容纳所有其它GUI组件元素的载体,Java中称为容器。典型的包括窗口(Window)、框架(Frame/JFrame)、对话框(Dialog/JDialog)、面板(Panel/JPanel)等。只有先创建了这些容器,其它界面元素如按钮(Button/JButton)、标签(Label/JLabel)、文本框(TextField/JTextField)等才有地方放。

②添加组件

为了实现GUI应用程序的功能,为了与用户交换,需要在容器上添加各种组件/控件。这需要根据具体的功能要求来决定用什么组件。例如,如果需要提示信息,可用标签(Label/JLabel);如果需要输入少量文本,可用文本框(TextField/JTextField);如果需要输入较多文本,可用文本区域(TextArea/JTextArea);如果需要输入密码,可用密码域(JPasswordField)等等。

③安排组件
与传统的Windows环境下的GUI软件开发工具不同,为了更好地实现跨平台,Java程序中各组件的位置、大小一般不是以绝对量来衡量,而是以相对量来衡量。例如有时候,程序的组件的位置是按"东/East"、"西/West"、"南/South"、"北/North"、"中 /Center"这种方位来标识的。因此,在组织界面时,除了要考虑所需的组件种类外,还需要考虑如何安排这些组件的位置与大小。这一般是通过设置布局管理器(Layout Manager)及其相关属性来实现的。事实上上述按方位来安排组件就是采用了Java中多种布局管理器里的BorderLayout布局管理器。

④处理事件

为了完成一个GUI应用程序所应具备的功能,除了适当地安排各种组件产生美观的界面外,还需要处理各种界面元素事件,以便真正实现与用户的交换,完成程序的功能。在Java程序中这一般是通过实现适当的事件监听者接口来完成的。比如如果需要响应按钮事件,就需要实现 ActionListener监听者接口;如果需要响应窗口事件,就需要实现WindowListener监听者接口。

『捌』 用Java设计一个图形界面(GUI)的计算器应用程序,完成简单的算术运算

下午发
五分钟搞定,先给你发个简单的,照例子自己去做你想要的计算器
package aaa;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class YunSuan implements ActionListener,ItemListener
{
public static JFrame jf;
public static Container c;
public static JTextField jtf1,jtf2,jtf3;
public static JButton jb1;
public static Choice c2;

public static String fuhao="";

public YunSuan()
{
jf=new JFrame("加减乘除运算");
c=jf.getContentPane();
c.setLayout(new FlowLayout());
jtf1=new JTextField(10);
jtf2=new JTextField(10);
jtf3=new JTextField(10);
c2=new Choice();
jb1=new JButton("=");

}

public void Jiemian()
{

c.add(jtf1);
c2.addItemListener(this);
c2.add("");
c2.add("+");
c2.add("-");
c2.add("*");
c2.add("/");
c.add(c2);
c.add(jtf2);
jb1.addActionListener(this);
c.add(jb1);
c.add(jtf3);

jf.setLocation(200,100);
jf.setVisible(true);
jf.pack();
}
public static void main(String args[])
{
YunSuan ys=new YunSuan();
ys.Jiemian();
}

public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
double s1=Integer.parseInt(jtf1.getText());
double s2=Integer.parseInt(jtf2.getText());
double result=0;
if(fuhao.equals("+"))
{
result=s1+s2;
String result2=String.valueOf(result);
jtf3.setText(result2);
}
if(fuhao.equals("-"))
{
result=s1-s2;
String result2=String.valueOf(result);
jtf3.setText(result2);
}
if(fuhao.equals("*"))
{
result=s1*s2;
String result2=String.valueOf(result);
jtf3.setText(result2);
}
if(fuhao.equals("/"))
{
result=s1/s2;
String result2=String.valueOf(result);
jtf3.setText(result2);
}
}

public void itemStateChanged(ItemEvent ie)
{
if(ie.getSource()==c2)
{
String str1=c2.getSelectedItem();
fanhui(str1);
}
}
public String fanhui(String str2)
{
return fuhao=str2;
}
}

『玖』 java里面的GUI图形界面编程

如果Panel里的内容是用户程序绘制出来的,那么你的绘制过程需要放在都应界面控件(panel)的paintComponent方法里。

『拾』 java编程中,GUI界面的一般框架有什么希望大神给写个程序实例,程序内容包括布局的应用,多个

java GUI一般,通过AWT,SWING 实现

其余比较流行的是SWT,但是它不是官方库, 并且不能跨平台

//注意:

Swing, 所以很多不涉及界面组件的一些类比如布局 等Swing也可以使用

但是组件,最好不要混用, 容易出现一些组件叠加,等显示错误 比如JButton TextField等混用


//参考代码

下面使用Swing组件来实现

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

//本类实现了ActionListener接口.一个ActionListener可以响应JMenuItem和JButton的动作
//本类实现FocusListener接口,一个FocusListener可以响应JTextField,JButton等
//JButton响应多个事件接口
,FocusListener{
privateJTextFieldjtf1;
privateJTextFieldjtf2;
privateJTextFieldjtf3;
privateJButtonjb1;

publicMyGuiFrame(){
//----------窗口属性的设置----------
setTitle("窗口应用程序");//窗口标题
setSize(380,120);//窗口大小
setLocationRelativeTo(null);//窗口居于屏幕中央
setDefaultCloseOperation(EXIT_ON_CLOSE);//点击关闭窗口后退出jvm虚拟机
getContentPane().setLayout(newBorderLayout(5,3));//边界布局,水平间距5,垂直间距3

//菜单栏组件初始化
initMenu();

//主要面板的初始化
initPanel();
}

privatevoidinitPanel(){
JPaneljp=newJPanel(newFlowLayout(FlowLayout.CENTER));//流式布局
jtf1=newJTextField(8);
jtf1.addFocusListener(this);//添加焦点响应
JLabeljl=newJLabel("+");
jtf2=newJTextField(8);
jtf2.addFocusListener(this);
jb1=newJButton("=");
jb1.addActionListener(this);//添加动作响应
jb1.addFocusListener(this);//添加焦点响应

jtf3=newJTextField(8);
jtf3.setEditable(false);

jp.add(jtf1);
jp.add(jl);
jp.add(jtf2);
jp.add(jb1);
jp.add(jtf3);
getContentPane().add(jp,BorderLayout.CENTER);

}

privatevoidinitMenu(){//菜单栏的初始化和设置
JMenuBarjmb=newJMenuBar();
JMenujm1=newJMenu("系统");
JMenuItemjmi101=newJMenuItem("退出");
jmi101.addActionListener(this);//添加动作响应
JMenujm2=newJMenu("帮助");
JMenuItemjmi201=newJMenuItem("功能说明");
jmi201.addActionListener(this);
jm1.add(jmi101);
jm2.add(jmi201);
jmb.add(jm1);
jmb.add(jm2);
setJMenuBar(jmb);//设置菜单栏
}

//main方法,创建对象窗口,并且设置可见
publicstaticvoidmain(String[]args){
newMyGuiFrame().setVisible(true);
}

//动作响应处理
publicvoidactionPerformed(ActionEvente){
Stringcmd=e.getActionCommand();//根据命令来区分不同的操作
if(cmd.equals("退出")){
System.exit(0);
}
if(cmd.equals("功能说明")){
JOptionPane.showMessageDialog(this,"加法");
}
if(cmd.equals("=")){

Strings1=jtf1.getText().trim();
Strings2=jtf2.getText().trim();
if(s1.equals("")){
s1="0";
}
if(s2.equals("")){
s2="0";
}
doublenum1=Double.parseDouble(s1);//从字符串转小数
doublenum2=Double.parseDouble(s2);
jtf3.setText((num1+num2)+"");//数字类型转字符串类型

}
}

//焦点响应处理
publicvoidfocusGained(FocusEvente){//获得焦点
JComponentcmp=(JComponent)e.getComponent();//根据事件来源组件来区分不同的操作
if(cmp==jtf1||cmp==jtf2){
cmp.setBorder(BorderFactory.createLineBorder(Color.BLUE));//设置边框
}
if(cmp==jb1){
jb1.setForeground(Color.RED);//设置文字颜色
}
}

publicvoidfocusLost(FocusEvente){//失去焦点
JComponentcmp=(JComponent)e.getComponent();
if(cmp==jtf1||cmp==jtf2){
cmp.setBorder(BorderFactory.createLineBorder(Color.GRAY));
}
if(cmp==jb1){
jb1.setForeground(Color.BLACK);
}
}

}


运行效果如图

阅读全文

与javagui图形界面编程实例相关的资料

热点内容
韩国电影姜恩惠禁止的爱 浏览:804
网络游戏数据库靠什么防护 浏览:130
十部顶级的盗墓电影 浏览:324
vbs定时关机代码 浏览:348
甘南大数据 浏览:193
app数据迁移维护多少钱 浏览:599
穿越解放战争成为国军 浏览:595
java正则括号外 浏览:962
恒大电影院今日影片 浏览:424
四级片大全 浏览:545
proe软件能打开什么格式的文件 浏览:47
爱书的下克上小说在线阅读 浏览:850
数据库关系图是什么 浏览:570
猫眼最新实时票房排行 浏览:1000
电话是保存在哪个文件夹 浏览:272
数据填充部门编号怎么做 浏览:814
808d编程软件通讯如何改 浏览:897
穿书肉文 浏览:315
男主从楼上偷看女主 浏览:16
手机连接不上win10投屏 浏览:898

友情链接