我有一个小应用程序如下GUI OF APP
问题是我将从表列1,2中获取数据并将两个数据相乘并将答案打印到第3列一切正常但我没有得到最后一行结果并将异常抛给我关注working error
完整代码如下
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
public class mU extends JFrame {
private JPanel contentPane;
private JTable table;
private JTextField textField;
Double l2;
Double l4;
Double d,d2;
int hu;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
mU frame = new mU();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public mU() {
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch(Exception w1){
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 486, 297);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNumberOfMachines = new JLabel("Number of Machines");
lblNumberOfMachines.setBounds(10, 11, 120, 14);
contentPane.add(lblNumberOfMachines);
JSpinner spinner = new JSpinner();
spinner.setBounds(140, 8, 48, 20);
contentPane.add(spinner);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(208, 11, 252, 239);
contentPane.add(scrollPane);
table = new JTable();
table.setRowSelectionAllowed(false);
table.setModel(new DefaultTableModel(
new Object[][] {
},
new String[] {
"Machine", "N(1)", "N(0)", "Qan"
}
));
scrollPane.setViewportView(table);
DefaultTableModel model = (DefaultTableModel) table.getModel();
table.setModel(model);
JLabel lblSum = new JLabel("Sum");
lblSum.setBounds(10, 117, 46, 14);
contentPane.add(lblSum);
textField = new JTextField();
textField.setBounds(39, 114, 105, 20);
contentPane.add(textField);
textField.setColumns(10);
JButton btnNewButton = new JButton("Generate");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DefaultTableModel model = (DefaultTableModel) table.getModel();
table.setModel(model);
int hu=(Integer) spinner.getValue();
for(int i=0;i<hu;i++) {
model.addRow(new Object[] {"M"+(i+1),"0","0",""});
}
}
});
btnNewButton.setBounds(10, 36, 77, 23);
contentPane.add(btnNewButton);
JButton btnClear = new JButton("CLEAR");
btnClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
model.setRowCount(0);
}
});
btnClear.setBounds(89, 36, 65, 23);
contentPane.add(btnClear);
JButton btnInitilize = new JButton("N");
btnInitilize.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int i=0; i<=model.getColumnCount(); i++)
{
Double d= Double.parseDouble((String) model.getValueAt(i,1));
Double d2= Double.parseDouble((String) model.getValueAt(i,2));
Double d3=d*d2;
model.setValueAt(d3,i,3);
}
}
});
btnInitilize.setBounds(10, 63, 46, 23);
contentPane.add(btnInitilize);
}
}
任何人都可以帮助我吗
答案 0 :(得分:2)
在btnInitilize
动作监听器中,行
for (int i=0; i<=model.getColumnCount(); i++)
应该是
for (int i=0; i<model.getRowCount(); i++)
根据您的请求,您的代码更改了一行。您没有经过测试,例外:
package mU;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
public class mU extends JFrame {
private JPanel contentPane;
private JTable table;
private JTextField textField;
Double l2;
Double l4;
Double d,d2;
int hu;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
mU frame = new mU();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public mU() {
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch(Exception w1){
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 486, 297);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNumberOfMachines = new JLabel("Number of Machines");
lblNumberOfMachines.setBounds(10, 11, 120, 14);
contentPane.add(lblNumberOfMachines);
final JSpinner spinner = new JSpinner();
spinner.setBounds(140, 8, 48, 20);
contentPane.add(spinner);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(208, 11, 252, 239);
contentPane.add(scrollPane);
table = new JTable();
table.setRowSelectionAllowed(false);
table.setModel(new DefaultTableModel(
new Object[][] {
},
new String[] {
"Machine", "N(1)", "N(0)", "Qan"
}
));
scrollPane.setViewportView(table);
final DefaultTableModel model = (DefaultTableModel) table.getModel();
table.setModel(model);
JLabel lblSum = new JLabel("Sum");
lblSum.setBounds(10, 117, 46, 14);
contentPane.add(lblSum);
textField = new JTextField();
textField.setBounds(39, 114, 105, 20);
contentPane.add(textField);
textField.setColumns(10);
JButton btnNewButton = new JButton("Generate");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DefaultTableModel model = (DefaultTableModel) table.getModel();
table.setModel(model);
int hu=(Integer) spinner.getValue();
for(int i=0;i<hu;i++) {
model.addRow(new Object[] {"M"+(i+1),"0","0",""});
}
}
});
btnNewButton.setBounds(10, 36, 77, 23);
contentPane.add(btnNewButton);
JButton btnClear = new JButton("CLEAR");
btnClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
model.setRowCount(0);
}
});
btnClear.setBounds(89, 36, 65, 23);
contentPane.add(btnClear);
JButton btnInitilize = new JButton("N");
btnInitilize.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int i=0; i<model.getRowCount(); i++)
{
Double d= Double.parseDouble((String) model.getValueAt(i,1));
Double d2= Double.parseDouble((String) model.getValueAt(i,2));
Double d3=d*d2;
model.setValueAt(d3,i,3);
}
}
});
btnInitilize.setBounds(10, 63, 46, 23);
contentPane.add(btnInitilize);
}
}