该计算器\(swing\)包实现
只实现了简单界面和基本输入
没有实现实际运算功能
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener{
JMenuItem aboutItem;
String[] rightButton = new String[] {"7","8","9","/","4","5","6","*","1","2","3","-","0",".","=","+"};
String[] leftButton = new String[] {"sin","and","(",")","cos","or","exp","ln","tan","xor","x^y","log","e","not","x^3","n!"};
Font font1 = new Font("微软雅黑",Font.BOLD,30);
Font font2 = new Font("微软雅黑",Font.BOLD,20);
JRadioButton dec = new JRadioButton("十进制");
JRadioButton oct = new JRadioButton("八进制");
JRadioButton bin = new JRadioButton("二进制");
JButton[] mainButton = new JButton[20];
JTextField jtf = new JTextField(30);
public Calculator() {
super("计算器");
this.setVisible(true);
this.setBounds(300,300,1000,500);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
//Menu
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu helpMenu = new JMenu("Help");
helpMenu.setFont(font2);
menuBar.add(helpMenu);
aboutItem = new JMenuItem("About");
aboutItem.setFont(font2);
aboutItem.addActionListener(this);
helpMenu.add(aboutItem);
JPanel root = new JPanel();
root.setLayout(new BorderLayout());
JPanel center = new JPanel();
JPanel north = new JPanel();
north.setLayout(new FlowLayout());
center.setLayout(new GridLayout(1,2,20,20));
JPanel centerWest = new JPanel(); //左功能区
centerWest.setLayout(new BorderLayout());
center.add(centerWest);
JPanel centerWestNorth = new JPanel();//进制选择区
centerWestNorth.setLayout(new FlowLayout(FlowLayout.CENTER,20,5));
centerWestNorth.setBorder(BorderFactory.createEtchedBorder());
centerWest.add(centerWestNorth,BorderLayout.NORTH);
ButtonGroup bg=new ButtonGroup();// 进制单选按钮组
dec.setFont(font2); //十进制
dec.addActionListener(this);
dec.setSelected(true);
bg.add(dec);
oct.setFont(font2); //八进制
oct.addActionListener(this);
bg.add(oct);
bin.setFont(font2); //二进制
bin.addActionListener(this);
bg.add(bin);
centerWestNorth.add(dec);
centerWestNorth.add(oct);
centerWestNorth.add(bin);
JPanel centerWestCenter = new JPanel();//左函数区
centerWestCenter.setLayout(new GridLayout(4,4,10,10));
centerWest.add(centerWestCenter,BorderLayout.CENTER);
for(int i=0;i<leftButton.length;i++) {
JButton tmp = new JButton(leftButton[i]);
tmp.setFont(font1);
tmp.setEnabled(false);
centerWestCenter.add(tmp);
}
JPanel centerEast = new JPanel(); //右功能区
centerEast.setLayout(new BorderLayout());
center.add(centerEast);
JPanel centerEastNorth = new JPanel(); //backspace and clear button area
centerEastNorth.setLayout(new FlowLayout(FlowLayout.CENTER,100,5));
centerEastNorth.setBorder(BorderFactory.createEtchedBorder());
centerEast.add(centerEastNorth,BorderLayout.NORTH);
JButton cl = new JButton("CLEAR");
cl.setFont(font2);
cl.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jtf.setText("");
}
});
JButton bs = new JButton("BACKSPACE");
bs.setFont(font2);
bs.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(jtf.getText().length()!=0) {
int end = jtf.getText().length()-1;
jtf.setText(jtf.getText().substring(0, end));
}
}
});
centerEastNorth.add(bs);
centerEastNorth.add(cl);
JPanel centerEastCenter = new JPanel(); //数字键盘区
centerEastCenter.setLayout(new GridLayout(4,4,10,10));
centerEast.add(centerEastCenter,BorderLayout.CENTER);
for(int i=0;i<rightButton.length;i++) {
mainButton[i] = new JButton(rightButton[i]);
mainButton[i].setFont(font1);
String tmp = rightButton[i];
mainButton[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jtf.setText(jtf.getText()+tmp);
}
});
centerEastCenter.add(mainButton[i]);
}
jtf.setFont(font1);//输入框
jtf.setHorizontalAlignment(JTextField.RIGHT);
north.add(jtf);
root.add(center,BorderLayout.CENTER);
root.add(north,BorderLayout.NORTH);
this.setContentPane(root);
validate();
}
public void actionPerformed(ActionEvent event) {
if(event.getSource()==aboutItem) {
JOptionPane.showMessageDialog(this,"Message Dialog","Message Dialog",JOptionPane.INFORMATION_MESSAGE);
}
else if(event.getSource()==dec){
for(int i=0;i<rightButton.length;i++) {
mainButton[i].setEnabled(true);
}
}
else if(event.getSource()==oct){
for(int i=0;i<rightButton.length;i++) {
if(i==1||i==2) {
mainButton[i].setEnabled(false);
}
else {
mainButton[i].setEnabled(true);
}
}
}
else if(event.getSource()==bin){
for(int i=0;i<rightButton.length;i++) {
if(i==3||i==7||i==8||i>=11) {
mainButton[i].setEnabled(true);
}
else {
mainButton[i].setEnabled(false);
}
}
}
}
public static void main(String[] args) {
new Calculator();
}
}