package GUI;
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
/**
*
* @author Zhu Zhengyan
*/
public class My_GUI3 {
public static void main(String[] args)
{
EventQueue.invokeLater(()->
{
JFrame frame = new CalculatorFrame();
frame.setTitle("Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
}
}
class CalculatorFrame extends JFrame
{
public CalculatorFrame()
{
JPanel panel = new CalculatorPanel();
add(panel);
pack();
}
}
计算器panelcore java 1第10版 p479,感觉书上不好,下面是我改进后的代码
事件处理、布局、组件
package GUI;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
*
* @author Zhu Zhengyan
*/
public class CalculatorPanel extends JPanel{
private JButton display;
private double result;
private String lastCommand;
private boolean start;
private JPanel panel_1 = new JPanel();
private JPanel panel_2 = new JPanel();
public CalculatorPanel()
{
setLayout(null);
setSize(600,500);
result = 0;
lastCommand = "+";
start = true;
display = new JButton("0");
display.setEnabled(false);
display.setBounds(0,0,600,100);
ActionListener insert = new InsertAction();
ActionListener command = new CommandAction();
panel_1.setLayout(null);
panel_1.setBounds(0,0,600,100);
panel_2.setLayout(new GridLayout(4,4));
panel_2.setBounds(0,100,600,400);
display.setFont(new Font("宋体",Font.PLAIN,32));
display.setForeground(Color.RED); //设置button中text的颜色···因为Enabled是false,所以不会变
panel_1.add(display);
add(panel_1);
addButton("7",insert);
addButton("8",insert);
addButton("9",insert);
addButton("/",command);
addButton("4",insert);
addButton("5",insert);
addButton("6",insert);
addButton("*",command);
addButton("1",insert);
addButton("2",insert);
addButton("3",insert);
addButton("-",command);
addButton("0",insert);
addButton(".",insert);
addButton("=",command);
addButton("+",command);
add(panel_2);
}
private void addButton(String label,ActionListener listener)//定义一个一步添加button和listener的方法
{
JButton button = new JButton(label);
button.addActionListener(listener);
button.setFont(new Font("宋体",Font.PLAIN,28)); //设置button中text || 3个参数是:字体、样式(粗体斜体等)、大小(字号)
button.setForeground(new Color(139,69,19)); //自己调色用new Color
panel_2.add(button); //注意此处是panel_2
}
private class InsertAction implements ActionListener
{
public void actionPerformed(ActionEvent event) //重写动作执行方法
{ //这个方法是,每按一个数字按钮,屏幕上就显示这个按钮的
String input = event.getActionCommand(); //text,并拼接在一起(计算器的输入一个数)
if(start)
{
display.setText("");
start = false;
}
display.setText(display.getText()+input);//这个计算器,表面显示0,实际上是0+
}
}
private class CommandAction implements ActionListener
{
public void actionPerformed(ActionEvent event) //每输入一个符号,就进行一次运算,不显示
{
String command = event.getActionCommand();
if(command.equals("="))
{
calculate(Double.parseDouble(display.getText()));
display.setText(String.valueOf(result)); //当用户输入=,就把运算结果显示出来
result = 0; //这三步是重置计算器
lastCommand = "+";
start = true;
}
else
{
calculate(Double.parseDouble(display.getText()));
display.setText(command); //显示用户按下的运算符号
lastCommand = command;
start = true; //start = true时,就可以输入另外的数了
}
}
}
public void calculate(double x) //计算器的计算方法
{
if(lastCommand.equals("+"))
result += x;
else if(lastCommand.equals("-"))
result -= x;
else if(lastCommand.equals("*"))
result *= x;
else if(lastCommand.equals("/"))
result /= x;
}
public Dimension getPreferredSize()
{
return new Dimension(600,500);
}
}