Experiment XI, twelve experiments

Experiment XI:

Source:

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 example 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

}

}}

Experimental experience:

The experiment compared to the previous experiment, adding event listeners, the program is more perfect.

 

Experiment 12:

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 ( "Software Engineering"));
  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);
 }
}

 

Experimental experience: swing awt component assembly more convenient than without the use of extra event listeners.

Guess you like

Origin www.cnblogs.com/myb1128/p/11041352.html