一.程序代码
package jisuanqi2;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Jisuanqi extends Frame implements ActionListener{
public Jisuanqi() {
super("计算器");
Frame ff=new Frame("framework test");
ff.setSize(400,100);
ff.setLocation(300,240);
ff.setLayout(new FlowLayout());
final TextField f1=new TextField("10",8);
ff.add(f1);
//this.add(new Label("+"));
Label l1=new Label("+");
ff.add(l1);
//this.add(new TextField("20",8));
TextField f2=new TextField("20",8);
ff.add(f2);
//this.add(new Button("="));
Button b1=new Button("=");
ff.add(b1);
//this.add(new TextField(10));
TextField f3=new TextField(10);
ff.add(f3);
ff.addWindowListener(new myclose());
ff.setVisible(true);
b1.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent e)
{double c;
String s1=f1.getText();
double a=Integer.parseInt(s1);
String s2=f2.getText();
double b=Integer.parseInt(s2);
c=a+b;
String m=String.valueOf(c);
f3.setText(m)}
private double Integer(String s) {
return 0;
}
});
}
class myclose implements WindowListener{
public void windowActivated(WindowEvent arg0) {
}
public void windowClosed(WindowEvent arg0) {
// TODO Auto-generated method stub
}
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}
public void windowDeactivated(WindowEvent arg0) {
}
public void windowDeiconified(WindowEvent arg0) {
}
public void windowIconified(WindowEvent arg0) {
}
public void windowOpened(WindowEvent arg0) {
}
}
public static void main(String[] args) {
new Jisuanqi();
}
public void actionPerformed(ActionEvent arg0) {
}
}二.实验心得
本次实验在上一次实验的基础上添加新的功能,用以实现
为控件加监测实现驱动。在编写过程中遇到很多问题,不过在
同学的帮助下解决了。对图形界面知识点的掌握还是不够熟练,
实验十二
package ziwojieshao;
import java.awt.FlowLayout;
import javax.swing.*;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class AA extends JFrame{
public AA()
{
JFrame jf1=new JFrame ("Hellow");
jf1.setLayout(new FlowLayout());
jf1.getContentPane().add(new JButton("姓名")) ;
jf1.getContentPane().add(new JTextField("陈香明",10)) ;
JRadioButton j1=new JRadioButton("女",true);
JRadioButton j2=new JRadioButton("男");
ButtonGroup g=new ButtonGroup();
g.add(j1);
g.add(j2);
JPanel p1=new JPanel();
p1.add(j1);
p1.add(j2);
jf1.getContentPane().add(p1);
jf1.setSize(320,210);
String proList[] = { "年龄","民族" ,"籍贯","学号","学院","专业"};
JComboBox comboBox;
Container conPane = getContentPane();
comboBox = new JComboBox(proList);
comboBox.setEditable(true);
conPane.add(comboBox);
JTextField b=new JTextField(20);
jf1.add(conPane);
jf1.add(b);
comboBox.addActionListene
r(new ActionListener()
{public void actionPerformed(Actio
nListener(new ActionListener()
{public void actionPerformed(ActionEvent e)
{
if(comboBox.getSelectedItem().equals("年龄"))
b.setText("19");
else if(comboBox.getSelectedItem().equals("民族"))
b.setText("汉");
else if(comboBox.getSelectedItem().equals("籍贯"))
b.setText("青海西宁");
else if(comboBox.getSelectedItem().equals("学号"))
b.setText("20173311106");
else if(comboBox.getSelectedItem().equals("学院"))
b.setText("计算机学院");
else if(comboBox.getSelectedItem().equals("专业"))
b.setText("网络工程");
}
});
jf1.setVisible(true);
}
public static void main (String[] args) {
new AA()
; }
}
二.实验心得
一开始没有对输入流输入的字符串转换为double型数据,
导致不能转换,采用转为double型,并抛出异常后解决了这个问题。