-2

我在尝试使用按钮更改 JPanel 时遇到问题。我有一个带有 2 个面板的 JFrame,其中 1 个用于按钮,我希望它们始终显示。另一个是我每次按下另一个面板的按钮时都会切换的那个。问题是每次我按下它们时都没有真正显示,我保留我的按钮,但我调用的另一个面板没有出现。

其中一个按钮的代码如下

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        ReparacaoPanel r = new ReparacaoPanel(this, this.jPanel1);
        this.getContentPane().remove(this.jPanel1);
        this.getContentPane().add(r);
        //this.setContentPane(r);
        this.visiblePanel.setVisible(false);
        this.visiblePanel = r;
        this.pack();
        this.setVisible(true);

        r.setLocation(200, 200);
        this.getContentPane().revalidate();
        this.repaint();
    }

如果我尝试使用“this.setContentPane(r);” (它将框架设置为仅显示面板)面板显示。但是,当我尝试在上面的代码中尝试调用它时,除了具有按钮的面板之外,什么都没有显示。

我不知道我做错了什么,这似乎不是我试图调用的 JPanel 的问题,因为如果单独使用它会显示。

任何人都可以帮助我吗?

4

1 回答 1

2

考虑这个在面板之间手动切换的工作示例。产生这个输出。

在此处输入图像描述 在此处输入图像描述…………

一些微小的NumberPanel

每个新实例在中心显示另一个数字。

import javax.swing.JPanel;
public class NumberPanel extends JPanel {
    private static int counter = 0;
    public NumberPanel() {
        setLayout(new BorderLayout(0, 0));
        JLabel lblNewLabel = new JLabel("" + counter++);
        lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
        add(lblNewLabel);
    }
}

设置框架

private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel();
    frame.getContentPane().add(panel, BorderLayout.SOUTH);

    JButton btnNewButton = new JButton("New button");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            frame.getContentPane().remove(numberPanel);
            numberPanel = new NumberPanel();
            frame.getContentPane().add(numberPanel, BorderLayout.CENTER);
            frame.pack();
        }
    });
    panel.add(btnNewButton);

    numberPanel = new NumberPanel();
    frame.getContentPane().add(numberPanel, BorderLayout.CENTER);
    frame.pack();
}

测试程序

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TestPanelSwitch {
    private JFrame frame;
    private NumberPanel numberPanel;
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TestPanelSwitch window = new TestPanelSwitch();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    public TestPanelSwitch() {
        initialize();
    }
    private void initialize() {
        // see above
    }
}

回到问题

我认为你只需要pack你的框架,就像在匿名中一样ActionListener

frame.getContentPane().remove(numberPanel);
numberPanel = new NumberPanel();
frame.getContentPane().add(numberPanel, BorderLayout.CENTER);
frame.pack();

编辑

正如 leonidas 提到的,也可以重新验证frame. 这只需要pack由theese替换上层调用。

frame.invalidate();
frame.validate();
于 2014-06-22T10:38:03.680 回答