0

I'm making a series of simple ball games, then probably progressing it further. I'm making these games to extend my knowledge about java and I'm trying to make this JLabel show up, and on the right side of this line seperator I've made here's a picture of it.

enter image description here

Here's the code

import javax.swing.*; import java.awt.*; import java.awt.geom.*;

public class MainMenu {

    public JFrame BallFrame = new JFrame();
    public JPanel BallPanel = new JPanel();
    public static MainMenu instance;
    Line2D verticalLine;  
    public int[] menuSize = {400, 380}; public int getMenuSize(int id) { if(id>=0 && id<=1) { return menuSize[0]; }else{ return menuSize[1]; } };

    public static void main(String[] args) {

    boolean scrollAble = Boolean.parseBoolean(args[0]);

    MainMenu menu = instance = new MainMenu();
    menu.startUp();

    /*if(scrollAble){
         JScrollPane pane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
         menu.BallFrame.setContentPane(pane);   
    } else {
         menu.BallFrame.setContentPane(menu.BallPanel); 
    }*/
    } 
    public int[] RightHandMessageBounds = {5,5}; public int getRightHandMessageBounds(int id){ if(id==1){ return RightHandMessageBounds[0]; }else{ return RightHandMessageBounds[1]; } };

    private void startUp() { 
        JLabel RightHandMessage;
        RightHandMessage = new JLabel("<html><col=000000>Welcome to the ball game!</font></html>", JLabel.CENTER);
        RightHandMessage.setLocation(getRightHandMessageBounds(1),getRightHandMessageBounds(2));
        BallFrame.setLocationRelativeTo(null);    
        BallFrame.setTitle("The Ball Game V1 - Cam"); 
        BallFrame.setSize(getMenuSize(1),getMenuSize(2));  
        BallPanel.setLayout(new FlowLayout());
        //BallPanel.setBackground(Color.WHITE);
        BallPanel.setSize(getMenuSize(1),getMenuSize(2)); 
        BallPanel.add(createVerticalSeparator());
        BallPanel.add(RightHandMessage);  
        BallFrame.setContentPane(BallPanel); 
        BallFrame.setLocationByPlatform(true);
        BallPanel.setVisible(true); 
        BallFrame.setVisible(true);


    } 

    public static JComponent createVerticalSeparator() {
        JSeparator x = new JSeparator(SwingConstants.VERTICAL);
        x.setPreferredSize(new Dimension(3,9999*100));
        return x;
    }
Mike Laren
  • 8,028
  • 17
  • 51
  • 70
  • 1
    1- The color property in the HTML is invalid & could be established using setForeground on the label instead; 2- setSize and setLocation on the components is kind of irrelevant why the container is using a layout manager – MadProgrammer Jun 10 '15 at 00:43
  • 3- Using a preferredSize of 399900 seems to be a massive overkill, especially when you could use a layout manager to achieve the same result without the need to use setPreferredSize – MadProgrammer Jun 10 '15 at 00:46
  • Sorry, can anybody help me fix this? I'm trying to make the JLabel appear. – Curryfishrs Jun 10 '15 at 01:00
  • So, you need to start at correcting those three issues, start by removing the JSeparator and HTML and see if that works – MadProgrammer Jun 10 '15 at 01:10
  • The height of your separator is throwing your layout into chaos, which is why the label isn't visible. Why would you want it to be 999,900 pixels high? A standard HD screen is only 1,080 pixels high. – VGR Jun 10 '15 at 01:11

1 Answers1

0

In situations like this, you need to strip your code back to basics and try and see where the problem is, lets start by seeing if we can just get the label to display...

private void startUp() {
    JLabel RightHandMessage;
    //RightHandMessage = new JLabel("<html><col=000000>Welcome to the ball game!</font></html>", JLabel.CENTER);
    RightHandMessage = new JLabel("Welcome to the ball game!", JLabel.CENTER);
    RightHandMessage.setLocation(getRightHandMessageBounds(1), getRightHandMessageBounds(2));
    BallFrame.setLocationRelativeTo(null);
    BallFrame.setTitle("The Ball Game V1 - Cam");
    //BallFrame.setSize(getMenuSize(1), getMenuSize(2));
    BallPanel.setLayout(new FlowLayout());
    //BallPanel.setBackground(Color.WHITE);
    //BallPanel.setSize(getMenuSize(1), getMenuSize(2));
    //BallPanel.add(createVerticalSeparator());
    BallPanel.add(RightHandMessage);
    BallFrame.setContentPane(BallPanel);
    BallFrame.pack();
    BallFrame.setLocationByPlatform(true);
    //BallPanel.setVisible(true);
    BallFrame.setVisible(true);

}

enter image description here

Okay, so we know we can display a basic label, what about with the HTML?

private void startUp() {
    JLabel RightHandMessage;
    RightHandMessage = new JLabel("<html><col=000000>Welcome to the ball game!</font></html>", JLabel.CENTER);
    RightHandMessage.setLocation(getRightHandMessageBounds(1), getRightHandMessageBounds(2));
    BallFrame.setLocationRelativeTo(null);
    BallFrame.setTitle("The Ball Game V1 - Cam");
    //BallFrame.setSize(getMenuSize(1), getMenuSize(2));
    BallPanel.setLayout(new FlowLayout());
    //BallPanel.setBackground(Color.WHITE);
    //BallPanel.setSize(getMenuSize(1), getMenuSize(2));
    //BallPanel.add(createVerticalSeparator());
    BallPanel.add(RightHandMessage);
    BallFrame.setContentPane(BallPanel);
    BallFrame.pack();
    BallFrame.setLocationByPlatform(true);
    //BallPanel.setVisible(true);
    BallFrame.setVisible(true);

}

enter image description here

So, okay, even though the mark up is invalid, that seems to work, what about the vertical spacer?

private void startUp() {
    JLabel RightHandMessage;
    RightHandMessage = new JLabel("<html><col=000000>Welcome to the ball game!</font></html>", JLabel.CENTER);
    RightHandMessage.setLocation(getRightHandMessageBounds(1), getRightHandMessageBounds(2));
    BallFrame.setLocationRelativeTo(null);
    BallFrame.setTitle("The Ball Game V1 - Cam");
    //BallFrame.setSize(getMenuSize(1), getMenuSize(2));
    BallPanel.setLayout(new FlowLayout());
    //BallPanel.setBackground(Color.WHITE);
    //BallPanel.setSize(getMenuSize(1), getMenuSize(2));
    BallPanel.add(createVerticalSeparator());
    BallPanel.add(RightHandMessage);
    BallFrame.setContentPane(BallPanel);
    BallFrame.pack();
    BallFrame.setLocationByPlatform(true);
    //BallPanel.setVisible(true);
    BallFrame.setVisible(true);

}

enter image description here

Ahh, that's where out problem "seems" to be, but how to fix?

First, we need to get rid of setPreferredSize, you might like to have a look at Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? for more details about why you should avoid using it.

enter image description here

Okay, so we have the label, but what separator?

Well, we need to do two basic things, first, we need to override the getPreferredSize method of the BallPanel to return the preferred size we want and then we need to change the layout manager of the BallPanel so it gives us more control to do things we want...

public JPanel BallPanel = new JPanel() {
    @Override
    public Dimension getPreferredSize() {
        return new Dimension(400, 380);
    }
};

//...

private void startUp() {
    JLabel RightHandMessage;
    RightHandMessage = new JLabel("<html><col=000000>Welcome to the ball game!</font></html>", JLabel.CENTER);
    RightHandMessage.setLocation(getRightHandMessageBounds(1), getRightHandMessageBounds(2));
    BallFrame.setLocationRelativeTo(null);
    BallFrame.setTitle("The Ball Game V1 - Cam");
    BallPanel.setLayout(new GridBagLayout());

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.weighty = 1;
    gbc.fill = GridBagConstraints.VERTICAL;

    BallPanel.add(createVerticalSeparator(), gbc);

    gbc.gridx = 1;
    gbc.weighty = 0;
    gbc.fill = GridBagConstraints.NONE;
    gbc.anchor = GridBagConstraints.NORTH;
    BallPanel.add(RightHandMessage, gbc);

    BallFrame.setContentPane(BallPanel);
    BallFrame.pack();
    BallFrame.setLocationByPlatform(true);
    //BallPanel.setVisible(true);
    BallFrame.setVisible(true);

}

enter image description here

Take a look at Laying Out Components Within a Container and How to Use GridBagLayout for more ideas

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366