⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jlab1002.java

📁 计算机系学习java的专用ppt
💻 JAVA
字号:
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
public class JLab1002 extends JFrame implements ActionListener 
{ 

private JTextField operation; // 结果栏 
private JButton dot, plus, minus, multi, div, sqrt, equal, dao,M,MS,MR,MC,c1,backs,ce,sign,yu; // 运算符 
private JButton[] numbers; // 数字 
// 构造者 
public JLab1002() 
{ 
setTitle("计算器"); 
// 初始化变量 
// 初始化窗口变量 
operation = new JTextField(" 0",250); 
operation.setEditable(false); 
numbers = new JButton[10]; 
for (int i = 0; i < 10; i++) 
numbers[i] = new JButton("" + i); 
 
backs=new JButton("Backplace");
ce=new JButton("CE");
c1=new JButton("C");
dot = new JButton("."); 
plus = new JButton("+"); 
minus = new JButton("-"); 
multi = new JButton("*"); 
sign=new JButton("+/-");
div = new JButton("/"); 
sqrt = new JButton("sqrt"); 
equal = new JButton("="); 
dao = new JButton("1/x"); 
M = new JButton("M+"); 
MS=new JButton("MS+");
MR=new JButton("MR+");
MC=new JButton("MC+");
yu=new JButton("%");
// 将窗口物体放入窗口 
GridBagLayout layout = new GridBagLayout(); 
getContentPane().setLayout(layout);

addComponent(layout, numbers[1], 2, 1, 1, 1); 
addComponent(layout, numbers[2], 2, 2, 1, 1); 
addComponent(layout, numbers[3], 2, 3, 1, 1); 
addComponent(layout, numbers[4], 3, 1, 1, 1); 
addComponent(layout, numbers[5], 3, 2, 1, 1); 
addComponent(layout, numbers[6], 3, 3, 1, 1); 
addComponent(layout, numbers[7], 4, 1, 1, 1); 
addComponent(layout, numbers[8], 4, 2, 1, 1); 
addComponent(layout, numbers[9], 4, 3, 1, 1); 
addComponent(layout, div,2,4,1,1);
addComponent(layout, sqrt,2,5,1,1);
addComponent(layout, multi,3,4,1,1);
addComponent(layout, yu,3,5,1,1);
addComponent(layout, minus,4,4,1,1);
addComponent(layout, dao,4,5,1,1);
addComponent(layout, numbers[0],5,1,1,1);
addComponent(layout, sign,5,2,1,1);
addComponent(layout, dot,5,3,1,1);
addComponent(layout, plus,5,4,1,1);
addComponent(layout, equal,5,5,1,1);
addComponent(layout, operation, 0, 0, 6, 1); 
addComponent(layout, backs,1,1,2,1);
addComponent(layout, ce,1,3,2,1);
addComponent(layout, c1,1,5,1,1);
addComponent(layout, MC,2,0,1,1);
addComponent(layout, MR,3,0,1,1);
addComponent(layout, MS,4,0,1,1);
addComponent(layout, M,5,0,1,1);

} 
// 对按钮进行反应的方法 
public void actionPerformed(ActionEvent e) 
{ 
JButton btn = (JButton)e.getSource(); 
if(btn==numbers[1]) operation.setText(String.valueOf(1));
if(btn==numbers[2]) operation.setText(String.valueOf(2));
if(btn==numbers[3]) operation.setText(String.valueOf(3));
if(btn==numbers[4]) operation.setText(String.valueOf(4));
if(btn==numbers[5]) operation.setText(String.valueOf(5));
if(btn==numbers[6]) operation.setText(String.valueOf(6));
if(btn==numbers[7]) operation.setText(String.valueOf(7));
if(btn==numbers[8]) operation.setText(String.valueOf(8));
if(btn==numbers[9]) operation.setText(String.valueOf(9));
if(btn==numbers[0]) operation.setText(String.valueOf(0));

} 

// 快捷使用GridBagLayout的方法 
private void addComponent(GridBagLayout layout, Component component, int row, int col, int width, int height) 
{ 
GridBagConstraints constraints = new GridBagConstraints(); 
constraints.fill = GridBagConstraints.BOTH; 
constraints.insets = new Insets(1, 1, 1, 1); 
constraints.weightx = 1; 
constraints.weighty = 1; 
constraints.gridx = col; 
constraints.gridy = row; 
constraints.gridwidth = width; 
constraints.gridheight = height; 
layout.setConstraints(component, constraints); 
if (component instanceof JButton) 
((JButton)component).addActionListener(this); 
getContentPane().add(component); 
} 
// 主方法初始化并显示窗口 
public static void main(String[] args) 
{ 
JLab1002 calc = new JLab1002(); 
calc.setSize(500,500); 
calc.setVisible(true); 
calc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} 
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -