Project 7 Design and Implementation of Graphical User Interface
Purpose
Master the use of Java graphical components and layout managers; master the use of Java event handling mechanisms; master the use of various controls in the graphical interface, such as labels, text boxes, buttons, check boxes, list boxes, window frames, etc.; Master the menu and pop-up menu design.
Experimental nature
Confirmatory experiment + design experiment
Experiment content
(1) Analyze and debug the examples in Chapter 8 of the textbook
slightly.
(2) Design a simple calculator
Design a simple calculator, as shown in the figure below. Enter the operands in the two text boxes on the right side of the "Operands" label. When you click the operator +, -, ×, ÷ buttons, perform operations on the two operands and fill in the result to the right of the "Result" label. Text box on the side.
import javax.swing.JFrame;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class example {
public static void main(String[] args) {
new LoginFrame();
}
}
class LoginFrame extends JFrame {
JTextField num1;
JTextField num2;
JTextField num3;
JPanel p;
public LoginFrame(){
this.setBounds(300, 300, 280, 200);
this.setTitle("轩哥牌计算器");
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Font ft=new Font("宋体",Font.PLAIN,24) ;
JLabel A=new JLabel("操作数:");
A.setFont(ft);
JLabel B=new JLabel("操作数:");
B.setFont(ft);
JLabel consequent=new JLabel("结果:");
consequent.setFont(ft);
num1=new JTextField(10);
num1.setFont(ft);
num2=new JTextField(10);
num2.setFont(ft);
num3=new JTextField(10);
num3.setFont(ft);
JButton ok=new JButton();
ok.setText("+");
ok.setActionCommand("+");
ok.setFont(ft);
ok.addActionListener(new OKAction());
ActionListener al=new OKAction();
ok.addActionListener(al);
//事件,事件源,对事件源添加相应的事件监听器,编写事件处理类(实现事件监听器接口,重写其抽象方法):内部类)
JButton ok2=new JButton("-");
ok2.setActionCommand("-");
ok2.setFont(ft);
ok2.addActionListener(new OKAction());
JButton ok3=new JButton("*");
ok3.setActionCommand("*");
ok3.setFont(ft);
ok3.addActionListener(new OKAction());
JButton ok4=new JButton("/");
ok4.setActionCommand("/");
ok4.setFont(ft);
ok4.addActionListener(new OKAction());
p=new JPanel();
p.setBackground(new Color(255,255,255));
p.add(A);
p.add(num1);
p.add(B);
p.add(num2);
p.add(ok);
p.add(ok2);
p.add(ok3);
p.add(ok4);
p.add(consequent);
p.add(num3);
this.add(p);
this.setVisible(true);
}
class OKAction implements ActionListener{
//成员内部类 编写事件处理类(实现事件监听器接口,重写其抽象方法):内部类
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
JPanel q = new JPanel();
String cmd=arg0.getActionCommand();
switch(cmd){
case "+":{
String NUM1=num1.getText();
int num11;
num11=Integer.parseInt(NUM1);
String NUM2=new String(num2.getText());
int num22;
num22=Integer.parseInt(NUM2);
int num33=num11+num22;
String a=Integer.toString(num33);
num3.setText(a);
}
break;
case "*":{
String NUM1=num1.getText();
int num11;
num11=Integer.parseInt(NUM1);
String NUM2=new String(num2.getText());
int num22;
num22=Integer.parseInt(NUM2);
int num33=num11*num22;
String a=Integer.toString(num33);
num3.setText(a);
}
break;
case "-":{
String NUM1=num1.getText();
int num11;
num11=Integer.parseInt(NUM1);
String NUM2=new String(num2.getText());
int num22;
num22=Integer.parseInt(NUM2);
int num33=num11-num22;
String a=Integer.toString(num33);
num3.setText(a);
}
break;
case "/":{
String NUM1=num1.getText();
int num11;
num11=Integer.parseInt(NUM1);
String NUM2=new String(num2.getText());
int num22;
num22=Integer.parseInt(NUM2);
int num33=num11/num22;
String a=Integer.toString(num33);
num3.setText(a);
}
break;
}
}
}
}
(3) Design a JAVA program to imitate Notepad
Design a JAVA program to imitate part of the functions of NotePad, the interface is as shown in the figure below.
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class NotePad extends JFrame {
public static void main(String[] args) {
// TODO Auto-generated method stub
new NotePad();
}
public NotePad() {
Container c = getContentPane();
JTextArea ta = new JTextArea(20,50);
JScrollPane sp =new JScrollPane(ta);
c.add(sp);
setTitle("轩哥牌记事本");
setBounds(500, 200, 500, 450);
JMenuBar menupanel = new JMenuBar();
setJMenuBar(menupanel);
JMenu wenjian = new JMenu("文件");// 创建菜单对象
menupanel.add(wenjian);// 将菜单对象添加到菜单栏对象中
JMenuItem xinjian = new JMenu("新建");
JMenuItem dakai = new JMenu("打开");
JMenuItem baocun = new JMenu("保存");
JMenuItem tuichu = new JMenu("退出");
wenjian.add(xinjian);
wenjian.add(dakai);
wenjian.add(baocun);
wenjian.add(tuichu);
JMenu bianji = new JMenu("编辑");// 创建菜单对象
menupanel.add(bianji);// 将菜单对象添加到菜单栏对象中
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
(4) Design a student information management interface
Student information includes: student ID (required to be unique), name, gender (selected by radio button when entering), date of birth (entered with date and time control), major (entered with combo box), photo. Entry function: store the information of several students in an array according to the above requirements; browse function: use the list box to display all the entered student information (pay attention to the beautiful display effect); query function: query according to student ID, gender, and major , And display the query results in the list box. (Hint: Define the student information structure or class, and define the student information array as a member of the form class). The use of the system is required to be authenticated, that is, before the main interface is displayed, the login window is displayed first, and the user name and password are checked. Only those who pass the verification can see the main interface, otherwise the program is not allowed to be used after 3 chances. system.