我创建了一个应用程序,但是我的JLabels出现问题,显示不正常。该应用程序目前看起来是这样的:JLabels(和其他东西)不会显示在JFrame中的JPanel内部(多个JFrames)
这些都是用下面的代码创建一个JFrame内2个JPanels:
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类:(只用GUI设计器创建一个文本框在Netbeans的)
/*
* 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
}
所属分类类(这里的文字应该是线以上,但不显示)
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);
}
}
希望有人能帮助我。再次感谢您。
您的代码有点碎片化和不完整,使我们很难完全理解。我们不想看到所有的代码,只是显示你的问题,完全可以编译和运行,并且没有与问题无关的代码。最好创建并发布我们可以运行的[sscce](http://sscce.org),并显示您的问题。另外,避免使用空布局的习惯。你几乎不会*想要使用它。 –
@Hovercraft Full Of Eels好的,谢谢你的回答。我得到了代码,我必须使它变得更好,更大,我上传了源代码(只需要任何人需要的部分)即可:[link](http://www.mediafire.com/?xr51ia7z016yxul)。所以你可以尝试一下,看看代码,在这里粘贴,这将是很多classess要做,并阅读......这就是为什么我上传它。我希望有人能帮助我。 – Marc
Marc:没有人想上传源代码。请重新阅读我的帖子,并查看我上面评论中的链接。它会完全告诉你我们需要什么。明白这将需要你的努力。所以,如果你很快就没有得到体面的答案,考虑加入这项工作,因为它可能是值得的。运气。 –