java Java问题如何刷新面板

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12149969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:47:50  来源:igfitidea点击:

Java issue How to refresh a panel

javaswinguser-interface

提问by Timothy Montanez

Hey guys I'm a huge noob at Java xD, got sorta of a issue, basically I have a JMenu and when a person clicks on a JMenuItem I want the middle panel to refresh and display some new stuff. So I tried the .removeAll which works fine but when I try to add something it wont show.

嘿伙计们,我是 Java xD 的一个大菜鸟,遇到了一个问题,基本上我有一个 JMenu,当有人单击 JMenuItem 时,我希望中间面板刷新并显示一些新内容。所以我尝试了 .removeAll ,它工作正常,但是当我尝试添加一些东西时它不会显示。

Note: I'm using the WindowBuilder PRO, so I still trying to get use to it and whatnot

注意:我使用的是 WindowBuilder PRO,所以我仍在尝试使用它等等

Here's my code :]

这是我的代码:]

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;

import java.awt.List;
import javax.swing.JLabel;
import javax.swing.JTextPane;
import javax.swing.JSeparator;
import org.eclipse.wb.swing.FocusTraversalOnArray;
import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Panel;
import java.awt.GridBagConstraints;


public class HomeScreen {

JFrame frame;
private final Panel panel = new Panel();

public HomeScreen(String name) {
    initialize(name);
}

/**
 * Initialize the contents of the frame.
 */
private void initialize(String name) {
    frame = new JFrame("Timzys CMS / Monitor / Account( " + name + " )");
    frame.setResizable(false);
    frame.setSize(694,525);
    frame.setLocationRelativeTo(null);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JMenuBar menuBar = new JMenuBar();
    frame.setJMenuBar(menuBar);

    JMenu mnMonitor = new JMenu("Monitor");
    menuBar.add(mnMonitor);

    JMenuItem mntmUsers = new JMenuItem("Users");
    mnMonitor.add(mntmUsers);

    JMenuItem mntmContentPosts = new JMenuItem("Content Posts");
    mnMonitor.add(mntmContentPosts);

    JMenuItem mntmLogs = new JMenuItem("Logs");
    mnMonitor.add(mntmLogs);

    JMenu mnExtras = new JMenu("Extras");
    menuBar.add(mnExtras);

    mntmUsers.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e)
        {
             frame.getContentPane().removeAll();

             frame.getContentPane().add(new JLabel("Test"));
             frame.getContentPane().revalidate();

        }
    });


    JMenuItem mntmFeedbacksuggestions = new JMenuItem("Feedback/Suggestions");
    mnExtras.add(mntmFeedbacksuggestions);
    frame.getContentPane().setLayout(null);
    panel.setBounds(0, 0, 688, 476);
    frame.getContentPane().add(panel);
    panel.setLayout(null);

    JTextPane txtpnWelcomeTimzysCms = new JTextPane();
    txtpnWelcomeTimzysCms.setFont(new Font("Arial", Font.PLAIN, 18));
    txtpnWelcomeTimzysCms.setEditable(false);
    txtpnWelcomeTimzysCms.setText("Welcome Timzys CMS Monitor. If you are here then you are a admin! So please do not tamper with any important things. If you have questions or suggestions goto the extras tab and submit a feedback idea. Enjoy!");
    txtpnWelcomeTimzysCms.setBounds(10, 11, 668, 72);
    panel.add(txtpnWelcomeTimzysCms);

 }
}

采纳答案by Reimeus

The problem is that because you have set the frame's layout to null, the new JLabelthat you add will need to have its size set. e.g.:

问题是因为您已将框架的布局设置为空,所以JLabel您添加的新元素需要设置其大小。例如:

JLabel label = new JLabel("Test");
label.setSize(100, 100);
frame.getContentPane().add(label);

As an aside:Actually I wonder why you are mixing AWT & Swing components here. You have a heavyweight AWT panel added to the frame which blocks out the menus. Switching to JPanelwould fix this.

顺便说一句:实际上我想知道您为什么在这里混合 AWT 和 Swing 组件。您有一个重量级的 AWT 面板添加到框架中,它挡住了菜单。切换到JPanel将解决此问题。

回答by Peter Butkovic

Probably you're facing similar problem as someone else in the question:
Java Swing revalidate() vs repaint()

可能您正面临与问题中的其他人类似的问题:
Java Swing revalidate() vs repaint()

As suggested: try to call repaintinstead of revalidate(the one you're calling right now in your implementation of method actionPerformed)

按照建议:尝试调用repaint而不是revalidate(您现在在方法实现中调用的那个actionPerformed