JPanel not displaying JLabel - java

I am trying to make an application, which displays a JLabel and a button, which if clicked switches to another jPanel. For some reason my JLabel is not displaying at all in either case. I would appreciate an expert eye to look over my code and see what I am doing wrong. Thanks in advance.
HomeScreenUI(){
//frame
JFrame frame = new JFrame("Opisa");
//panels, one before button click and one after
JPanel panel = new JPanel();
JPanel panelAfterButtonClick = new JPanel();
panel.setLayout(null);
panelAfterButtonClick.setLayout(null);
//jlabel that isnt displaying + dimensions
JLabel label = new JLabel("Opisa");
Dimension size = label.getPreferredSize();
label.setBounds(100, 100, size.width, size.height);
label.setFont(new Font("Helvetica", Font.PLAIN, 70));
//second jlabel that isn't displaying
JLabel label2 = new JLabel("Opisa");
Dimension size4 = label2.getPreferredSize();
label2.setBounds(100, 100, size4.width, size4.height);
label2.setFont(new Font("Helvetica", Font.PLAIN, 70));
//adding the labels to the panels
panel.add(label);
panelAfterButtonClick.add(label2);
//button that is displaying both before and after
JButton button = new JButton("Click Me..");
JButton buttonAfterClick = new JButton("Clicked Me..");
//dimensions
Dimension size2 = button.getPreferredSize();
button.setBounds(100, 100, size2.width, size2.height);
Dimension size3 = button.getPreferredSize();
buttonAfterClick.setBounds(100, 100, size3.width, size3.height);
//adding the buttons to the jpanel
panel.add(button);
panelAfterButtonClick.add(buttonAfterClick);
//function that changes the panel after the button is clicked
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
frame.setContentPane(panelAfterButtonClick);
frame.invalidate();
frame.validate();
}
});
//adding the panel to the frame and setting the size
frame.add(panel);
frame.setSize(1000,800);
frame.setVisible(true);
}

Check the label bounds, more specifically its size... try setting some fixed values (great enough to show its content like setBounds(100, 100, 250, 80)).
The preferred size is being retrieved before changing the font size, so it is not big enough to show that big characters. Try changing the font first.

Related

can't make Jframe in center using setLocationRelativeTo(null);

I have a jframe when i use
setLocationRelativeTo(null);
the JFrame does not appear at the middle of window why it is so
my full code-
JFrame ca = new JFrame("Start");
ca.setUndecorated(true);
ca.setLocationRelativeTo(null);
JLabel lab = new JLabel(" Space Invaders");
JButton bu = new JButton("START");
JButton bu2 = new JButton("QUIT");
lab.setFont(new Font("Serif Italic", Font.BOLD, 60));
lab.setForeground(Color.RED);
bu.setForeground(Color.WHITE);
bu.setBackground(Color.BLACK);
bu.setFont(new Font("serif", Font.BOLD, 50));
bu.setBorderPainted(false);
bu2.setForeground(Color.WHITE);
bu2.setBackground(Color.BLACK);
bu2.setFont(new Font("serif", Font.BOLD, 50));
bu2.setBorderPainted(false);
My Output is this-
setLocationRelativeTo(null);
Must be invoked AFTER you have added components to the frame and invoked pack() on the frame, otherwise the frame has a size of (0, 0) so it can't be centered properly.
Edit:
JFrame frame = new JFrame(...);
frame.add(someComponent);
frame.add(anotherComponent);
frame.pack(); // now the frame width/height is greater than 0.
frame.setLocationRelativeTo( null );
frame.setVisible( true );

How to arrange components using BorderLayout?

I'm trying to get my GUI to appear as such:
Grocery Cart [Refill]
(TextArea)
I am currently using BorderLayout and I would like to stick with it. How can I get the text area underneath the JLabel and the JButton whilst being in the same JPanel? Here is my code for the specific area:
How do I add the text box underneath the two side by side? Whenever I add it, it just goes next to them.
JPanel newPanel = new JPanel();
JLabel label = new JLabel("Grocery Cart");
label.setFont(new Font("Arial", Font.BOLD, 20));
newPanel.add(label);
contentPane.add(newPanel, BorderLayout.WEST) ;
JButton btnNewButton = new JButton("Refill");
btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 20));
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
newPanel.add(btnNewButton);
If my understanding is correct, here's what you need to do:
JPanel mainPanel = new JPanel(new BorderLayout());
JPanel eastPanel = new JPanel(new BorderLayout());
JTextArea area = new JTextArea("Test content");
JLabel label = new JLabel("Grocery Cart");
label.setFont(new Font("Arial", Font.BOLD, 20));
mainPanel.add(label, BorderLayout.WEST);
JButton btnNewButton = new JButton("Refill");
btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 20));
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
eastPanel.add(btnNewButton, BorderLayout.WEST);
eastPanel.add(area, BorderLayout.CENTER);
mainPanel.add(eastPanel, BorderLayout.CENTER);
contentPane.add(mainPanel, BorderLayout.NORTH);
The main idea is that in order to construct complex layouts with simple layout types such as border and flow, you have to use container hierarchy and get creative with a combination of flow and border layouts.
In my example the label and button aren't resizable and always have their widths equal to their preferred widths. The text area, however is resizable and takes up its container's remaining width.
Note, that all components added to mainPanel are resizable vertically. In order to keep mainPanel to its preferred height you place it to the contentPane's BorderLayout.NORTH or SOUTH for that matter.

Java swing jpanel dimension

JLabel titleLabel = new JLabel(title, SwingConstants.CENTER);
titleLabel.setForeground(Color.DARK_GRAY);
titleLabel.setFont(new Font("Comic Sans MS", Font.PLAIN, 15));
add(titleLabel, BorderLayout.PAGE_START);
JLabel descLabel = new JLabel("<html>description");
descLabel.setForeground(Color.GRAY);
descLabel.setFont(new Font("Comic Sans MS", Font.PLAIN, 13));
add(descLabel, BorderLayout.PAGE_START);
JLabel picLabel = new JLabel(new ImageIcon(Home.class.getResource("/icon/img.jpg")));
picLabel.setIconTextGap(-310);
picLabel.setOpaque(true);
picLabel.setLayout(null);
add(picLabel);
JPanel buttonPanel = new JPanel();
buttonPanel.setBorder(new LineBorder(Color.BLACK, 1, true));
JButton btnDetails = new JButton("Dettagli");
JButton btnDetails2 = new JButton("Modifica");
buttonPanel.add(btnDetails);
buttonPanel.add(btnDetails2);
add(buttonPanel);
I want to change the dimension of the panel (see the image) and align it with the image.
Any suggestion ?
Change the layout of your main panel to use BorderLayout.
Then add the picLabel to be add(picLabel, BorderLayout.CENTER) and your buttonPanel as add(buttonPanel, BorderLayout.SOUTH) and you should get better layout.
Packing the dialog / frame will also help.
You cannot add two labels to the same position in a BorderLayout. Your label descLabel will replace your titleLabel
You could do that like the buttons: Create another panel, add both labels vertically and add that to your main panel with BorderLayout.NORTH (or PAGE_START)

Java GUi Being inconsistent

Hi so I am trying to creat this menu for the game, and when I run the code it is very hit or miss. In the same computer with same screen same OS and everything the same, I might run the code ones and the menu is fine. and then I run it again and the pictures are missalighed or the just disappear and so on. I have tried changing the order of the contentPane.add but and I am out of ideas. What else could it be? Thanks
public static int type(){
// Create a "clickable" image icon
int i =0;
Color c = new Color (0,0,0);
ImageIcon icon = new ImageIcon("images/mike_main.png");
JLabel label = new JLabel(icon);
ImageIcon icon1 = new ImageIcon("images/igal_main.png");
JLabel label1 = new JLabel(icon1);
JPanel contentPane = new JPanel();
JLabel label3 = new JLabel("Choose your character!");
JLabel mike_l = new JLabel("Mike");
JLabel mike_info = new JLabel("<html>Speed: 10<br>Range: 7</html>");
JLabel igal_l = new JLabel("Igal");
JLabel igal_info = new JLabel("<html>Speed: 7<br>Range: 10</html>");
label3.setBounds(600,100,2000,100);
label3.setFont(new Font("Serif", Font.BOLD, 56));
label3.setForeground(Color.WHITE);
contentPane.setOpaque(true);
contentPane.setBackground(c);
contentPane.setLayout(null);
final JFrame frame = new JFrame("My Window");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(label);
frame.setTitle("The Ultimate Game");
frame.setExtendedState(frame.MAXIMIZED_BOTH);
frame.pack();
frame.setVisible(true);
label.setBounds(400,400,300,600);
mike_l.setBounds(500,330,1000,100);
mike_info.setBounds(750,500,1000,100);
//label.setLocation(50, 50);
label1.setBounds(1200,400,300,600);
igal_l.setBounds(1300,330,1000,100);
igal_info.setBounds(1550,500,1000,100);
//label1.setLocation(250, 250);
mike_l.setFont(new Font("Serif", Font.BOLD, 26));
mike_l.setForeground(Color.WHITE);
mike_info.setFont(new Font("Serif", Font.BOLD, 26));
mike_info.setForeground(Color.WHITE);
igal_l.setFont(new Font("Serif", Font.BOLD, 26));
igal_l.setForeground(Color.WHITE);
igal_info.setFont(new Font("Serif", Font.BOLD, 26));
igal_info.setForeground(Color.WHITE);
contentPane.add(label);
contentPane.add(label1);
contentPane.add(label3);
contentPane.add(mike_l);
contentPane.add(mike_info);
contentPane.add(igal_l);
contentPane.add(igal_info);
frame.setContentPane(contentPane);
label.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
System.out.println("CLICKED");
frame.setTitle("Mike");
}
});
label1.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
System.out.println("CLICKED");
frame.setTitle("Igal");
}
});
while (i==0){
//System.out.println("I am here");
String s = frame.getTitle();
System.out.println(s);
if (s.equals("Mike")){
i =1;
}
else if(s.equals("Igal")){
i = 2;
}
// Add it to a frame.
}
System.out.println("im out");
frame.setVisible(false);
return i;
}
I think the issue is that your are calling frame.setVisible(true) before you are done making changes to frame. If you move this (and maybe the pack() call) pass all your add()s and after you setContentFrame(), then your window will open more reliably.
As for the alignment, that is going to come down to all your setBounds() calls. If you don't need to use setBounds() you should probably look into Layout Managers and nest JPanels for modular sections of you're design (e.g. character info).

Java Panel overlapping Menu

I am new to Java, so this question might be obvious.
I have this Initialization code:
frame = new JFrame();
frame.setTitle("Test");
frame.setBounds(100, 100, 512, 468);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new MyJPanel();
FlowLayout flowLayout = (FlowLayout) panel.getLayout();
panel.setAlignmentY(Component.TOP_ALIGNMENT);
panel.setAlignmentX(Component.LEFT_ALIGNMENT);
panel.setPreferredSize(new Dimension(0, 0));
panel.setMinimumSize(new Dimension(0, 0));
frame.getContentPane().add(panel, BorderLayout.CENTER);
JMenuBar menuBar = new JMenuBar();
frame.getContentPane().add(menuBar, BorderLayout.NORTH);
JMenu mnFile = new JMenu("File");
menuBar.add(mnFile);
mntmOpenBBinary.setPreferredSize(new Dimension(149, 22));
mnFile.add(mntmOpenBBinary);
JSeparator separator = new JSeparator();
mnFile.add(separator);
JMenuItem mntmExit = new JMenuItem("Exit");
mntmExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
mnFile.add(mntmExit);
MyJPanel is a custom class that extends the JPanel class. Just as a test, it just writes "test" to the screen in the paintComponent method:
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.BLACK);
g.setFont(RenderFont);
g.drawString("TEST", 1, 1);
}
You can see from the image below that, for some reason, the drawString method is drawing behind the menu. The coordinates I give in drawString, I'd think, would be the coordinates relative to the JPanel window. Also, the JPanel is "filling" the entire space of the JFrame. I'd prefer that my MyJFrame be only 100x100, but it seems to always want to auto fill the JFrame. How can I solve these 2 issues?
The text is hidden by the menu bar because last parameter of drawString() is the text baseline, and not the upper bound: http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html#drawString(java.lang.String,%20int,%20int)
So you need to use something like:
g.drawString("TEST", 1, 50);
Or better, use Font.getStringBounds() to compute your text height:
Rectangle2D textBounds = g.getFont().getStringBounds("TEST", (((Graphics2D) g).getFontRenderContext());
And to avoid having your panel taking all Frame space, replace:
frame.getContentPane().add(panel, BorderLayout.CENTER);
with:
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(panel, BorderLayout.NORTH);
frame.getContentPane().add(mainPanel, BorderLayout.CENTER);
Although, you should not need this:
panel.setAlignmentY(Component.TOP_ALIGNMENT);
panel.setAlignmentX(Component.LEFT_ALIGNMENT);
panel.setPreferredSize(new Dimension(0, 0));
panel.setMinimumSize(new Dimension(0, 0));

Categories

Resources