Dispose_on_Close关闭JFrame,但它仍处于活动状态

时间:2019-02-23 03:02:57

标签: java swing jframe jtable

我正在编写一个程序,如果单击一个按钮,我需要在其中打开JFrame。我将默认的关闭操作设置为Dispose_on_close,以便在关闭窗口时程序不会完全关闭。 在要打开的框架中,我想放置一个JTable,所以我编写了两个方法,一个createFrame()方法和一个MechanicListTableProperties(),后者是创建JTable并向其中添加元素的方法。然后,我在createFrame()内部调用MechanicListTableProperties,在actionPerformed()方法内部调用createFrame。当我打开框架1次时,该表显示在窗口内,但是如果我关闭并重新打开框架,该表也将被读取,并且当我尝试再次看到一个表时,我会看到2个表。这是我的源代码:

public class SeeMechanicsButtonHandler implements ActionListener {      
    JFrame mechanicListFrame;
    boolean isOpen = false;     
    JTable mechanicListTable;   
    JPanel tablePanel = new JPanel(new GridLayout()); 
    JScrollPane sp;     
    List<String> names = new ArrayList<String>();   
    String[] namesArray;

     public void createFrame() {        
        mechanicListFrame = new JFrame();
        mechanicListFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);       
        mechanicListFrame.setSize(new Dimension(500,500));      
        mechanicListFrame.add(tablePanel);
        mechanicListFrame.setVisible(true);                 
        //Prevents the window from being opened multiple times  
        mechanicListFrame.addWindowListener(new WindowAdapter(){            
            public void windowClosing(WindowEvent e) {              
                isOpen = false;                 
            }       
        });          
    }

     public void mechanicListTableProperties(){
        mechanicListTable = new JTable(){
        public boolean isCellEditable(int row, int column) {
            return false;           
            }       
        };      
        DefaultTableModel model = new DefaultTableModel();  
        model.addColumn("Nome", namesArray); 
        //Creates a column with title as Nome and lines as the array    

        mechanicListTable.setModel(model); //adds column to the the table
        mechanicListTable.setBounds(30, 40, 200, 300); //table size

        mechanicListTable.setFont(new Font("Arial Rounded MT", Font.BOLD, 15)); 
                        // adding it to JScrollPane
        sp = new JScrollPane(mechanicListTable);    
        tablePanel.add(sp);     
    }

public void actionPerformed(ActionEvent e) {    
    if(!isOpen) {       
        try {
             //SQL code to get the data from mechanics table
            ResultSet rs = ServerConnection.createQueryStatement("SELECT * FROM mechanics");
            while (rs.next()){
                //loop to add each entry in the table to an array list
                names.add(rs.getString("nome"));
                }
                //creates an array to put the values from the arraylist
                namesArray = new String[names.size()];
                for (int iterate = 0; iterate < names.size(); iterate++){
                    //loop that iterates through the arraylist and puts the values in the array
                    namesArray[iterate] = names.get(iterate);

                    System.out.println(namesArray[iterate]);
                     //prints to the console for testing purposes
                }
            } catch (SQLException e1) {
            e1.printStackTrace();
            }
        createFrame();
        isOpen = true;
     }
    }
}

0 个答案:

没有答案