我最近一直试图通过点击按钮开发一个代码,以便在JTable
中将两列数据相乘。按钮单击操作中的代码如下:
for (int i=0;i<model.getRowCount();i++){
Double d= Double.parseDouble((String) model2.getValueAt(i,1));
Double d2= Double.parseDouble((String) model3.getValueAt(i,2));
Double d3=d*d2;
model.setValueAt(d3,i,3);
}
“生成”按钮根据N值添加微调框中的行数:
按钮执行上述代码。在运行应用程序时,我收到以下错误和警告:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1838)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at mU$4.actionPerformed(mU.java:133)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6535)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6300)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4891)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
任何人都可以请更正我的代码或告诉我如何将JTable
中的数据乘以列数据的逻辑吗?
完整代码如下
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),null,null,null});
}
}
});
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);
}
}
答案 0 :(得分:1)
生成按钮的ActionListener
重复将null
添加到TableModel
,这会导致Double.parseDouble()
投掷NullPointerException
。至少要为您的模型添加可解析的值:
for (int i = 0; i < hu; i++) {
String s = String.valueOf(i);
model.addRow(new Object[] {"M" + s, s, "42", ""});
}
请问你能给我正确的代码吗?
所选值仅供参考;您可能希望从标识元素的字符串表示开始,例如"0"
:
model.addRow(new Object[] {"M" + s, "0", "0", ""});
顺便说一句,截图说明了为什么你应该避免绝对定位而选择合适的layout。