简体   繁体   English

如何通过单击按钮添加具有行和列的新JTable?

[英]How to add new JTable with rows and columns by clicking button?

I am a beginner in Java and I need help. 我是Java的初学者,需要帮助。

I want to add table with rows and columns in JFrame or JPanel when a button is clicked. 我想在单击按钮时在JFrameJPanel添加具有行和列的表。 How to add new table with rows and columns by clicking button? 如何通过单击按钮添加具有行和列的新表?

  1. First you have to add JButton to frame 首先,您必须将JButton添加到框架
  2. Than add ActionListener in button 比在按钮中添加ActionListener
  3. than make a list when button is pressed like this 像这样按下按钮时列出清单

     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); } }); 

More information on JButton & JTable . 有关JButtonJTable更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM