JAVA小程序,简单的加法计算!请求帮忙!
程序里有个JTable表,有两列,我想让两列能进行计算!在表里输入数字,然后点按钮进行加法计算!请高手帮忙写一下!问题解决了给追分!谢谢了!importjava.awt....
程序里有个JTable表,有两列,我想让两列能进行计算!
在表里输入数字,然后点按钮进行加法计算!请高手帮忙写一下!
问题解决了给追分!谢谢了!
import java.awt.Dimension;
import java.awt.Rectangle;
import javax.swing.table.DefaultTableModel;
import javax.swing.*;
public class Creg extends JFrame {
DefaultTableModel tableModel = new DefaultTableModel();
private JTable table = new JTable(tableModel);
private JScrollPane jScrollPane1 = new JScrollPane();
private JButton jButton1 = new JButton();
public Creg() {
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Creg creg = new Creg();
creg.setSize(500,400);
creg.setVisible(true);
creg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void jbInit() throws Exception {
jScrollPane1.setBounds(new Rectangle(25, 80, 170, 225));
jButton1.setText("计算两数相加");
jButton1.setBounds(new Rectangle(55, 35, 115, 35));
jScrollPane1.getViewport().add(table, null);
this.getContentPane().add(jButton1, null);
this.getContentPane().add(jScrollPane1, null);
this.getContentPane().setLayout(null);
this.setSize(new Dimension(429, 389));
tableModel.addColumn("第1个数");
tableModel.addColumn("第2个数");
tableModel.addRow(new String[] {"" , ""});
}
} 展开
在表里输入数字,然后点按钮进行加法计算!请高手帮忙写一下!
问题解决了给追分!谢谢了!
import java.awt.Dimension;
import java.awt.Rectangle;
import javax.swing.table.DefaultTableModel;
import javax.swing.*;
public class Creg extends JFrame {
DefaultTableModel tableModel = new DefaultTableModel();
private JTable table = new JTable(tableModel);
private JScrollPane jScrollPane1 = new JScrollPane();
private JButton jButton1 = new JButton();
public Creg() {
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Creg creg = new Creg();
creg.setSize(500,400);
creg.setVisible(true);
creg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void jbInit() throws Exception {
jScrollPane1.setBounds(new Rectangle(25, 80, 170, 225));
jButton1.setText("计算两数相加");
jButton1.setBounds(new Rectangle(55, 35, 115, 35));
jScrollPane1.getViewport().add(table, null);
this.getContentPane().add(jButton1, null);
this.getContentPane().add(jScrollPane1, null);
this.getContentPane().setLayout(null);
this.setSize(new Dimension(429, 389));
tableModel.addColumn("第1个数");
tableModel.addColumn("第2个数");
tableModel.addRow(new String[] {"" , ""});
}
} 展开
1个回答
展开全部
其实代码就只缺 JButton 的事件监听器,及提取、转换并计算和的部分:
import java.awt.event.ActionEvent;
jButton1.addActionListener(new AbstractAction() {
public void actionPerformed(ActionEvent ae) {
try {
double sum = Double.valueOf(tableModel.getValueAt(0, 0).toString()) +
Double.valueOf(tableModel.getValueAt(0, 1).toString());
JOptionPane.showMessageDialog(Creg.this, "结果:" + sum );
} catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(Creg.this,
"数字个数或格式错误!", "", JOptionPane.ERROR_MESSAGE);
}
}
});
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询