[Java] 纯文本查看 复制代码
package swing;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Event;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.peer.ButtonPeer;
import java.util.ArrayList;
import java.util.Set;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
/**
* @title
* [url=home.php?mod=space&uid=686208]@AuThor[/url]
* [url=home.php?mod=space&uid=686237]@date[/url] 2020年6月26日上午10:45:39
*/
@SuppressWarnings({ "serial", "unused" })
public class Calculator extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Calculator frame = new Calculator();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Calculator() {
setTitle("计算器");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 220, 240);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(0, 0, 0, 0));
contentPane.setLayout(new BorderLayout(0,0));
setContentPane(contentPane);
JPanel displayPanel = new JPanel();
contentPane.add(displayPanel,BorderLayout.NORTH);
JTextField display = new JTextField();//创建文本框对象 上面输出框
display.setToolTipText("0");
display.setHorizontalAlignment(SwingConstants.RIGHT);//设置文本框中的文本内容的对齐有式为右对齐
display.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String a = display.getText();
int a1 = Integer.valueOf(display.getText());
int a2 = Integer.valueOf(display.getText());
int sum = a1 + a2;
String summ = sum+"";
// display.setText(summ);
// sum.setText(Integer.parseInt(display.getText())+Integer.parseInt(display.getText())+"");
}
});
display.setEditable(false);//设置文本框不可编辑
display.setFont(new Font("微软雅黑",Font.PLAIN,15));//设置文本框中的字体样式
displayPanel.add(display);//将文本框添加到displayPanel面板中
display.setColumns(13);//设置文本框的长度
JPanel buttonPanel = new JPanel();
contentPane.add(buttonPanel,BorderLayout.CENTER);
buttonPanel.setLayout(new GridLayout(4,4,5,5));
JButton number7Button = new JButton("7");//数字7
number7Button.setFont(new Font("微软雅黑",Font.PLAIN,15));
buttonPanel.add(number7Button);
number7Button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String seven = number7Button.getText();//获取按钮的text,然后赋值给textfield
display.setText(seven);//输出框显示按钮数字
int seven1 = Integer.valueOf(display.getText());//转换成int类型进行运算
}
});
JButton number8Button = new JButton("8");//数字8
number8Button.setFont(new Font("微软雅黑",Font.PLAIN,15));
buttonPanel.add(number8Button);
number8Button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String eight = number8Button.getText();//获取按钮的text,然后赋值给textfield
display.setText(eight);//输出框显示按钮数字
int eight1 = Integer.valueOf(display.getText());//转换成int类型进行运算
}
});
JButton number9Button = new JButton("9");//数字9
number9Button.setFont(new Font("微软雅黑",Font.PLAIN,15));
buttonPanel.add(number9Button);
number9Button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String nine = number9Button.getText();//获取按钮的text,然后赋值给textfield
display.setText(nine);//输出框显示按钮数字
int nine1 = Integer.valueOf(display.getText());//转换成int类型进行运算
}
});
JButton divideButton = new JButton("/");//除法
divideButton.setFont(new Font("微软雅黑",Font.PLAIN,15));
buttonPanel.add(divideButton);
JButton number4Button = new JButton("4");//数字4
number4Button.setFont(new Font("微软雅黑",Font.PLAIN,15));
buttonPanel.add(number4Button);
number4Button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String four = number4Button.getText();//获取按钮的text,然后赋值给textfield
display.setText(four);//输出框显示按钮数字
int four1 = Integer.valueOf(display.getText());//转换成int类型进行运算
}
});
JButton number5Button = new JButton("5");//数字5
number5Button.setFont(new Font("微软雅黑",Font.PLAIN,15));
buttonPanel.add(number5Button);
number5Button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String fiv = number5Button.getText();//获取按钮的text,然后赋值给textfield
display.setText(fiv);//输出框显示按钮数字
int fiv1 = Integer.valueOf(display.getText());//转换成int类型进行运算
}
});
JButton number6Button = new JButton("6");//数字6
number6Button.setFont(new Font("微软雅黑",Font.PLAIN,15));
buttonPanel.add(number6Button);
number6Button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String six = number6Button.getText();//获取按钮的text,然后赋值给textfield
display.setText(six);//输出框显示按钮数字
int six1 = Integer.valueOf(display.getText());//转换成int类型进行运算
}
});
JButton multiplyButton = new JButton("*");//乘法
multiplyButton.setFont(new Font("微软雅黑",Font.PLAIN,15));
buttonPanel.add(multiplyButton);
JButton number3Button = new JButton("1");//数字1
number3Button.setFont(new Font("微软雅黑",Font.PLAIN,15));
buttonPanel.add(number3Button);
number3Button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String one = number3Button.getText();
display.setText(one);//输出框显示按钮数字
int one1 = Integer.valueOf(display.getText());//转换成int类型进行运算
}
});
JButton number2Button = new JButton("2");//数字2
number2Button.setFont(new Font("微软雅黑",Font.PLAIN,15));
buttonPanel.add(number2Button);
number2Button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String two = number2Button.getText();//获取按钮的text,然后赋值给textfield
display.setText(two);//输出框显示按钮数字
int two1 = Integer.valueOf(display.getText());//转换成int类型进行运算
}
});
JButton number1Button = new JButton("3");//数字3
number1Button.setFont(new Font("微软雅黑",Font.PLAIN,15));
buttonPanel.add(number1Button);
number1Button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String third = number1Button.getText();//获取按钮的text,然后赋值给textfield
display.setText(third);//输出框显示按钮数字
int third1 = Integer.valueOf(display.getText());//转换成int类型进行运算
}
});
JButton subtractButton = new JButton("-");//减法
subtractButton.setFont(new Font("微软雅黑",Font.PLAIN,15));
buttonPanel.add(subtractButton);
JButton numberButton = new JButton("0");//数字0
numberButton.setFont(new Font("微软雅黑",Font.PLAIN,15));
buttonPanel.add(numberButton);
numberButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String zero = numberButton.getText();//获取按钮的text,然后赋值给textfield
display.setText(zero);//输出框显示按钮数字
int zero1 = Integer.valueOf(display.getText());//转换成int类型进行运算
}
});
JButton dotButton = new JButton(".");//小数点
dotButton.setFont(new Font("微软雅黑",Font.PLAIN,15));
buttonPanel.add(dotButton);
JButton equalButton = new JButton("=");//等于 (结算)
equalButton.setFont(new Font("微软雅黑",Font.PLAIN,15));
buttonPanel.add(equalButton);
equalButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String a = display.getText();
int a1 = Integer.valueOf(display.getText());
int a2 = Integer.valueOf(display.getText());
if (display.getText() == "0") {
}
// int a2 = Integer.valueOf(display.getText());
int sum = a1 + a2;
String summ = sum+"";
display.setText(summ);
}
});
JButton addButton = new JButton("+");//加法
addButton.setFont(new Font("微软雅黑",Font.PLAIN,15));
buttonPanel.add(addButton);
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (addButton.equals(e.getSource())) {
int zero = 0;
display.setText(zero+"");
}
}
});
}
}