JPanel不会出现在JFrame中

时间:2016-12-20 12:25:16

标签: java swing

为什么单击按钮后我的JPanel不会显示在JFrame中。 这是我的代码:

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JList;

import javax.swing.DefaultListModel;
import javax.swing.AbstractListModel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;

public class Main {

    private JFrame frame;
    private JTable table;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Main window = new Main();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Main() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setResizable(false);
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        table = new JTable();
        table.setModel(new DefaultTableModel(
            new Object[][] {
                {"1", "Marcin Zelek", "537573656"},
                {"2", "Krzysztof Tomala", "324159103"},
                {"3", "Zbigniew S", "324159104"},
            },
            new String[] {
                "#", "Name", "Phone number"
            }
        ));
        table.getColumnModel().getColumn(1).setPreferredWidth(214);
        table.getColumnModel().getColumn(2).setPreferredWidth(246);
        table.setBounds(12, 103, 426, 185);
        frame.getContentPane().add(table);

        JButton btnDodajNowy = new JButton("Dodaj nowy");
        btnDodajNowy.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent arg0) {
                JPanel panel = new JPanel(null);// Creating the JPanel
                panel.setBounds(0, 243, 286, 150);
                panel.setVisible(true);

                JButton button = new JButton("New button");
                button.setBounds(12, 12, 117, 25);
                panel.add(button);

                frame.getContentPane().add(panel);
            }
        });
        btnDodajNowy.setBounds(12, 30, 117, 25);
        frame.getContentPane().add(btnDodajNowy);

        JButton btnUsuZaznaczone = new JButton("Usuń zaznaczone");
        btnUsuZaznaczone.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent arg0) {
                DefaultTableModel model = (DefaultTableModel) table.getModel();
                int[] selection = table.getSelectedRows();
                for (int i = 0; i < selection.length; i++)
                {
                    model.removeRow(selection[i]-i);
                }
            }
        });
        btnUsuZaznaczone.setBounds(141, 30, 204, 25);
        frame.getContentPane().add(btnUsuZaznaczone);

    }
}

感谢。

3 个答案:

答案 0 :(得分:0)

您应该使用actionListener:

btnDodajNowy.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        JPanel panel = new JPanel();// Creating the JPanel
        panel.setBounds(0, 243, 286, 150);
        JButton button = new JButton("New button");
        button.setBounds(12, 12, 117, 25);
        panel.add(button);
        frame.getContentPane().add(panel);
        frame.repaint();
    }
});

还有:

  1. 您不需要将null作为参数传递给JPanel构造函数
  2. 您可能想要添加frame.repaint()
  3. 您无需通过JPanelsetVisible()设置为可见。

答案 1 :(得分:0)

它被添加,然而,它隐藏在JTable之下。最快的解决方法就是:

public void mouseReleased(MouseEvent arg0) {
    //...
    panel.setBounds(0, 300, 286, 150); // 300 is on the y axis
    //...
    // then render the frame again:
    frame.revalidate();
    frame.repaint();
}

但是,我建议稍微改写一下:

this.generalPanel = new JPanel();
frame.getContentPane().add(generalPanel);
...
generalPanel.add(table);

通过这种方式,您可以获得最佳容器的良好布局。

答案 2 :(得分:0)

如果您希望在添加按钮的同一jframe中显示该面板,您可能应该使用CardLayout这里是链接http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html