gpt4 book ai didi

java - 标签(和其他东西)不会出现在 JFrame(多个 IFrame)的 JPanel 中

转载 作者:行者123 更新时间:2023-12-04 05:20:10 25 4
gpt4 key购买 nike

我已经创建了一个应用程序,但我的 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 设计器创建的文本字段)
  /*
* 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);
}
}

希望现在有人可以帮助我。再次提前致谢。

最佳答案

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

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

关于java - 标签(和其他东西)不会出现在 JFrame(多个 IFrame)的 JPanel 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13778043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com