简体   繁体   English

为什么这个JLabel不在这个Java应用程序的中心?

[英]Why isn't this JLabel in the center of this Java application?

newbie Java programmer here: 新手Java程序员在这里:

I hate asking questions every time I run into a problem, but I don't see what I should use to get the green "Hello World" label go right into the center of the JPanel. 每当我遇到问题时,我都不想问问题,但是我看不到应该如何使用绿色的“ Hello World”标签进入JPanel的中心。 Here is my code: 这是我的代码:

package game;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Javagame extends JPanel implements ActionListener{
    protected JButton b1;
    private JLabel label;
    public Javagame() {
        b1 = new JButton("Button!");
        b1.setActionCommand("change");

        b1.addActionListener(this);
        add(b1);

        label = new JLabel("Hello World!", SwingConstants.CENTER);
        label.setFont(new Font("Arial", Font.BOLD, 20));
        label.setForeground(new Color(0x009900));
        add(label, BorderLayout.CENTER);
    }
    public void actionPerformed(ActionEvent e) {
        if ("change".equals(e.getActionCommand())) {
            label.setText("Hello Universe!");   
            b1.setActionCommand("changeBack");
        }
        if ("changeBack".equals(e.getActionCommand())) {
            label.setText("Hello World!");
            b1.setActionCommand("change");
        }
    }
    private static void createWindow(){
        JFrame frame = new JFrame("Javagame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(500,500));

        Javagame newContentPane = new Javagame();
        newContentPane.setOpaque(true);
        frame.setContentPane(newContentPane);

        frame.setLocationRelativeTo(null);
        frame.pack();
        frame.setVisible(true);
    }
    public static void main(String[] args) {
        createWindow();
    }
}

BorderLayout.CENTER doesn't seem to work in add() . BorderLayout.CENTERadd()似乎不起作用。 Any help would be appreciated, thank you! 任何帮助,将不胜感激,谢谢!

The JLabel is in the centered within the parent container, the text is aligned within the label. JLabel位于父容器的中心,文本在标签内对齐。

Try... 尝试...

label.setHorizontalAlignment(JLabel.CENTER);
label.setVerticalAlignment(JLabel.CENTER);

BorderLayout.CENTER doesn't seem to work BorderLayout.CENTER似乎不起作用

The default layout of a JPanel is a FlowLayout. JPanel的默认布局是FlowLayout。 You need to set the layout to a BorderLayout. 您需要将布局设置为BorderLayout。

Also, you need to add the button to the NORTH of the BorderLayout. 另外,您需要将按钮添加到BorderLayout的NORTH。

Then the button will appear at the top and the label will be centered. 然后,按钮将出现在顶部,标签将居中。

When adding the label use FlowLayout field. 添加标签时,请使用FlowLayout字段。

//Code apove
label = new JLabel("Hello World!", SwingConstants.CENTER);
label.setFont(new Font("Arial", Font.BOLD, 20));
label.setForeground(new Color(0x009900));
add(label, FlowLayout.CENTER);//!!
//Code under

Because you haven't changed the layout in your panel, don't use BorderLayout. 因为您尚未更改面板中的布局,所以请勿使用BorderLayout。

If you really want to use BorderLayout rarther than FlowLayout add just setLayout() command in it. 如果您真的想使用BorderLayout而不是FlowLayout,请在其中添加setLayout()命令。

//Code apove
label = new JLabel("Hello World!", SwingConstants.CENTER);
label.setFont(new Font("Arial", Font.BOLD, 20));
label.setForeground(new Color(0x009900));
setLayout(new BorderLayout()); //!!
add(label, BorderLayout.CENTER);
add(b1,BorderLayout.????); //Edited
//Code under

@Edit - add your button too after setLayout() method or set the layout in the beginning when you make you panel @Edit-在setLayout()方法之后也添加按钮,或者在设置面板时在开头设置布局

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

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