设计与实现一个小学生四则运算考试系统要求:
(1)要用Java Swing组件设计GUI界面
(2)能够进行加、减、乘、除四种运算,能够自动出题,自动计时,自动
判断对错并评分,可以注册用户保存用户信息到文件中,可以读取文件并完成登录验证。
只用写基础运算
这个问题不是很难,但是很费时间,自己搞一搞应该还是可以搞出来的。swing开发,可以关注我~有问题好交流
回答:折磨,绝对的折磨,还不如用Winform,哭死了,这调格式得调哭死
package calculate;
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
/**
* @author bbyh
* @date 2022/12/13 0013 21:57
* @description
*/
public class MainFrame extends JFrame {
public MainFrame() {
Panel titlePanel = new Panel();
Panel leftPanel = new Panel();
Panel centerPanel = new Panel();
Panel rightPanel = new Panel();
setLayout(new BorderLayout());
add(titlePanel, BorderLayout.NORTH);
add(leftPanel, BorderLayout.WEST);
add(centerPanel, BorderLayout.CENTER);
add(rightPanel, BorderLayout.EAST);
// 标题
initTitle(titlePanel);
// 左侧按钮
initLeftButton(leftPanel);
// 中间区域
JPanel centerTopPanel = new JPanel();
JPanel centerContentPanel = new JPanel();
centerPanel.setLayout(new BorderLayout());
centerPanel.add(centerTopPanel, BorderLayout.NORTH);
centerPanel.add(centerContentPanel, BorderLayout.CENTER);
// 中间区域的按钮
initCenterButton(centerTopPanel, centerContentPanel);
// 中间区域的文本框
initCenterText(centerContentPanel);
// 右侧当前用户
initRightUser(rightPanel);
}
private void initRightUser(Panel rightPanel) {
rightPanel.setLayout(new FlowLayout());
JLabel currentUser = new JLabel("当前用户为:");
JLabel username = new JLabel("暂定,待设置");
Font fontOfUser = new Font("楷体", Font.BOLD, 16);
currentUser.setFont(fontOfUser);
username.setFont(fontOfUser);
rightPanel.add(currentUser);
rightPanel.add(username);
}
private void changeCenterText(JPanel centerContentPanel) {
centerContentPanel.removeAll();
centerContentPanel.setVisible(false);
initChangeCenterText(centerContentPanel);
centerContentPanel.setVisible(true);
}
private void initChangeCenterText(JPanel centerContentPanel) {
JLabel questionOne = new JLabel("题目1: ");
JLabel questionTwo = new JLabel("题目2: ");
JLabel questionTree = new JLabel("题目3: ");
JLabel questionFour = new JLabel("题目4: ");
JLabel questionFive = new JLabel("题目5: ");
JLabel questionOneText = new JLabel("1");
JLabel questionTwoText = new JLabel("2");
JLabel questionTreeText = new JLabel("3");
JLabel questionFourText = new JLabel("4");
JLabel questionFiveText = new JLabel("5");
JTextField questionOneAnswer = new JTextField();
JTextField questionTwoAnswer = new JTextField();
JTextField questionTreeAnswer = new JTextField();
JTextField questionFourAnswer = new JTextField();
JTextField questionFiveAnswer = new JTextField();
JLabel questionOneAnswerText = new JLabel("1");
JLabel questionTwoAnswerText = new JLabel("2");
JLabel questionTreeAnswerText = new JLabel("3");
JLabel questionFourAnswerText = new JLabel("4");
JLabel questionFiveAnswerText = new JLabel("5");
Font fontOfAnswer = new Font("楷体", Font.BOLD, 20);
questionOne.setFont(fontOfAnswer);
questionTwo.setFont(fontOfAnswer);
questionTree.setFont(fontOfAnswer);
questionFour.setFont(fontOfAnswer);
questionFive.setFont(fontOfAnswer);
questionOneText.setFont(fontOfAnswer);
questionTwoText.setFont(fontOfAnswer);
questionTreeText.setFont(fontOfAnswer);
questionFourText.setFont(fontOfAnswer);
questionFiveText.setFont(fontOfAnswer);
questionOneAnswer.setFont(fontOfAnswer);
questionTwoAnswer.setFont(fontOfAnswer);
questionTreeAnswer.setFont(fontOfAnswer);
questionFourAnswer.setFont(fontOfAnswer);
questionFiveAnswer.setFont(fontOfAnswer);
questionOneAnswerText.setFont(fontOfAnswer);
questionTwoAnswerText.setFont(fontOfAnswer);
questionTreeAnswerText.setFont(fontOfAnswer);
questionFourAnswerText.setFont(fontOfAnswer);
questionFiveAnswerText.setFont(fontOfAnswer);
// 设置位置
setTextLayout(centerContentPanel, questionOne, questionTwo, questionTree, questionFour, questionFive, questionOneText, questionTwoText, questionTreeText, questionFourText, questionFiveText, questionOneAnswer, questionTwoAnswer, questionTreeAnswer, questionFourAnswer, questionFiveAnswer, questionOneAnswerText, questionTwoAnswerText, questionTreeAnswerText, questionFourAnswerText, questionFiveAnswerText);
addComponentToCenterContentPanel(centerContentPanel, questionOne, questionTwo, questionTree, questionFour, questionFive, questionOneText, questionTwoText, questionTreeText, questionFourText, questionFiveText, questionOneAnswer, questionTwoAnswer, questionTreeAnswer, questionFourAnswer, questionFiveAnswer, questionOneAnswerText, questionTwoAnswerText, questionTreeAnswerText, questionFourAnswerText, questionFiveAnswerText);
}
private void addComponentToCenterContentPanel(JPanel centerContentPanel, JLabel questionOne, JLabel questionTwo, JLabel questionTree, JLabel questionFour, JLabel questionFive,
JLabel questionOneText, JLabel questionTwoText, JLabel questionTreeText, JLabel questionFourText, JLabel questionFiveText,
JTextField questionOneAnswer, JTextField questionTwoAnswer, JTextField questionTreeAnswer, JTextField questionFourAnswer, JTextField questionFiveAnswer,
JLabel questionOneAnswerText, JLabel questionTwoAnswerText, JLabel questionTreeAnswerText, JLabel questionFourAnswerText, JLabel questionFiveAnswerText) {
centerContentPanel.setLayout(null);
centerContentPanel.add(questionOne);
centerContentPanel.add(questionOneText);
centerContentPanel.add(questionOneAnswer);
centerContentPanel.add(questionOneAnswerText);
centerContentPanel.add(questionTwo);
centerContentPanel.add(questionTwoText);
centerContentPanel.add(questionTwoAnswer);
centerContentPanel.add(questionTwoAnswerText);
centerContentPanel.add(questionTree);
centerContentPanel.add(questionTreeText);
centerContentPanel.add(questionTreeAnswer);
centerContentPanel.add(questionTreeAnswerText);
centerContentPanel.add(questionFour);
centerContentPanel.add(questionFourText);
centerContentPanel.add(questionFourAnswer);
centerContentPanel.add(questionFourAnswerText);
centerContentPanel.add(questionFive);
centerContentPanel.add(questionFiveText);
centerContentPanel.add(questionFiveAnswer);
centerContentPanel.add(questionFiveAnswerText);
}
private void setTextLayout(JPanel centerContentPanel, JLabel questionOne, JLabel questionTwo, JLabel questionTree, JLabel questionFour, JLabel questionFive,
JLabel questionOneText, JLabel questionTwoText, JLabel questionTreeText, JLabel questionFourText, JLabel questionFiveText,
JTextField questionOneAnswer, JTextField questionTwoAnswer, JTextField questionTreeAnswer, JTextField questionFourAnswer, JTextField questionFiveAnswer,
JLabel questionOneAnswerText, JLabel questionTwoAnswerText, JLabel questionTreeAnswerText, JLabel questionFourAnswerText, JLabel questionFiveAnswerText) {
int textWidth = 130;
int textHeight = 40;
int startOfX = 140;
centerContentPanel.setBounds(10, 10, 900, 500);
questionOne.setBounds(startOfX, 10, textWidth, textHeight);
questionTwo.setBounds(startOfX, 70, textWidth, textHeight);
questionTree.setBounds(startOfX, 130, textWidth, textHeight);
questionFour.setBounds(startOfX, 190, textWidth, textHeight);
questionFive.setBounds(startOfX, 250, textWidth, textHeight);
questionOneText.setBounds(startOfX + 150, 10, textWidth, textHeight);
questionTwoText.setBounds(startOfX + 150, 70, textWidth, textHeight);
questionTreeText.setBounds(startOfX + 150, 130, textWidth, textHeight);
questionFourText.setBounds(startOfX + 150, 190, textWidth, textHeight);
questionFiveText.setBounds(startOfX + 150, 250, textWidth, textHeight);
int textAnswerWidth = 200;
int textAnswerHeight = 40;
questionOneAnswer.setBounds(startOfX + 300, 10, textAnswerWidth, textAnswerHeight);
questionTwoAnswer.setBounds(startOfX + 300, 70, textAnswerWidth, textAnswerHeight);
questionTreeAnswer.setBounds(startOfX + 300, 130, textAnswerWidth, textAnswerHeight);
questionFourAnswer.setBounds(startOfX + 300, 190, textAnswerWidth, textAnswerHeight);
questionFiveAnswer.setBounds(startOfX + 300, 250, textAnswerWidth, textAnswerHeight);
questionOneAnswerText.setBounds(startOfX + 510, 10, textAnswerWidth, textAnswerHeight);
questionTwoAnswerText.setBounds(startOfX + 510, 70, textAnswerWidth, textAnswerHeight);
questionTreeAnswerText.setBounds(startOfX + 510, 130, textAnswerWidth, textAnswerHeight);
questionFourAnswerText.setBounds(startOfX + 510, 190, textAnswerWidth, textAnswerHeight);
questionFiveAnswerText.setBounds(startOfX + 510, 250, textAnswerWidth, textAnswerHeight);
}
private void initCenterText(JPanel centerContentPanel) {
JLabel questionOne = new JLabel("题目1: ");
JLabel questionTwo = new JLabel("题目2: ");
JLabel questionTree = new JLabel("题目3: ");
JLabel questionFour = new JLabel("题目4: ");
JLabel questionFive = new JLabel("题目5: ");
JTextField questionOneInput = new JTextField();
JTextField questionTwoInput = new JTextField();
JTextField questionTreeInput = new JTextField();
JTextField questionFourInput = new JTextField();
JTextField questionFiveInput = new JTextField();
Font questionFont = new Font("楷体", Font.BOLD, 20);
questionOne.setFont(questionFont);
questionTwo.setFont(questionFont);
questionTree.setFont(questionFont);
questionFour.setFont(questionFont);
questionFive.setFont(questionFont);
questionOneInput.setFont(questionFont);
questionTwoInput.setFont(questionFont);
questionTreeInput.setFont(questionFont);
questionFourInput.setFont(questionFont);
questionFiveInput.setFont(questionFont);
// 初始化位置
int textWidth = 130;
int textHeight = 40;
centerContentPanel.setBounds(10, 10, 700, 500);
questionOne.setBounds(210, 10, textWidth, textHeight);
questionTwo.setBounds(210, 70, textWidth, textHeight);
questionTree.setBounds(210, 130, textWidth, textHeight);
questionFour.setBounds(210, 190, textWidth, textHeight);
questionFive.setBounds(210, 250, textWidth, textHeight);
int textInputWidth = 300;
int textInputHeight = 40;
questionOneInput.setBounds(350, 10, textInputWidth, textInputHeight);
questionTwoInput.setBounds(350, 70, textInputWidth, textInputHeight);
questionTreeInput.setBounds(350, 130, textInputWidth, textInputHeight);
questionFourInput.setBounds(350, 190, textInputWidth, textInputHeight);
questionFiveInput.setBounds(350, 250, textInputWidth, textInputHeight);
centerContentPanel.setLayout(null);
centerContentPanel.add(questionOne);
centerContentPanel.add(questionOneInput);
centerContentPanel.add(questionTwo);
centerContentPanel.add(questionTwoInput);
centerContentPanel.add(questionTree);
centerContentPanel.add(questionTreeInput);
centerContentPanel.add(questionFour);
centerContentPanel.add(questionFourInput);
centerContentPanel.add(questionFive);
centerContentPanel.add(questionFiveInput);
}
private void initCenterButton(JPanel centerTopPanel, JPanel centerContentPanel) {
JButton publishQuestion = new JButton("出题");
JButton submit = new JButton("提交");
JButton again = new JButton("再来一次");
Font centerButtonFont = new Font("楷体", Font.BOLD, 20);
publishQuestion.setFont(centerButtonFont);
submit.setFont(centerButtonFont);
again.setFont(centerButtonFont);
centerTopPanel.setLayout(new FlowLayout());
centerTopPanel.add(publishQuestion);
centerTopPanel.add(submit);
centerTopPanel.add(again);
// 按下出题按钮,进行页面切换
publishQuestion.addActionListener(e -> {
changeCenterText(centerContentPanel);
});
}
private void initLeftButton(Panel leftPanel) {
JButton baseCalculate = new JButton("基础运算");
JButton intCalculate = new JButton("整数混合运算");
JButton decimalCalculate = new JButton("小数混合运算");
JButton exit = new JButton("退出");
Font leftButtonFont = new Font("楷体", Font.BOLD, 20);
baseCalculate.setFont(leftButtonFont);
intCalculate.setFont(leftButtonFont);
decimalCalculate.setFont(leftButtonFont);
exit.setFont(leftButtonFont);
int btnWidth = 180;
int btnHeight = 50;
leftPanel.setBounds(0, 100, 200, 600);
baseCalculate.setBounds(5, 10, btnWidth, btnHeight);
intCalculate.setBounds(5, 80, btnWidth, btnHeight);
decimalCalculate.setBounds(5, 150, btnWidth, btnHeight);
exit.setBounds(5, 220, btnWidth, btnHeight);
leftPanel.setLayout(null);
leftPanel.add(baseCalculate);
leftPanel.add(intCalculate);
leftPanel.add(decimalCalculate);
leftPanel.add(exit);
}
private void initTitle(Panel titlePanel) {
JLabel title = new JLabel("欢迎使用小学生四则运算测试系统");
Font titleFont = new Font("楷体", Font.BOLD, 24);
title.setFont(titleFont);
titlePanel.add(title);
title.setBorder(new LineBorder(Color.BLACK, 2));
}
public static void main(String[] args) {
JFrame frame = new MainFrame();
frame.setTitle("小学生四则运算测试系统");
frame.setSize(1200, 700);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}