简体   繁体   中英

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

newbie Java programmer here:

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. 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() . Any help would be appreciated, thank you!

The JLabel is in the centered within the parent container, the text is aligned within the label.

Try...

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

BorderLayout.CENTER doesn't seem to work

The default layout of a JPanel is a FlowLayout. You need to set the layout to a BorderLayout.

Also, you need to add the button to the NORTH of the BorderLayout.

Then the button will appear at the top and the label will be centered.

When adding the label use FlowLayout field.

//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.

If you really want to use BorderLayout rarther than FlowLayout add just setLayout() command in it.

//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

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