当前位置: 首页 > 知识库问答 >
问题:

用JFrame中的另一个JPanel替换当前显示的JPanel

施刚毅
2023-03-14

一个jFrame:HomeView、一个jPanel:TopicListView、另一个jPanel:ReplyListView。

在HomeView中,我有一个菜单项,可以单击它来显示TopicListView。在TopicListView中,我希望有一个可以单击以显示ReplyListView的按钮。单击按钮时,它将调用openReplyListView()方法。该方法将创建一个新的JPanel并用它替换当前的JPanel。但是,openReplyListView()中的代码不起作用。

注意:我没有使用卡片布局。

public class HomeView extends JFrame {

private JPanel contentPane;
private JMenuBar menuBar;
private JMenu mnForum;
private JMenuItem mntmReply;
private JMenuItem mntmTopic;

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

/**
 * Create the frame.
 */
public HomeView() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 1280, 720);
    setJMenuBar(getMenuBar_1());
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);
}

private JMenuBar getMenuBar_1() {
    if (menuBar == null) {
        menuBar = new JMenuBar();
        menuBar.add(getMnForum());
    }
    return menuBar;
}
private JMenu getMnForum() {
    if (mnForum == null) {
        mnForum = new JMenu("Forum");
        mnForum.add(getMntmTopic());
        mnForum.add(getMntmReply());
    }
    return mnForum;
}
private JMenuItem getMntmReply() {
    if (mntmReply == null) {
        mntmReply = new JMenuItem("Reply");
        mntmReply.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                JPanel panel = new ReplyView();
                getContentPane().removeAll();
                getContentPane().add(panel);
                getContentPane().validate();
                getContentPane().repaint();
            }
        });
    }
    return mntmReply;
}


private JMenuItem getMntmTopic() {
    if (mntmTopic == null) {
        mntmTopic = new JMenuItem("Topic");
        mntmTopic.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                JPanel panel = new TopicListView();
                getContentPane().removeAll();
                getContentPane().add(panel);
                getContentPane().validate();
                getContentPane().repaint();
            }
        });
    }
    return mntmTopic;
}
}

TopicListView类:

public class TopicListView extends JPanel {
private JTable table;
private JScrollPane scrollPane;
private JLabel lblTopics;


/**
 * Create the panel.
 */
public TopicListView() {
    setLayout(null);
    add(getTable());
    add(getScrollPane());
    add(getLblTopics());
}

**Code snippet for the table and the scrollpane:**


private void openReplyListView(){
    JPanel panel = new ReplyListView();
    getContentPane().removeAll();
    getContentPane().add(panel);
    getContentPane().validate();
    getContentPane().repaint();

}

ReplyListView类(通常与TopicListView相同)

public class ReplyListView extends JPanel {
private JTable table;
private JScrollPane scrollPane;
private JLabel lblTest;

/**
 * Create the panel.
 */
public ReplyListView() {
    setLayout(null);
    add(getTable());
    add(getScrollPane());
    add(getLblTest());

}
private JTable getTable() {
    if (table == null) {
        table = new JTable();
        table.setBounds(414, 114, 464, 354);
    }
    return table;
}
private JScrollPane getScrollPane() {
    if (scrollPane == null) {
        scrollPane = new JScrollPane(getTable());
        scrollPane.setBounds(414, 114, 464, 349);
    }
    return scrollPane;
}
private JLabel getLblTest() {
    if (lblTest == null) {
        lblTest = new JLabel("TEST");
        lblTest.setFont(new Font("Tahoma", Font.PLAIN, 30));
        lblTest.setBounds(593, 35, 81, 68);
    }
    return lblTest;
}
}

共有1个答案

薛保臣
2023-03-14

您的TopicListView没有LayoutManger(即构造函数中的SetLayout(null))。

这通常是个坏主意。(我很确定这就是你问题的原因)

试试这样的东西

private void openReplyListView(){
    JPanel panel = new ReplyListView();
    removeAll();
    setLayout(new BorderLayout());
    add(panel, BorderLayout.CENTER);
    validate();
    repaint();
}
 类似资料:
  • 我有一个JFrame,我使用它作为主要的JFrame,并在同一个JFrame中更改JPanels。第一个JPanel已经在JFrame中,所以我可以用“this.removeAll()then,this.setContentPane(new JPanel2)”来替换它,因为这里的“this”调用JFrame本身。 在另一个JPanel中,我希望创建一个JButton,用另一个JPanel替换当前的

  • 我有一个重新组合一些JPanel的主框架。我的JFrame已经完全填满了。 我希望能够在左侧JFrame中的另一个JPanel上显示/隐藏一个小JPanel。此JPanel是用户的配置区域。 所以这里是我的问题,在我的JFrame中,在一个小区域中显示JPanel的最好方式是什么? 我试过了,但没有按预期工作(这是单击设置图标时执行的代码): 谢谢

  • 我想在一个JFrame中用另一个Jpanel替换一个Jpanel,我已经搜索并尝试了我的代码,但什么也没有发生,这是我的代码: 有人能帮帮我吗?多谢

  • 我为欢迎屏幕设计了一个接口,其中有一个JFrame,包括两个JPanel(JPanel1在右边,JPanel2在左边)。左边的按钮用于切换JPanel1中的面板。我想按下一个按钮,用另一个JPanel替换JPanel1内容,但我不知道怎么做。请帮忙。

  • 我知道同样的问题已经被问过很多次了,但是我似乎真的没有在我的代码中发现阻碍JPanel类型的对象显示在JFrame中的错误。下面是扩展JFrame的类的构造函数: 当我运行main方法(这里没有显示)时,它只显示框架和按钮。如果有人能在这方面给点提示,我会非常感谢的。

  • 问题内容: 不幸的是,我必须在该程序中使用多个窗口,而且我认为CardLayout不能正常工作,因为在不同布局之间我无法保持任何按钮不变。因此,我尝试编写一个按钮以隐藏当前的JPanel(thePanel)并显示一个新的JPanel(thePlacebo)。 我试图在这样的ActionListener中隐藏thePanel: 我以为这可以用,但是只要按一下按钮,它就会冻结程序。 这是上下文代码的一