怎么样用java编写界面实现两个数的加法运算

2025-01-05 16:34:49
推荐回答(2个)
回答1:

这个必须通过swing技术实现,先看下效果图: import java.awt.BorderLayout;import java.awt.Color;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.ImageIcon;import ...

回答2:

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Enumeration;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource;

public class NewFrame extends JFrame {
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
private int a;
private int b;
private int total = 0;
private int result = 0;
private Random random = new Random();

public NewFrame() {
initComponents();
}

public int getA() {
a = random.nextInt(100);
return a;
}

public void setA(int a) {
this.a = a;
}

public int getB() {
b = random.nextInt(100);
return b;
}

public void setB(int b) {
this.b = b;
}

private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;

jTextField1 = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jTextField2 = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
jTextField3 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("加法测试");
getContentPane().setLayout(new java.awt.GridBagLayout());

jTextField1.setColumns(8);
jTextField1.setText(getA() + "");
jTextField1.setEditable(false);
getContentPane().add(jTextField1, new java.awt.GridBagConstraints());

jLabel2.setText("加");
getContentPane().add(jLabel2, new java.awt.GridBagConstraints());

jTextField2.setColumns(8);
jTextField2.setEditable(false);
jTextField2.setText(getB() + "");
jTextField3.addKeyListener(new KeyListener() {

public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if (KeyEvent.VK_ENTER == e.getKeyCode()) {
System.out.println("keyPressed");
action();
}
}

public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}

public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub

}

});
getContentPane().add(jTextField2, new java.awt.GridBagConstraints());

jLabel3.setText("等于");
getContentPane().add(jLabel3, new java.awt.GridBagConstraints());

jTextField3.setColumns(8);
getContentPane().add(jTextField3, new java.awt.GridBagConstraints());

jButton1.setText("确定");
jButton1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
action();
}
});
getContentPane().add(jButton1, new java.awt.GridBagConstraints());

jLabel1.setText("总分:0 ");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
getContentPane().add(jLabel1, gridBagConstraints);

pack();
}

private void action() {

try {
if (total >= 99) {
JOptionPane.showMessageDialog(null, "测试结束" + jLabel1.getText(),
"提示", 1);
jTextField3.setText("测试结束");
return;
}
int a = Integer.parseInt(jTextField1.getText());
int b = Integer.parseInt(jTextField2.getText());
int c = Integer.parseInt(jTextField3.getText().trim());
System.out.println(a + " " + b + " " + c);
if (a + b == c) {
result++;
JOptionPane.showMessageDialog(null, "正确", "正确提示", 1);
} else {
JOptionPane.showMessageDialog(null, "错误", "错误提示", 1);
}
total++;
if (total < 3) {
jLabel1.setText("总分:" + result + " ");
jTextField1.setText(getA() + "");
jTextField2.setText(getB() + "");
jTextField3.setText("");
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "输入提示", "请输入数字", 1);
}

}

public static void main(String[] args) {
Font font = new Font("宋体", Font.PLAIN, 12);
for (Enumeration keys = UIManager.getDefaults().keys(); keys
.hasMoreElements();) {
Object key = keys.nextElement();
Object value = UIManager.get(key);
if (value instanceof FontUIResource) {
UIManager.put(key, font);
}
}
NewFrame frame = new NewFrame();
frame.setLocationRelativeTo(null);
frame.setSize(400, 200);
frame.setVisible(true);
}
}