JAVA为什么不会出现这个JLabel?

时间:2015-06-09 23:55:09

标签: java

我正在进行一系列简单的球类运动,然后可能会进一步发展。我正在制作这些游戏来扩展我对java的了解,并且我试图让这个JLabel出现,并且在这行的右侧分隔符我在这里制作了一张照片。它

enter image description here

这是代码

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

1 个答案:

答案 0 :(得分:0)

在这种情况下,您需要将代码删回基础并尝试查看问题所在,让我们先看看是否可以显示标签......

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

好的,我们知道我们可以显示一个基本标签,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

所以,好吧,即使标记无效,这似乎也有效,垂直隔离怎么办?

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

啊,这就是问题“似乎”的地方,但如何解决?

首先,我们需要摆脱setPreferredSize,您可能需要查看Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?,了解有关您应该避免使用它的详细信息。

enter image description here

好的,我们有标签,但是有什么分隔符?

好吧,我们需要做两件基本的事情,首先,我们需要覆盖getPreferredSize的{​​{1}}方法,以返回我们想要的首选大小,然后我们需要更改布局管理器BallPanel所以它让我们有更多的控制来做我们想要的事情......

BallPanel

enter image description here

请查看Laying Out Components Within a ContainerHow to Use GridBagLayout了解更多提示