JLabels(以及其他东西)不会出现在JFrame(多个JFrame)中的JPanel中

时间:2012-12-08 13:48:12

标签: java swing jframe jpanel null-layout-manager

我已经创建了一个应用程序,但是我的JLabel没有正常显示问题。该应用目前看起来像这样:

app screenshot

这些是使用以下代码创建的JFrame中的2个JPanel:

public JFrame window = new JFrame();
public JPanel top = new JPanel();
public static JPanel main = new JPanel();
public JPanel login = new JPanel();

// ...

主要课程内:

window.setSize(1000, 700);
login.setSize(250, 200);
//   main.setSize(500,500);
main.setLocation(500,100);

window.add(login);
window.add(main);

login.add(new view.LoginPanel());

main.setLayout(new BorderLayout());
main.add(new view.CategoryList(), BorderLayout.CENTER);
main.validate();
main.repaint();

window.validate();
window.setVisible(true);

这将显示我现在拥有的帧。但是在黑线之上应该有一个由此创建的标题:

public class CategoryList extends JPanel implements MouseListener {
    public CategoryList() {
        super();
        setLayout(null);
        initComponents();
        revalidate();
        repaint();
        addTitle();
    }
}

标题是这样创建的:

private void addTitle() {
    JLabel lblTitle = new JLabel();
    lblTitle.setText("Winkelapplicatie");
    lblTitle.setBounds(20, 20, 150, 20);
    lblTitle.setFont(WinkelApplication.FONT_16_BOLD);
    this.add(lblTitle);
    System.out.println("addTitle");
}

但它没有出现。我知道这个代码中没有显示很多方法,但我已经包含了我认为所有必需的代码。

我希望有人可以帮助我,提前谢谢!

编辑:

我删除了我的代码,以便它可以在线: 主要包装:     WinkelApplication.java:

package main;


import java.awt.BorderLayout;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;



public final class WinkelApplication {

public static final String NAME = "hi";
public static final String CURRENCY = "\u20AC";
public JFrame window = new JFrame();
public JPanel top = new JPanel();
public static JPanel main = new JPanel();
public JPanel login = new JPanel();

public static final Font FONT_10_PLAIN = new Font("Verdana", Font.PLAIN, 10);
public static final Font FONT_10_BOLD = new Font("Verdana", Font.BOLD, 10);
public static final Font FONT_12_BOLD = new Font("Verdana", Font.BOLD, 12);
public static final Font FONT_16_BOLD = new Font("Verdana", Font.BOLD, 16);

private static WinkelApplication instance = new WinkelApplication();

private WinkelApplication() {
}

public void initialize() {
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
        System.err.println("Error setting LookAndFeelClassName: " + e);
    }

}

public void startup() {
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setSize(1000, 700);
    login.setSize(250, 200);
     main.setLocation(500,100);
    window.add(login);
    window.add(main);
    login.add(new main.LoginPanel());  
     main.setLayout(new BorderLayout());
    main.add(new main.CategoryList(), BorderLayout.CENTER);
    main.validate();
    main.repaint();
    window.validate();
    window.setVisible(true);


}

public static WinkelApplication getInstance() {
    return instance;
}

public static void main(String args[]) {
    final WinkelApplication applicatie = WinkelApplication.getInstance();
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                applicatie.initialize();
                applicatie.startup();
            } catch (Exception e) {
                System.out.println("Application" + applicatie.getClass().getName() + "failed to launch");
            }
        }
    });
}
}

LoginPanel类:(只是在Netbeans中使用GUI Designer创建的文本字段)

  /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import main.WinkelApplication;

public class LoginPanel extends javax.swing.JPanel {

/**
 * Creates new form LoginPanel
 */
public LoginPanel() {
    initComponents();
}

private Boolean loggedIn = null;

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    javax.swing.JLabel jLabel1 = new javax.swing.JLabel();

    jLabel1.setText("TextField in JPanel 1");

    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()
            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(114, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jLabel1)
            .addContainerGap(98, Short.MAX_VALUE))
    );
}// </editor-fold>                        

public Boolean isLoggedIn(){
    return loggedIn;
}
// Variables declaration - do not modify                     
// End of variables declaration                   
}

CategoryList类(此处文本应位于行上方,但不会显示)

package main;

import main.WinkelApplication;
import java.awt.Graphics;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class CategoryList extends JPanel {

public CategoryList() {
    super();
    setLayout(null);
    initComponents();
}

/** create the gui for this page */
private void initComponents() {
    addTitle();
}

/** add the page's title */
private void addTitle() {
    JLabel lblTitle = new JLabel();
    lblTitle.setText("Winkelapplicatie");
    lblTitle.setBounds(20, 20, 150, 20);
    lblTitle.setFont(WinkelApplication.FONT_16_BOLD);
    this.add(lblTitle);
    System.out.println("addTitle");
}

@Override
public void paint(Graphics graphics) {
    super.paint(graphics);
    graphics.drawLine(20, 45, 540, 45);
}
}

希望有人能帮助我。再次感谢。

2 个答案:

答案 0 :(得分:2)

一种可能的简单解决方案:

您的主应用程序JPanel应该使用BorderLayout。将标题JLabel放在BorderLayout.PAGE_STATE(也称为BorderLayout.NORTH)中,并将GUI的其余部分放在BorderLayout.CENTER位置。

答案 1 :(得分:1)

我通过将主布局设置为setLayout(null)来修复此问题,我只需要将此项用于此项目......

下次当我必须从头开始编写时,我不会使用它! 无论如何,谢谢你的帮助,我从来没有发现过这个问题。