3

我有一个 JMenu,当有人单击 JMenuItem 时,我希望中间面板刷新并显示一些新内容。所以我尝试了 .removeAll ,它工作正常,但是当我尝试添加它不会显示的东西时。

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

这是我的代码:

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);

 }
}
4

2 回答 2

3

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

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

于 2012-08-27T21:47:51.227 回答
1

问题在于,因为您已将框架的布局设置为 null,所以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 JPanel would fix this.

于 2012-08-27T22:12:44.370 回答