📄 caculatorframe.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.util.EventListener;
/**
* Sample application using Frame.
*
* @author
* @version 1.00 07/08/06
*/
public class CaculatorFrame extends JFrame implements ActionListener {
/**
* The constructor.
*/
JTextField jtf = new JTextField();
JPanel jpl = new JPanel();
public CaculatorFrame()
{
//需要使用两种布局管理器,BorderLayout,GridLayout
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//增加文本框
jtf.setHorizontalAlignment(JTextField.RIGHT);
Container c = this.getContentPane();
c.add(jtf,BorderLayout.NORTH);
c.add(jpl,BorderLayout.CENTER);
jpl.setLayout(new GridLayout(4,4));//首先设置16个按键的布局管理
JButton b1 = null;
b1 = new JButton("1");
b1.addActionListener(this);
jpl.add(b1);
JButton b2 = null;
b2 = new JButton("2");
b2.addActionListener(this);
jpl.add(b2);
JButton b3 = null;
b3 = new JButton("3");
b3.addActionListener(this);
jpl.add(b3);
JButton b4 = null;
b4 = new JButton("+");
b4.addActionListener(this);
jpl.add(b4);
JButton b5 = null;
b5 = new JButton("4");
b5.addActionListener(this);
jpl.add(b5);
JButton b6 = null;
b6 = new JButton("5");
b6.addActionListener(this);
jpl.add(b6);
JButton b7 = null;
b7 = new JButton("6");
b7.addActionListener(this);
jpl.add(b7);
JButton b8 = null;
b8 = new JButton("-");
b8.addActionListener(this);
jpl.add(b8);
JButton b9 = null;
b9 = new JButton("7");
b9.addActionListener(this);
jpl.add(b9);
JButton b10 = null;
b10 = new JButton("8");
b10.addActionListener(this);
jpl.add(b10);
JButton b11 = null;
b11 = new JButton("9");
b11.addActionListener(this);
jpl.add(b11);
JButton b12 = null;
b12 = new JButton("*");
b12.addActionListener(this);
jpl.add(b12);
JButton b13 = null;
b13 = new JButton("0");
b13.addActionListener(this);
jpl.add(b13);
JButton b14 = null;
b14 = new JButton(".");
b14.addActionListener(this);
jpl.add(b14);
JButton b15 = null;
b15 = new JButton("=");
b15.addActionListener(this);
jpl.add(b15);
JButton b16 = null;
b16 = new JButton("\\");
b16.addActionListener(this);
jpl.add(b16);
MenuBar menuBar = new MenuBar();
Menu menuFile = new Menu();
MenuItem menuFileExit = new MenuItem();
menuFile.setLabel("File");
menuFileExit.setLabel("Exit");
// Add action listener.for the menu button
menuFileExit.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
CaculatorFrame.this.windowClosed();
}
}
);
menuFile.add(menuFileExit);
menuBar.add(menuFile);
setTitle("Caculator");
setMenuBar(menuBar);
setSize(new Dimension(400, 400));
// Add window listener.
/* this.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
CaculatorFrame.this.windowClosed();
}
}
); */
}
/**
* Shutdown procedure when run as an application.
*/
protected void windowClosed() {
// TODO: Check if it is safe to close the application
// Exit application.
System.exit(0);
}
/**
* Method actionPerformed
*
*
* @param e
*
*/
public void actionPerformed(ActionEvent e)
{
// TODO: Add your code here
jtf.setText(jtf.getText() + e.getActionCommand());//返回按钮文本上的信息
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -