JAVA问题...没人回答!我又问了..!
用Swing做一个GUI界面的小程序!能计算2个数相加加一个按钮,输入完2个系数之后,点击按钮做加法计算,把得出的结果显示在JTable表里,重复计算时,在显示在JTab...
用Swing做一个GUI界面的小程序!能计算2个数相加
加一个按钮,输入完2个系数之后,点击按钮做加法计算,把得出的结果显示在JTable表里,重复计算时,在显示在JTable表里(表里加一行结果)!
高手们帮忙写一个例子.... 展开
加一个按钮,输入完2个系数之后,点击按钮做加法计算,把得出的结果显示在JTable表里,重复计算时,在显示在JTable表里(表里加一行结果)!
高手们帮忙写一个例子.... 展开
1个回答
展开全部
这个应该可以满足你的要求了,
用 jbuilderX 的界面设计器制作的界面。
不懂的可以问我,QQ:2838844
MAIL:neu.hubs@gmail.com
import javax.swing.*;
import java.awt.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.*;
public class PlusCaculator
extends JFrame {
JTextField tfNum1 = new JTextField();
JTextField tfNum2 = new JTextField();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JTextField tfAnswer = new JTextField();
JButton btnCaculate = new JButton();
// 下面两句比较关键
DefaultTableModel tableModel = new DefaultTableModel(); //存放jtable中的数据的 tableModel
JTable tabAnswer = new JTable(tableModel);
JScrollPane jScrollPane1 = new JScrollPane();
public PlusCaculator() {
try {
jbInit();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
void jbInit() throws Exception {
jScrollPane1.setBounds(new Rectangle(24, 57, 354, 230));
this.getContentPane().setLayout(null);
tfNum1.setMinimumSize(new Dimension(11, 21));
tfNum1.setPreferredSize(new Dimension(11, 21));
tfNum1.setText("");
tfNum1.setBounds(new Rectangle(32, 21, 66, 21));
tfNum2.setText("");
tfNum2.setBounds(new Rectangle(126, 22, 65, 21));
jLabel1.setText("+");
jLabel1.setBounds(new Rectangle(105, 25, 17, 16));
jLabel2.setText("=");
jLabel2.setBounds(new Rectangle(205, 27, 16, 16));
tfAnswer.setText("");
tfAnswer.setBounds(new Rectangle(228, 23, 64, 21));
btnCaculate.setBounds(new Rectangle(306, 21, 68, 25));
btnCaculate.setText("计算");
btnCaculate.addActionListener(new
PlusCaculator_btnCaculate_actionAdapter(this));
this.setLocale(java.util.Locale.getDefault());
this.getContentPane().add(tfNum2, null);
this.getContentPane().add(jScrollPane1, null);
this.getContentPane().add(tfNum1, null);
this.getContentPane().add(jLabel1, null);
this.getContentPane().add(tfAnswer, null);
this.getContentPane().add(btnCaculate, null);
this.getContentPane().add(jLabel2, null);
jScrollPane1.getViewport().add(tabAnswer, null);
// 初始化 tableModel, 一共有3列
tableModel.addColumn("加数1");
tableModel.addColumn("加数2");
tableModel.addColumn("结果");
}
public static void main(String[] args) {
PlusCaculator plusCaculator = new PlusCaculator();
plusCaculator.setTitle("简单加法计算器");
plusCaculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
plusCaculator.setLocation(200, 200);
plusCaculator.setSize(500, 400);
plusCaculator.setVisible(true);
}
// 点击 计算按钮执行的方法
void btnCaculate_actionPerformed(ActionEvent e) {
try {
int num1 = Integer.parseInt(tfNum1.getText().trim()); // 加数1
int num2 = Integer.parseInt(tfNum2.getText().trim()); //加数2
int sum = num1 + num2;
tfAnswer.setText("" + sum);
// 将结果加到 Jtable中
tableModel.addRow(new String[] {"" + num1, "" + num2, "" + sum});
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
class PlusCaculator_btnCaculate_actionAdapter
implements java.awt.event.ActionListener {
PlusCaculator adaptee;
PlusCaculator_btnCaculate_actionAdapter(PlusCaculator adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnCaculate_actionPerformed(e);
}
}
用 jbuilderX 的界面设计器制作的界面。
不懂的可以问我,QQ:2838844
MAIL:neu.hubs@gmail.com
import javax.swing.*;
import java.awt.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.*;
public class PlusCaculator
extends JFrame {
JTextField tfNum1 = new JTextField();
JTextField tfNum2 = new JTextField();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JTextField tfAnswer = new JTextField();
JButton btnCaculate = new JButton();
// 下面两句比较关键
DefaultTableModel tableModel = new DefaultTableModel(); //存放jtable中的数据的 tableModel
JTable tabAnswer = new JTable(tableModel);
JScrollPane jScrollPane1 = new JScrollPane();
public PlusCaculator() {
try {
jbInit();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
void jbInit() throws Exception {
jScrollPane1.setBounds(new Rectangle(24, 57, 354, 230));
this.getContentPane().setLayout(null);
tfNum1.setMinimumSize(new Dimension(11, 21));
tfNum1.setPreferredSize(new Dimension(11, 21));
tfNum1.setText("");
tfNum1.setBounds(new Rectangle(32, 21, 66, 21));
tfNum2.setText("");
tfNum2.setBounds(new Rectangle(126, 22, 65, 21));
jLabel1.setText("+");
jLabel1.setBounds(new Rectangle(105, 25, 17, 16));
jLabel2.setText("=");
jLabel2.setBounds(new Rectangle(205, 27, 16, 16));
tfAnswer.setText("");
tfAnswer.setBounds(new Rectangle(228, 23, 64, 21));
btnCaculate.setBounds(new Rectangle(306, 21, 68, 25));
btnCaculate.setText("计算");
btnCaculate.addActionListener(new
PlusCaculator_btnCaculate_actionAdapter(this));
this.setLocale(java.util.Locale.getDefault());
this.getContentPane().add(tfNum2, null);
this.getContentPane().add(jScrollPane1, null);
this.getContentPane().add(tfNum1, null);
this.getContentPane().add(jLabel1, null);
this.getContentPane().add(tfAnswer, null);
this.getContentPane().add(btnCaculate, null);
this.getContentPane().add(jLabel2, null);
jScrollPane1.getViewport().add(tabAnswer, null);
// 初始化 tableModel, 一共有3列
tableModel.addColumn("加数1");
tableModel.addColumn("加数2");
tableModel.addColumn("结果");
}
public static void main(String[] args) {
PlusCaculator plusCaculator = new PlusCaculator();
plusCaculator.setTitle("简单加法计算器");
plusCaculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
plusCaculator.setLocation(200, 200);
plusCaculator.setSize(500, 400);
plusCaculator.setVisible(true);
}
// 点击 计算按钮执行的方法
void btnCaculate_actionPerformed(ActionEvent e) {
try {
int num1 = Integer.parseInt(tfNum1.getText().trim()); // 加数1
int num2 = Integer.parseInt(tfNum2.getText().trim()); //加数2
int sum = num1 + num2;
tfAnswer.setText("" + sum);
// 将结果加到 Jtable中
tableModel.addRow(new String[] {"" + num1, "" + num2, "" + sum});
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
class PlusCaculator_btnCaculate_actionAdapter
implements java.awt.event.ActionListener {
PlusCaculator adaptee;
PlusCaculator_btnCaculate_actionAdapter(PlusCaculator adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnCaculate_actionPerformed(e);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询