简体   繁体   English

JAVA为什么此JLabel不显示?

[英]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. 我正在制作这些游戏以扩展我对Java的了解,并试图使此JLabel出现,在此行分隔符的右侧,我制作了一张图片。

在此处输入图片说明

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? 好的,所以我们知道我们可以显示一个基本标签,那么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? 首先,我们需要摆脱setPreferredSize ,您可能想看看我应该避免在Java Swing中使用set(Preferred | Maximum | Minimum)Size方法吗? 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... 好,我们需要做两个基本的事情,首先,我们需要重写BallPanelgetPreferredSize方法以返回所需的首选大小,然后我们需要更改BallPanel的布局管理器,以便它使我们有更多的控制权来做事情我们想要...

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 看一下在容器布置组件以及如何使用GridBagLayout以获得更多想法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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