图形界面二
1.实验程序
package SYL2;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class SYL extends Frame implements ActionListener{
public SYL() {
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) {
}
2.心得
(1)JAVA的图形界面下有两组控件,一组是awt,一组是swing,一般都是使用swing。
(2)Label用于显示文字; 使用JLabel显示图片,java GUI 显示图片是通过在label上设置图标实现的;
(3)JButton 表示普通按钮
(4)JCheckBox是 复选框,使用isSelected来获取是否选中了
(5)JRadioButton 是单选框 ,使用isSelected来获取是否选中了 ,为了实现只能选中一个,还需要用到ButtonGroup
(6)ButtonGroup 对按钮进行分组,把不同的按钮,放在同一个分组里 ,同一时间,只有一个 按钮 会被选中
(7)JComboBox 是下拉框 ,使用getSelectedItem来获取被选中项 ,使用setSelectedItem() 来指定要选中项