我是Java的初学者,我需要帮助。
我想在点击按钮时在JFrame
或JPanel
中添加包含行和列的表格。如何通过单击按钮添加包含行和列的新表?
答案 0 :(得分:0)
JButton
添加到第ActionListener
比按下按钮时列表
JButton b = new JButton("Click Me");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String[] columnNames = {"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian"};
Object[][] data = {
{"Kathy", "Smith",
"Snowboarding", new Integer(5), new Boolean(false)},
{"John", "Doe",
"Rowing", new Integer(3), new Boolean(true)},
{"Sue", "Black",
"Knitting", new Integer(2), new Boolean(false)},
{"Jane", "White",
"Speed reading", new Integer(20), new Boolean(true)},
{"Joe", "Brown",
"Pool", new Integer(10), new Boolean(false)}
};
JTable table = new JTable(data, columnNames);
add(table);
}
});