我正在创建一个Java swing应用程序。我复制了代码的精简版。我有一个要填充一些数据的JTable。当用户按下GO时,它将打开一个新窗口,并填充JTable中的数据。我希望将数据填充到JTable中与GO按钮相同的窗口中。有什么想法为什么每次我按下按钮都会打开一个相同的新窗口?

public class test extends JFrame {

protected JPanel mainPane;
protected JTable displayTable;
protected JPanel tabbedPanel;
protected JTabbedPane tabbedPane;
protected DefaultTableModel displayModel;
protected JButton displayButton;
protected JComboBox<String> comboBox;
public test() {
    setVisible(true);
    setBounds(100, 100, 1000, 600);
    setResizable(false);
    mainPane = new JPanel();
    mainPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(mainPane);
    GridBagLayout gbl_mainPane = new GridBagLayout();
    gbl_mainPane.columnWidths = new int[] { 0, 93, 42, 189, 165, 0, 184, 0 };
    gbl_mainPane.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    gbl_mainPane.columnWeights = new double[] { 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, Double.MIN_VALUE };
    gbl_mainPane.rowWeights = new double[] { 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
    mainPane.setLayout(gbl_mainPane);

    tabbedPane = new JTabbedPane(JTabbedPane.TOP);
    GridBagConstraints gbc_tabbedPane = new GridBagConstraints();
    gbc_tabbedPane.gridwidth = 7;
    gbc_tabbedPane.gridheight = 9;
    gbc_tabbedPane.fill = GridBagConstraints.BOTH;
    gbc_tabbedPane.gridx = 0;
    gbc_tabbedPane.gridy = 0;
    mainPane.add(tabbedPane, gbc_tabbedPane);

    tabbedPanel = new JPanel();
    tabbedPane.addTab("Volume", null, tabbedPanel, null);
    GridBagLayout gbl_tabbedPanel = new GridBagLayout();
    gbl_tabbedPanel.columnWidths = new int[] { 86, 86, 86, 73, 73, -30, 140, 120, 0 };
    gbl_tabbedPanel.rowHeights = new int[] { 249, 28, 35, 10, 3, 3, 23, 0, 0 };
    gbl_tabbedPanel.columnWeights = new double[] { 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
    gbl_tabbedPanel.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
    tabbedPanel.setLayout(gbl_tabbedPanel);

    JScrollPane scrollPane = new JScrollPane();
    GridBagConstraints gbc_scrollPane = new GridBagConstraints();
    gbc_scrollPane.fill = GridBagConstraints.BOTH;
    gbc_scrollPane.insets = new Insets(0, 0, 5, 0);
    gbc_scrollPane.gridwidth = 8;
    gbc_scrollPane.gridx = 0;
    gbc_scrollPane.gridy = 0;
    tabbedPanel.add(scrollPane, gbc_scrollPane);

    displayTable = new JTable();
    displayTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    displayTable.setShowVerticalLines(false);
    displayTable.setShowHorizontalLines(false);
    displayTable.getTableHeader().setReorderingAllowed(false);
    displayTable.setModel(
            new DefaultTableModel(new Object[][] {}, new String[] { "1", "2", "3", "4", "5", "6", }) {
                Class[] columnTypes = new Class[] { Object.class, String.class, Integer.class, Integer.class,
                        String.class, String.class, String.class, Integer.class };
            });

    displayTable.getColumnModel().getColumn(0).setMinWidth(100);
    displayTable.getColumnModel().getColumn(1).setMinWidth(20);
    scrollPane.setViewportView(displayTable);
    displayModel = (DefaultTableModel) displayTable.getModel();
    scrollPane.setViewportView(displayTable);
    displayModel = (DefaultTableModel) displayTable.getModel();

    displayButton = new JButton("GO");
    GridBagConstraints gbc_displayButton = new GridBagConstraints();
    gbc_displayButton.gridwidth = 2;
    gbc_displayButton.anchor = GridBagConstraints.NORTH;
    gbc_displayButton.fill = GridBagConstraints.HORIZONTAL;
    gbc_displayButton.insets = new Insets(0, 0, 5, 0);
    gbc_displayButton.gridx = 6;
    gbc_displayButton.gridy = 2;
    tabbedPanel.add(displayButton, gbc_displayButton);
    displayButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {

            Thread populate = new Thread(new PopulateDisplay());
            populate.start();
            displayButton.setEnabled(false);

        }
    });

    comboBox = new JComboBox<String>();
    GridBagConstraints gbc_comboBox = new GridBagConstraints();
    gbc_comboBox.insets = new Insets(0, 0, 5, 5);
    gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
    gbc_comboBox.gridx = 1;
    gbc_comboBox.gridy = 4;
    tabbedPanel.add(comboBox, gbc_comboBox);

    comboBox.addItem("Test 1");
    comboBox.addItem("Test 2");
    comboBox.addItem("Test3");


    }
}






class PopulateDisplay extends test implements Runnable {

public void run() {

    try {
        getData(comboBox.getSelectedItem().toString());
        lookup();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private void getData(String sector) throws UnsupportedEncodingException, IOException {
    //DOES SOMETHING
    }


private void lookup() throws IOException {

    //added here
     displayModel.addRow(new Object[] { "", "" , ""});


     }

  }

最佳答案

您的PopulateDisplay类扩展了test。因此,PopulateDisplay实例是一个test,当您创建一个PopulateDisplay时,您正在创建一个新的test,因此您将创建一个全新的JFrame并显示它。

PopulateDisplay不应扩展test

您还应该遵守Java命名约定:类以大写字母开头。

10-06 02:15