Java习题07_GUI
习题07(01)简单计算器
事件处理:简单计算器实现
题目描述
实现一个简单的计算器,前面是一个文本框(操作数1),后面是标签(操作符),再后面是一个文本框(操作数2),然后是一个"="按钮(JButton),最后是一个文本框(结果)
(1)实现按钮ActionEvent事件的处理,点击等号获取计算结果,并显示在最后一个文本框;
(2)实现第二个文本框的ActionEvent事件处理,在第二个文本框按回车,直接获得计算结果,并显示在最后一个文本框。
(3)实现操作符标签的MouseEvent事件的处理,点击标签,可以更换标签的操作符,比如:将加号改成减号,从而实现减法功能,点击等号按钮,实现减法操作的结果,并显示在第三个文本框。
注:本系统中switch()的case表达式后不能使用字符串常量
输出样例
本题无测试样例
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
CalculatorGUI calculatorGUI = new CalculatorGUI();
}
}
class CalculatorGUI extends JFrame implements ActionListener, KeyListener, MouseListener {
JPanel panel1, panel2, panel3;
String input1 = "";
String input2 = "";
String output = "";
JTextField inputText1, inputText2, outputText;
JLabel symbolLabel;
JButton calculateButton;
int clickNum = 0;
CalculatorGUI() {
panel1 = new JPanel();
panel2 = new JPanel();
inputText1 = new JTextField();
inputText2 = new JTextField();
outputText = new JTextField();
inputText1.setPreferredSize(new Dimension(50, 20));
inputText2.setPreferredSize(new Dimension(50, 20));
outputText.setPreferredSize(new Dimension(50, 20));
symbolLabel = new JLabel("+");
symbolLabel.setPreferredSize(new Dimension(10, 20));
calculateButton = new JButton("=");
calculateButton.setPreferredSize(new Dimension(50, 20));
calculateButton.addActionListener(this);
inputText2.addKeyListener(this);
symbolLabel.addMouseListener(this);
this.setLayout(new FlowLayout());
this.add(inputText1);
this.add(symbolLabel);
this.add(inputText2);
this.add(calculateButton);
this.add(outputText);
this.setTitle("简单计算器");
this.setSize(300, 70);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int width = Toolkit.getDefaultToolkit().getScreenSize().width;
int height = Toolkit.getDefaultToolkit().getScreenSize().height;
this.setLocation(width / 2 - 200, height / 2 - 150);
this.setVisible(true);
}
public String calculate(String num1Text, String num2Text) {
double num = 0;
double num1 = Double.parseDouble(num1Text);
double num2 = Double.parseDouble(num2Text);
String symbolTest = symbolLabel.getText();
if (symbolTest.equals("+"))
num = num1 + num2;
if (symbolTest.equals("-"))
num = num1 - num2;
if (symbolTest.equals("*"))
num = num1 * num2;
if (symbolTest.equals("/"))
num = num1 / num2;
return String.valueOf(num);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == calculateButton) {
String answer = calculate(inputText1.getText(), inputText2.getText());
outputText.setText(answer);
}
}
@Override
public void keyPressed(KeyEvent e) {
String tempText = KeyEvent.getKeyText(e.getKeyCode());
if (tempText.equals("Enter")) {
String answer = calculate(inputText1.getText(), inputText2.getText());
outputText.setText(answer);
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 1) {
if (clickNum == 0)
symbolLabel.setText("+");
else if (clickNum == 1)
symbolLabel.setText("-");
else if (clickNum == 2)
symbolLabel.setText("*");
else if (clickNum == 3)
symbolLabel.setText("/");
clickNum++;
if (clickNum > 3)
clickNum = 0;
}
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
}
习题07(02)生日界面
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class StudentDemo {
public static void main(String[] args) {
StudentGUI studentGUI = new StudentGUI();
String name = studentGUI.getName();
int year = studentGUI.getYear();
int month = studentGUI.getMonth();
int day = studentGUI.getDay();
Student student = new Student(name, year, month, day);
int age = student.calAge();
}
}
class Student {
private String name;
private int year;
private int month;
private int day;
public Student(String name, int year, int month, int day) {
this.name = name;
this.year = year;
this.month = month;
this.day = day;
}
public int calAge() {
int age = 0;
age = 2020 - year;
return age;
}
}
class StudentGUI extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
JLabel nameLabel, yearLabel, monthLabel, dayLabel, ageLabel;
JTextField nameTextField, yearTextField, monthTextField, dayTextField, ageTextField;
JButton calAgeButton;
JPanel namepanel, yearpanel, monthpanel, daypanel, agepanel;
public StudentGUI() {
nameLabel = new JLabel("姓名");
yearLabel = new JLabel("年");
monthLabel = new JLabel("月");
dayLabel = new JLabel("日");
ageLabel = new JLabel("年龄");
nameTextField = new JTextField(10);
yearTextField = new JTextField(10);
monthTextField = new JTextField(10);
dayTextField = new JTextField(10);
ageTextField = new JTextField(10);
namepanel = new JPanel();
yearpanel = new JPanel();
monthpanel = new JPanel();
daypanel = new JPanel();
agepanel = new JPanel();
calAgeButton = new JButton("计算");
namepanel.add(nameLabel);
namepanel.add(nameTextField);
yearpanel.add(yearLabel);
yearpanel.add(yearTextField);
monthpanel.add(monthLabel);
monthpanel.add(monthTextField);
daypanel.add(dayLabel);
daypanel.add(dayTextField);
agepanel.add(ageLabel);
agepanel.add(ageTextField);
calAgeButton.addActionListener(this);
this.setLayout(new GridLayout(6, 1));
this.add(namepanel);
this.add(yearpanel);
this.add(monthpanel);
this.add(daypanel);
this.add(calAgeButton);
this.add(agepanel);
this.setTitle("Student类");
this.setSize(250, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int width = Toolkit.getDefaultToolkit().getScreenSize().width;
int height = Toolkit.getDefaultToolkit().getScreenSize().height;
this.setLocation(width / 2 - 200, height / 2 - 150);
this.setVisible(true);
}
public String getName() {
return nameTextField.getText();
}
public int getYear() {
return Integer.parseInt(yearTextField.getText());
}
public int getMonth() {
return Integer.parseInt(monthTextField.getText());
}
public int getDay() {
return Integer.parseInt(dayTextField.getText());
}
public void setAge(int age) {
ageTextField.setText(String.valueOf(age));
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == calAgeButton) {
ageTextField.setText(String.valueOf(2020 - this.getYear()));
}
}
}
习题07(03)用户界面
教材P191-创建用户界面
题目描述
使用swing中的组件构建图形用户界面,具体图见课本P191,题1,学习通习题7中也有图
输入描述
无
输出描述
无
输入样例
无
输出样例
无(此题无样例,不要在意分数)
import javax.swing.*;
import java.awt.*;
public class UserDemo {
public static void main(String[] args) {
UserGUI userGUI = new UserGUI();
}
}
class UserGUI extends JFrame {
JLabel nameLabel, descriptionLabel;
JTextField nameTextField;
JTextArea descriptionTextArea;
JButton oKButton, cancelButton;
JPanel buttonPanel, namePanel;
public UserGUI() {
namePanel = new JPanel();
nameLabel = new JLabel("请输入书籍名称:");
descriptionLabel = new JLabel("请输入该书籍的详细描述");
nameTextField = new JTextField(10);
descriptionTextArea = new JTextArea();
buttonPanel = new JPanel();
oKButton = new JButton("OK");
cancelButton = new JButton("Cancel");
JScrollPane sp = new JScrollPane(descriptionTextArea);
buttonPanel.add(oKButton);
buttonPanel.add(cancelButton);
namePanel.setLayout(new GridLayout(3, 1));
namePanel.add(nameLabel);
namePanel.add(nameTextField);
namePanel.add(descriptionLabel);
this.setLayout(new BorderLayout());
this.add(namePanel, BorderLayout.NORTH);
this.add(sp, BorderLayout.CENTER);
this.add(buttonPanel, BorderLayout.SOUTH);
this.setTitle("评价");
this.setSize(300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int width = Toolkit.getDefaultToolkit().getScreenSize().width;
int height = Toolkit.getDefaultToolkit().getScreenSize().height;
this.setLocation(width / 2 - 200, height / 2 - 150);
this.setVisible(true);
}
}