JLabel定位不起作用 [英] JLabel positioning is not working

查看:129
本文介绍了JLabel定位不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将JLabel放置在我的JFrame中.我创建了一个标签,并希望设置其位置,但是setAlignmentsetBounds都不起作用.标签始终保持在面板顶部,并且不会移动.

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

你能帮忙吗?

推荐答案

以最小尺寸(如果可调整大小,则具有更大的宽度和高度)提供ASCII艺术作品或GUI预期布局的简单图形-以显示额外的应该使用空间.我可以从当前的描述中猜出这是最好的.

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.

它显示了如何在标签的左侧和顶部(通过使用EmptyBorder)在红色面板中填充20像素,在蓝色面板中填充100 x 30.

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.

阅读代码中的注释以获取更多提示,并检查Java Docs是否使用了与原始源代码不同的任何方法.

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

这篇关于JLabel定位不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆