import javax.swing.*; import java.awt.*; import java.awt.event.*; //设置界面框架 public class text1_1 { public static void main(String[] args) { Win win = new Win(); win.setTitle("四则运算"); win.setBounds(0,0,500,200); } } //设置界面 class Win extends JFrame{ Win(){ setLayout(null); Container con = getContentPane();//设置底层容器 con.setBackground(Color.blue); init(); setBounds(10,10,460,360); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } void init(){ JButton button1 = new JButton("加法题目"); JTextArea textshow1 = new JTextArea(); JButton button2 = new JButton("减法题目"); JTextArea textshow2 = new JTextArea(); JButton button3 = new JButton("乘法题目"); JTextArea textshow3 = new JTextArea(); JButton button4 = new JButton("除法题目"); JTextArea textshow4 = new JTextArea(); JButton button6 = new JButton("重置"); textshow1.setFont(new Font("楷体",Font.BOLD,20));//修改字体样式 textshow2.setFont(new Font("楷体",Font.BOLD,20)); textshow3.setFont(new Font("楷体",Font.BOLD,20)); textshow4.setFont(new Font("楷体",Font.BOLD,20)); Listen listen = new Listen(); listen.setJTextArea1(textshow1); listen.setJTextArea2(textshow2); listen.setJTextArea3(textshow3); listen.setJTextArea4(textshow4); button1.addActionListener(listen); button2.addActionListener(listen); button3.addActionListener(listen); button4.addActionListener(listen); button6.addActionListener(listen); button1.setBounds(10,10,100,25); textshow1.setBounds(110,10,200,25); button2.setBounds(10,40,100,25); textshow2.setBounds(110,40,200,25); button3.setBounds(10,70,100,25); textshow3.setBounds(110,70,200,25); button4.setBounds(10,100,100,25); textshow4.setBounds(110,100,200,25); button6.setBounds(350,50,100,25); add(button1); add(textshow1); add(button2); add(textshow2); add(button3); add(textshow3); add(button4); add(textshow4); add(button6); } } //设置监视器 class Listen implements ActionListener{ JTextArea textshow1; JTextArea textshow2; JTextArea textshow3; JTextArea textshow4; int numberOne; int numberTwo; public void setJTextArea1(JTextArea textshow1) { this.textshow1 = textshow1; } public void setJTextArea2(JTextArea textshow2) { this.textshow2 = textshow2; } public void setJTextArea3(JTextArea textshow3) { this.textshow3 = textshow3; } public void setJTextArea4(JTextArea textshow4) { this.textshow4 = textshow4; } public void setnumberOne(int numberOne) { this.numberOne = numberOne; } public void setnumberTwo(int numberTwo) { this.numberTwo = numberTwo; } public void actionPerformed(ActionEvent e) { String str = e.getActionCommand(); if(str.equals("加法题目")) { numberOne = (int)(Math.random()*100); numberTwo = (int)(Math.random()*100); setnumberOne(numberOne); setnumberTwo(numberTwo); int sum1 = numberOne + numberTwo; textshow1.append(numberOne+"+"+numberTwo+"="+sum1); } if(str.equals("减法题目")) { numberOne = (int)(Math.random()*100); numberTwo = (int)(Math.random()*100); setnumberOne(numberOne); setnumberTwo(numberTwo); int sum2 = numberOne - numberTwo; textshow2.append(numberOne+"-"+numberTwo+"="+sum2); } if(str.equals("乘法题目")) { numberOne = (int)(Math.random()*100); numberTwo = (int)(Math.random()*100); setnumberOne(numberOne); setnumberTwo(numberTwo); int sum3 = numberOne * numberTwo; textshow3.append(numberOne+"*"+numberTwo+"="+sum3); } if(str.equals("除法题目")) { numberOne = (int)(Math.random()*100); numberTwo = (int)(Math.random()*100); setnumberOne(numberOne); setnumberTwo(numberTwo); float sum4 =(float) numberOne / numberTwo; textshow4.append(numberOne+"/"+numberTwo+"="+sum4); } if(str.equals("重置")){ textshow1.setText(""); textshow2.setText(""); textshow3.setText(""); textshow4.setText(""); } } }
以下是代码运行界面:
其中界面颜色、按钮参数等可在代码中修改;通过点击加法/减法/乘法/除法题目,可以实现自动出题,点击重置则可以清除已有题目。