繁体   English   中英

我想要2个JLabel在Java的同一JFrame上显示

[英]I want 2 JLabels to show on the same JFrame in Java

我无法同时将JLabel片段显示在同一JFrame上。 我尝试过调整大小; 但这没用。 我需要将JLabel放置在JPanel中吗? 我希望两个JLabel都显示在同一JFrame上。 任何帮助表示赞赏。 谢谢。

public class HelloWorldFrame extends JFrame
{
    private TitledBorder title;
    private TitledBorder title2;

    public HelloWorldFrame()
    {
        super("Hello World!                  ");
        JFrame helloWorld = new JFrame();
        JLabel label = new JLabel();

        title = BorderFactory.createTitledBorder("Language");
        title.setTitleJustification(TitledBorder.LEFT);
        label.setBorder(title);
        add(label);

        setSize(300, 200);

        JRadioButton button1 = new JRadioButton("English");
        JRadioButton button2 = new JRadioButton("French");
        JRadioButton button3 = new JRadioButton("Spanish");

        label.setLayout(new FlowLayout());
        label.add(button1);
        label.add(button2);
        label.add(button3);

        JLabel label2 = new JLabel();
        title2 = BorderFactory.createTitledBorder("Greeting");
        title2.setTitleJustification(TitledBorder.LEFT);
        label2.setBorder(title2);
        label2.setSize(100, 100);
        add(label2);

        helloWorld.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
}

//The main begins below

import javax.swing.JFrame;

public class HelloWorldApp
{
    public static void main(String[] args)
    {
        JFrame helloWorld = new HelloWorldFrame();
        helloWorld.setVisible(true);    
    }

}

您需要示例中的布局管理器。

您应该具有的结构是: JFrame > JPanel > JLabel

JFrame frame = new JFrame();
JPanel panel = new JPanel();

JLabel label_1 = new JLabel("some text");
JLabel label_2 = new JLabel("other text");

panel.add(label_1);
panel.add(label_2);

frame.add(panel);
frame.pack();
frame.setVisible(true);

在您的代码中,您有一些JButton :将它们添加到JPanel ,而不是JLabel

暂无
暂无

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

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