I fixed this by setting the main layout also to setLayout(null), I just have to use this for this project...
Next time when I have to write it from scratch I won't use it! Anyway, thanks for helping, I would have never found the problem.
Pergunta
I've created an app but I have a problem with my JLabels not showing up properly. The app currently looks like this:
These are 2 JPanels inside a JFrame created with the following code:
public JFrame window = new JFrame();
public JPanel top = new JPanel();
public static JPanel main = new JPanel();
public JPanel login = new JPanel();
// ...
Inside the main class:
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);
That will show up the frames as I have them now. But above the black line there should be a title that would be created by this:
public class CategoryList extends JPanel implements MouseListener {
public CategoryList() {
super();
setLayout(null);
initComponents();
revalidate();
repaint();
addTitle();
}
}
The title is created like this:
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");
}
But it does not show up. I know there are a lot of methods not shown in this code, but I have included what I think is all the necessary code.
I hope someone can help me, thanks in advance!
Edit:
I've stripped my code so it can be online: main package: 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 class: (Just a textfield created with the GUI Designer in 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
}
CategoryList class (Here the text should be above the line, but that does not show up)
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);
}
}
Hope somebody can help me now. Thanks in advance again.
Solução 2
I fixed this by setting the main layout also to setLayout(null), I just have to use this for this project...
Next time when I have to write it from scratch I won't use it! Anyway, thanks for helping, I would have never found the problem.
Outras dicas
A possible simple solution:
Your main application JPanel should use a BorderLayout. Place the title JLabel in the BorderLayout.PAGE_STATE (also known as BorderLayout.NORTH), and place the rest of your GUI in the BorderLayout.CENTER position.