实验十一:
源代码:
package ji;
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
//import 例题62.QueryFrame.WinClose;
public class Add extends Frame{
public static void main(String[]arg){new Add();}
private Button button_char;//button_nui;
public Add() {
super("运算器");
this.setSize(400,200);
this.setLocation(300,240);
this.setBackground(Color.white);
this.setLayout(new FlowLayout());
this.add(new TextField("10",8));
this.add(new Label("+"));
this.add(new TextField("20",8));
this.button_char=new Button("=");
this.add(this.button_char);
this.add(new TextField(10));
this.add(new TextField("30",8));
this.add(new Label("-"));
this.add(new TextField("20",8));
this.button_char=new Button("=");
this.add(this.button_char);
//this.add(new Button("="));
this.add(new TextField(10));
this.add(new TextField("10",8));
this.add(new Label("*"));
this.add(new TextField("20",8));
this.button_char=new Button("=");
this.add(this.button_char);
//this.add(new Button("="));
this.add(new TextField(10));
this.add(new TextField("10",8));
this.add(new Label("/"));
this.add(new TextField("2",8));
this.button_char=new Button("=");
this.add(this.button_char);
//this.add(new Button("="));
this.add(new TextField(10));
button_char.addActionListener(new MyActionListener());
this.setVisible(true);
this.addWindowListener(new WinClose());
}
class MyActionListener implements ActionListener{
// 事件处理方法
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("有人点击了按钮");
System.out.println(((Button)e.getSource()).getLabel());}
}
public class WinClose implements WindowListener
{
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
// TODO Auto-generated method stub
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
}}
实验心得:
本次实验相比上一次实验,添加了事件监听,是程序功能更加完善。
实验十二:
package 实验十二;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
@SuppressWarnings("unused")
public class Jianli{
public static void main(String[] args) {
JFrame jf1=new JFrame("简历");
jf1.getContentPane().setLayout(new FlowLayout());
jf1.getContentPane().add(new JLabel("姓名"));
jf1.getContentPane().add(new JTextField(20));
jf1.getContentPane().add(new JLabel("性别"));
JRadioButton j2=new JRadioButton("女");
jf1.add(j2);
jf1.getContentPane().add(new JLabel(""));
JRadioButton j21=new JRadioButton("男");
jf1.add(j21);
jf1.getContentPane().add(new JLabel("民族"));
jf1.getContentPane().add(new JTextField(20));
jf1.getContentPane().add(new JLabel("年龄"));
jf1.getContentPane().add(new JTextField(20));
jf1.getContentPane().add(new JLabel("专业"));
jf1.getContentPane().add(new JLabel("从以下选择"));
jf1.getContentPane().add(new JLabel("网络工程"));
jf1.getContentPane().add(new JRadioButton("是"));
jf1.getContentPane().add(new JLabel("物联网工程"));
jf1.getContentPane().add(new JRadioButton("是"));
jf1.getContentPane().add(new JLabel("软件工程"));
jf1.getContentPane().add(new JRadioButton("是"));
jf1.getContentPane().add(new JLabel("计算机科学与技术"));
jf1.getContentPane().add(new JRadioButton("是"));
jf1.setLayout(new GridLayout(15,2));
jf1.setSize(400, 400);
jf1.setVisible(true);
}
}
实验心得:swing组件比起awt组件更加方便,不需要使用多余的事件监听。