急 编写一个Java程序,在程序中建立一个窗口
我给你写一个吧,记得采纳正确答案,免得对不起我花掉的这十几分钟
================================================================
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Test extends JFrame implements ActionListener{
JTextField jtf1 = new JTextField();
JTextField jtf2 = new JTextField();
JButton jb1 = new JButton("加");
JButton jb2 = new JButton("减");
JButton jb3 = new JButton("乘");
JButton jb4 = new JButton("除");
JTextField jtf3 = new JTextField();
public Test(){
setBounds(300,300,300,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setLayout(null);
jtf1.setBounds(10,50,60,30);
add(jtf1);
jtf2.setBounds(220, 50, 60, 30);
add(jtf2);
jb1.setBounds(120, 20, 60, 30);
add(jb1);
jb2.setBounds(120, 60, 60, 30);
add(jb2);
jb3.setBounds(120, 100, 60, 30);
add(jb3);
jb4.setBounds(120, 140, 60, 30);
add(jb4);
jtf3.setBounds(110, 200, 80, 30);
add(jtf3);
jtf3.setEditable(false);
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
jb4.addActionListener(this);
}
public static void main(String[] args) {
Test t = new Test();
}
public void actionPerformed(ActionEvent e) {
try{
if("加".equals(e.getActionCommand())){
jtf3.setText(""+(Integer.parseInt(jtf1.getText())+Integer.parseInt(jtf2.getText())));
}else if("减".equals(e.getActionCommand())){
jtf3.setText(""+(Integer.parseInt(jtf1.getText())-Integer.parseInt(jtf2.getText())));
}else if("乘".equals(e.getActionCommand())){
jtf3.setText(""+(Integer.parseInt(jtf1.getText())*Integer.parseInt(jtf2.getText())));
}else if("除".equals(e.getActionCommand())){
jtf3.setText(""+(Integer.parseInt(jtf1.getText())/Integer.parseInt(jtf2.getText())));
}
}catch(Exception ex){
jtf3.setText("error");
}
}
}
=======================================================
计算器的话,我这边还有代码,我自己也是新手,第一次做这个。呵呵
不全是计算器,只是一点点的吧
一楼已经写好了,就这个样子差不多了吧