JLabel positioning is not working

强颜欢笑 提交于 2019-12-24 19:43:08

问题


I need to position the JLabel in my JFrame. I created a label and wanted to set its position, but both setAlignment and setBounds do not work. The label stays all the time at the top of the panel and it does not move.

import java.awt.GridLayout;
import javax.swing.*;

public class Start extends JFrame {
    //JLabel label1 = new JLabel("HELLO!");
    //JLabel label2 = new JLabel("CHOOSE LANGUAGE:");

    public Start() {
        super();
        setSize(480, 360);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(null);
        setLayout(new GridLayout(2, 1));
        //setLayout(new FlowLayout());
        JPanel panel1 = new JPanel();

        add(panel1);
        JLabel label1 = new JLabel("HELLO!");
        //label1.setBounds(20,20,100,20);
        //label1.SetAlignmentX(20);
        //label1.SetAlignmentY(20);
        panel1.add(label1);
    }

    public static void main(String[] args) {
        Start frame = new Start();
        frame.setVisible(true);
    }
}

Could you help?


回答1:


Provide ASCII art or a simple drawing of the intended layout of the GUI at minimum size, and if resizable, with more width and height - to show how the extra space should be used. This is the best I can guess the requirement from the current description.

It shows how to pad the left and top of the label (by using an EmptyBorder) by 20 pixels in the red panel, and by 100 x 30 in the blue panel.

Read the comments in the code for further tips, and check the Java Docs for any methods used that are different to the original source code.

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

public class Start extends JFrame {

    public Start() {
        super();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new GridLayout(2, 1));

        JPanel panel1 = new JPanel(new FlowLayout(SwingConstants.LEADING));
        panel1.setBorder(new LineBorder(Color.RED, 4));
        add(panel1);
        JLabel label1 = new JLabel("HELLO!", SwingConstants.LEADING);
        //label1.setBounds(20,20,100,20);

        // to provide a 20 x 20 offset, an empty border will do nicely.
        label1.setBorder(new EmptyBorder(20, 20, 0, 0));

        // Did you actually check the Java Docs for these methods? 
        // They do not do what you seem to think they do.
        //label1.SetAlignmentX(20);
        //label1.SetAlignmentY(20);
        panel1.add(label1);

        JPanel panel2 = new JPanel(new FlowLayout(SwingConstants.LEADING));
        panel2.setBorder(new LineBorder(Color.BLUE, 4));
        add(panel2);
        JLabel label2 = new JLabel("HELLO!", SwingConstants.LEADING);
        label2.setBorder(new EmptyBorder(30, 100, 30, 100));
        panel2.add(label2);

        pack();
    }

    public static void main(String[] args) {
        Start frame = new Start();
        frame.setVisible(true);
    }
}


来源:https://stackoverflow.com/questions/49211619/jlabel-positioning-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!