简体   繁体   中英

JAVA Why won't this JLabel show up?

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.

在此处输入图片说明

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;
    }

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);

}

在此处输入图片说明

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);

}

在此处输入图片说明

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);

}

在此处输入图片说明

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.

在此处输入图片说明

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);

}

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM