Reputation: 73
I'm now creating a simple program with Frame. I, however, face one unknown error that any contents do not appear ,except Panel sized 90 * 90 on the frame. when I adjust windows size by dragging , then the contents appear as I expected. I've tired few methods by googling.. all the same.. not working .
For making this clear, I will attach few screenshots.
Java code :
import java.awt.Color;
import java.awt.Container;
import javax.swing.*;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
class menuListener implements MenuListener {
@Override
public void menuSelected(MenuEvent e) {
// TODO Auto-generated method stub
System.out.println("menu clicked !");
System.exit(0);
}
@Override
public void menuDeselected(MenuEvent e) {
// TODO Auto-generated method stub
}
@Override
public void menuCanceled(MenuEvent e) {
// TODO Auto-generated method stub
}
}
public class pcMain extends JFrame implements Runnable {
userSeat us;
int user;
JPanel[] userSeat;
Container con;
JPanel leftPane, rightPane;
JMenuBar jm;
JMenu exit;
public pcMain(int user) {
super("Pc Management Application");
con = getContentPane();
con.setBackground(new Color(0, 128, 128));
setVisible(true);
setSize(1100, 500);
// setLayout(null);
this.setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Menu bar
jm = new JMenuBar();
// Menu items
exit = new JMenu("EXIT");
exit.addMenuListener(new menuListener());
// letPanel
leftPane = new JPanel();
leftPane.setLayout(null);
// leftPane.setSize(800, 470);
leftPane.setBorder(BorderFactory.createLineBorder(Color.black));
leftPane.setOpaque(false);
leftPane.setBounds(0, 0, 800, 500);
// rightPanel
rightPane = new JPanel();
// rightPane.setSize(300, 470);
rightPane.setBorder(BorderFactory.createLineBorder(Color.red));
rightPane.setOpaque(false);
rightPane.setBounds(800, 0, 300, 500);
// adding all components
this.user = user;
jm.add(exit);
us = new userSeat(user);
userSeat = us.getSeat();
setJMenuBar(jm);
// add(leftPane);
// add(rightPane);
}
@Override
public void run() {
int seatX = 10;
int seatY = 10;
// Add user seats into leftPane
for (int i = 0; i < user; i++) {
if (i != 0 && i % 5 == 0) {
// Increase y value
System.out.println("if : " + i);
seatX = 10;
seatY += 100;
}
// Increase x values
userSeat[i].setLocation(seatX, seatY);
leftPane.add(userSeat[i]);
// add(leftPane);
System.out.println("else :" + i);
seatX += 95;
}
add(leftPane);
}
public static void main(String[] args) {
new Thread(new pcMain(10)).start();
}
}
Upvotes: 0
Views: 55
Reputation: 73
As @Kiheru said I moved the method,setVisibe() to the last and to make it work, I added two methods repaint() & revalidate() after adding the leftPane.
see as below:
Thank you, all :)
Upvotes: 2