一个java程序 基于Swing的图形用户界面设计
编写程序,设计一个GUI程序,实现100以内两位整数相加的加法器。功能是随机产生两个整数(加数和被加数),考生输入和数,若输入正确,给出“正确”提示,总分加1,否则,给出...
编写程序,设计一个GUI程序,实现100以内两位整数相加的加法器。
功能是随机产生两个整数(加数和被加数),考生输入和数,若输入正确,给出“正确”提示,总分加1,否则,给出“错”提示,共100道题程序结束,给出“总分是:****”的提示。
很急的,各位高手帮帮忙~~
可以不用对话框做吗?我们还没学... 展开
功能是随机产生两个整数(加数和被加数),考生输入和数,若输入正确,给出“正确”提示,总分加1,否则,给出“错”提示,共100道题程序结束,给出“总分是:****”的提示。
很急的,各位高手帮帮忙~~
可以不用对话框做吗?我们还没学... 展开
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++;
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);
}
}
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++;
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);
}
}
2010-04-13
展开全部
import javax.swing.JOptionPane;
public class Test {
static int answer, x, y, score = 0;
static String generate() {
x = (int) (Math.random() * 100);
y = (int) (Math.random() * 100);
answer = x + y;
return "请问" + x + "+" + y + "=?";
}
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, "现在开始测试,总共一百题!");
for (int i = 0; i < 100; i++) {
String tmp = JOptionPane.showInputDialog(generate());
if (tmp == null) break;
try {
if (Integer.parseInt(tmp) == answer) {
score++;
JOptionPane.showMessageDialog(null, "恭喜答对,进入下一步!");
} else {
JOptionPane.showMessageDialog(null, "错!");
}
} catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(null, "请输入数字!");
i--;
continue;
}
}
JOptionPane.showMessageDialog(null, "测试结束,你的分数是:" + score);
}
}
不用对话框的话,代码会长得多,所以你确定?
public class Test {
static int answer, x, y, score = 0;
static String generate() {
x = (int) (Math.random() * 100);
y = (int) (Math.random() * 100);
answer = x + y;
return "请问" + x + "+" + y + "=?";
}
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, "现在开始测试,总共一百题!");
for (int i = 0; i < 100; i++) {
String tmp = JOptionPane.showInputDialog(generate());
if (tmp == null) break;
try {
if (Integer.parseInt(tmp) == answer) {
score++;
JOptionPane.showMessageDialog(null, "恭喜答对,进入下一步!");
} else {
JOptionPane.showMessageDialog(null, "错!");
}
} catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(null, "请输入数字!");
i--;
continue;
}
}
JOptionPane.showMessageDialog(null, "测试结束,你的分数是:" + score);
}
}
不用对话框的话,代码会长得多,所以你确定?
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询