所以,让我们从一些背景开始,这是第一年的拼贴项目。 GUI构建为一个类,接口处理程序调用所有JPanel并将它们放在正确的位置。这是为了结构,因此易于维护。
问题是拿着菜单的面板在添加之后不会显示它的组件。我会包含一些图片,以便更容易理解。
相关面板名为MenuPanel。
所以这里是登录按钮,目前只清除内容面板,然后添加菜单面板。
Login page where the button is located
这里是菜单面板空
here is the menu panel all empty
代码 接口处理程序 你可以看到我已经在菜单方法中使用.repaint()方法搞乱了。 公共类InterfaceHandler {
private static InterfaceHandler singleton;
/**
* Checks if there is already a instance running of InterfaceHandler if so
* return the instance if not create a instance.
*
* @return Instance
*/
public static InterfaceHandler instance() {
if (singleton == null) {
singleton = new InterfaceHandler();
}
return singleton;
}
private ContentPanel contentPanel;
private LoginPanel loginPanel;
private MainFrame mainFrame;
private MainPanel mainPanel;
private MenuPanel menuPanel;
/**
* Initializes default Interface
*/
public InterfaceHandler() {
initComponents();
}
/**
* Initializes GUI Components that will be needed now or in the future.
*/
public void initComponents() {
mainFrame = new MainFrame();
mainPanel = new MainPanel();
contentPanel = new ContentPanel();
loginPanel = new LoginPanel();
menuPanel = new MenuPanel();
contentPanel.setBounds(100, 100, 860, 700);
menuPanel.setBounds(10, 10, 80, 400);
loginPanel.setBounds(280, 250, 300, 200);
mainFrame.add(mainPanel);
mainPanel.add(contentPanel);
mainFrame.setVisible(true);
contentPanel.add(loginPanel);
}
/**
* Clears the ContentPanel of all components
*/
public void clear() {
System.out.println("Clear Ran");
contentPanel.removeAll();
contentPanel.repaint();
}
/**
* Adds the Login Screen.
*/
public void login() {
System.out.println("Login Ran");
contentPanel.add(loginPanel);
contentPanel.repaint();
}
/**
* Adds the menu.
*/
public void menu() {
System.out.println("Menu Ran");
mainPanel.sidePanel.add(menuPanel);
menuPanel.repaint();
mainPanel.repaint();
mainPanel.sidePanel.repaint();
}
}
以下是面板的代码,其中大部分是由netbeans设计编辑器自动生成的。 所以它很难看(对不起)
公共类MenuPanel扩展了javax.swing.JPanel {
/**
* Creates new form MenuPanel
*/
public MenuPanel() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jSeparator1 = new javax.swing.JSeparator();
jLabel2 = new javax.swing.JLabel();
baggageButton = new javax.swing.JLabel();
jLabel1.setBackground(new java.awt.Color(204, 204, 204));
jLabel1.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("Menu");
jSeparator1.setBackground(new java.awt.Color(204, 204, 204));
baggageButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
baggageButton.setText("Baggage");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jSeparator1)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(baggageButton, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, 0)
.addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 4, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(baggageButton, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JLabel baggageButton;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JSeparator jSeparator1;
// End of variables declaration
}
只是为了澄清,方法菜单是从另一个类调用的。
答案 0 :(得分:6)
如果要向已经可见的面板添加组件,则需要调用validate()
。
public void menu() {
System.out.println("Menu Ran");
mainPanel.sidePanel.add(menuPanel);
mainPanel.sidePanel.validate(); // try this
menuPanel.repaint();
mainPanel.repaint();
mainPanel.sidePanel.repaint();
}
答案 1 :(得分:0)
(这个答案是错误的,但随后的评论确实有助于理解为什么@ whiskeyspider的答案是正确的)
在此代码中:
mainFrame.add(mainPanel);
mainPanel.add(contentPanel);
mainFrame.setVisible(true);
contentPanel.add(loginPanel);
您未在任何地方添加menuPanel
。它在menu()
方法中添加(太晚),但未被调用。