目录
一、设计要求
二、设计思路
1、初始化窗体
2、功能的实现
3、布局方式
4、简易类方法
三、完整代码附录
一、设计要求
设计一个简易计算器,可以实现基本的四则运算,以及自动获取随机计算式的功能。
实现如下:
二、设计思路
使用GUI编程,即图像用户界面,实现更好的交互,增强软件的实用性。
1、初始化窗体
对所需的组键进行初始化,构建构造方法:
public class Test1 {
public static void main(String []agrs)
{
new Computer();
}
}
class Computer extends JFrame
{
private JTextField FieldOne=new JTextField(5);
private JTextField FieldTwo=new JTextField(5);
private JComboBox<String>Symbol;
private JButton Button_Count;
private JButton Button_GetNumber,Button_GetResult;
private JLabel Tag1,Tag2;
private JTextField AnswerField=new JTextField(10);
private JTextArea Area=new JTextArea(15,15);
private JTextArea RandomArea=new JTextArea(1,1);
private Box box1,box2,box3,box4,box_a;
public Computer()
{
init();
setBounds(300,300,400,320);
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
void init()
2、功能的实现
主要是通过init()方法来进行实现,关键是监听器和匿名类的实现,代码有点长:
void init()
{
final double[] Aanswer = new double[1];
final double[] Result = new double[1];
setTitle("计算器");
setLayout(new FlowLayout());
box1=Box.createHorizontalBox();
box2=Box.createHorizontalBox();
box3=Box.createHorizontalBox();
box4=Box.createVerticalBox();
box_a=Box.createHorizontalBox();
Symbol=new JComboBox<String>();
Symbol.setSelectedIndex(-1);
Symbol.addItem("+");
Symbol.addItem("-");
Symbol.addItem("*");
Symbol.addItem("/");
Tag1=new JLabel("点击随机获取计算式");
Button_GetNumber=new JButton("获取");
Button_Count=new JButton("计算");
Button_Count.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
int number1 = Integer.parseInt(FieldOne.getText());
int number2 = Integer.parseInt(FieldTwo.getText());
int SymbolNumber= Symbol.getSelectedIndex();//Symbol.getSelectedIndex();
String Symbol="0";
switch (SymbolNumber)
{
case 0:Symbol="+";break;
case 1:Symbol="-";break;
case 2:Symbol="*";break;
case 3:Symbol="/";break;
}
Area.append(" "+number1+Symbol+" "+number2+" = "+GetResult(number1,number2,Symbol)+"\n");
}
catch (Exception Exp)
{
int x =JOptionPane.showConfirmDialog(null,"输入了非法字符!",
"检查", JOptionPane.CLOSED_OPTION);
}
FieldOne.setText(null);
FieldTwo.setText(null);
}
});
Tag2=new JLabel("结果:");
Button_GetResult=new JButton("计算校验");
Button_GetResult.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
if(AnswerField.getText().equals("") ){
int x =JOptionPane.showConfirmDialog(null,"请确认是否输入了答案",
"确认", JOptionPane.CLOSED_OPTION);
}
if(!(AnswerField.getText().equals("")))
{
Result[0] = Double.parseDouble((AnswerField.getText()));
Area.append(" "+Result[0]);
if(Result[0]==Aanswer[0])
{
Area.append(" 对了!\n");
}
else
{
Area.append(" 错了!\n");
}
RandomArea.setText(null);
AnswerField.setText(null);
}
}
});
box1.add(FieldOne);
box1.add(Symbol);
box1.add(FieldTwo);
box1.add(Button_Count);
box2.add(Tag1);
box2.add(Box.createHorizontalStrut(15));
box2.add(Button_GetNumber);
Button_GetNumber.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (!(RandomArea.getText().equals(""))) {
int x = JOptionPane.showConfirmDialog(null, "请确认是否输入了计算结果",
"确认", JOptionPane.CLOSED_OPTION);
}
if (RandomArea.getText().equals("")) {
int number1 = new Random().nextInt(100 - 1) + 1;
int number2 = new Random().nextInt(100 - 1) + 1;
String Symbol[] = {"+", "-", "*", "/"};
String symbol = GetRamdom(Symbol);
RandomArea.setText(null);
RandomArea.append((number1) + " " + symbol +
" " + (number2) + " = ?"+"\n");
Aanswer[0] =GetResult(number1,number2,symbol);
Area.append(" "+String.valueOf(number1)+" "+symbol+
" "+String.valueOf(number2)+" = " +"\n");
}
}
});
box3.add(Tag2);
box3.add(AnswerField);
box3.add(Box.createHorizontalStrut(15));
box3.add(Button_GetResult);
box3.add(Box.createVerticalStrut(15));
box_a.add(new JLabel("随机计算式:"));
box_a.add(Box.createHorizontalStrut(10));
box_a.add(RandomArea);
box4.add(box1);
box4.add(Box.createVerticalStrut(15));
box4.add(box2);
box4.add(Box.createVerticalStrut(15));
box4.add(box_a);
box4.add(Box.createVerticalStrut(15));
box4.add(box3);
box4.add(Box.createVerticalStrut(15));
box4.add(Area);
add(box4);
}
3、布局方式
我主要采用的是Box盒子布局,即把若干的行式布局的容器装进一个盒子,再把四个这样的盒子装进一个列式布局的盒子,最后把所以盒子加到一个大盒子里,把大盒子装进容器中就可以了。
box4.add(box1);
box4.add(Box.createVerticalStrut(15));
box4.add(box2);
box4.add(Box.createVerticalStrut(15));
box4.add(box_a);
box4.add(Box.createVerticalStrut(15));
box4.add(box3);
box4.add(Box.createVerticalStrut(15));
box4.add(Area);
add(box4);//大盒子添加到容器中
4、简易类方法
编写了两个简单的类方法给匿名类监听器实现基本运算:
public static String GetRamdom (String[] Arry)
{
Random rand=new Random();
int number=rand.nextInt(Arry.length);
return Arry[number];
}
public static int GetResult(int x,int y,String symbol)
{
switch (symbol)
{
case "+": return x+y;
case "-": return x-y;
case "*": return x*y;
case "/": return x/y;
}
return 0;
}
三、完整代码附录
仅供参考学习使用:
package GUI__Test1;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class Test1 {
public static void main(String []agrs)
{
new Computer();
}
}
class Computer extends JFrame
{
private JTextField FieldOne=new JTextField(5);
private JTextField FieldTwo=new JTextField(5);
private JComboBox<String>Symbol;
private JButton Button_Count;
private JButton Button_GetNumber,Button_GetResult;
private JLabel Tag1,Tag2;
private JTextField AnswerField=new JTextField(10);
private JTextArea Area=new JTextArea(15,15);
private JTextArea RandomArea=new JTextArea(1,1);
private Box box1,box2,box3,box4,box_a;
public Computer()
{
init();
setBounds(300,300,400,320);
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
void init()
{
final double[] Aanswer = new double[1];
final double[] Result = new double[1];
setTitle("计算器");
setLayout(new FlowLayout());
box1=Box.createHorizontalBox();
box2=Box.createHorizontalBox();
box3=Box.createHorizontalBox();
box4=Box.createVerticalBox();
box_a=Box.createHorizontalBox();
Symbol=new JComboBox<String>();
Symbol.setSelectedIndex(-1);
Symbol.addItem("+");
Symbol.addItem("-");
Symbol.addItem("*");
Symbol.addItem("/");
Tag1=new JLabel("点击随机获取计算式");
Button_GetNumber=new JButton("获取");
Button_Count=new JButton("计算");
Button_Count.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
int number1 = Integer.parseInt(FieldOne.getText());
int number2 = Integer.parseInt(FieldTwo.getText());
int SymbolNumber= Symbol.getSelectedIndex();//Symbol.getSelectedIndex();
String Symbol="0";
switch (SymbolNumber)
{
case 0:Symbol="+";break;
case 1:Symbol="-";break;
case 2:Symbol="*";break;
case 3:Symbol="/";break;
}
Area.append(" "+number1+Symbol+" "+number2+" = "+GetResult(number1,number2,Symbol)+"\n");
}
catch (Exception Exp)
{
int x =JOptionPane.showConfirmDialog(null,"输入了非法字符!",
"检查", JOptionPane.CLOSED_OPTION);
}
FieldOne.setText(null);
FieldTwo.setText(null);
}
});
Tag2=new JLabel("结果:");
Button_GetResult=new JButton("计算校验");
Button_GetResult.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
if(AnswerField.getText().equals("") ){
int x =JOptionPane.showConfirmDialog(null,"请确认是否输入了答案",
"确认", JOptionPane.CLOSED_OPTION);
}
if(!(AnswerField.getText().equals("")))
{
Result[0] = Double.parseDouble((AnswerField.getText()));
Area.append(" "+Result[0]);
if(Result[0]==Aanswer[0])
{
Area.append(" 对了!\n");
}
else
{
Area.append(" 错了!\n");
}
RandomArea.setText(null);
AnswerField.setText(null);
}
}
});
box1.add(FieldOne);
box1.add(Symbol);
box1.add(FieldTwo);
box1.add(Button_Count);
box2.add(Tag1);
box2.add(Box.createHorizontalStrut(15));
box2.add(Button_GetNumber);
Button_GetNumber.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (!(RandomArea.getText().equals(""))) {
int x = JOptionPane.showConfirmDialog(null, "请确认是否输入了计算结果",
"确认", JOptionPane.CLOSED_OPTION);
}
if (RandomArea.getText().equals("")) {
int number1 = new Random().nextInt(100 - 1) + 1;
int number2 = new Random().nextInt(100 - 1) + 1;
String Symbol[] = {"+", "-", "*", "/"};
String symbol = GetRamdom(Symbol);
RandomArea.setText(null);
RandomArea.append((number1) + " " + symbol +
" " + (number2) + " = ?"+"\n");
Aanswer[0] =GetResult(number1,number2,symbol);
Area.append(" "+String.valueOf(number1)+" "+symbol+
" "+String.valueOf(number2)+" = " +"\n");
}
}
});
box3.add(Tag2);
box3.add(AnswerField);
box3.add(Box.createHorizontalStrut(15));
box3.add(Button_GetResult);
box3.add(Box.createVerticalStrut(15));
box_a.add(new JLabel("随机计算式:"));
box_a.add(Box.createHorizontalStrut(10));
box_a.add(RandomArea);
box4.add(box1);
box4.add(Box.createVerticalStrut(15));
box4.add(box2);
box4.add(Box.createVerticalStrut(15));
box4.add(box_a);
box4.add(Box.createVerticalStrut(15));
box4.add(box3);
box4.add(Box.createVerticalStrut(15));
box4.add(Area);
add(box4);
}
public static String GetRamdom (String[] Arry)
{
Random rand=new Random();
int number=rand.nextInt(Arry.length);
return Arry[number];
}
public static int GetResult(int x,int y,String symbol)
{
switch (symbol)
{
case "+": return x+y;
case "-": return x-y;
case "*": return x*y;
case "/": return x/y;
}
return 0;
}
}