我试图弄清楚为什么我看不到我的标签,就像当我尝试将2个标签放到一个面板中时,它们消失了一样,我似乎能使它正常工作的唯一方法是,如果我将所有内容都添加到JFrame中而没有任何类型等级。

import javax.swing.*;

import java.awt.*;

public class GUI extends JFrame {
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    static JRadioButton tirebg1 = new JRadioButton();
    static JRadioButton tirebg2 = new JRadioButton();
    static JRadioButton tirebg3 = new JRadioButton();

    static ButtonGroup tirebg = new ButtonGroup();

    public static void main(String[] args) {
        Car cspeed = new Car();
        int carspeed = cspeed.getSpeed();
        Motorcycle mspeed = new Motorcycle();
        int motospeed = mspeed.getSpeed();
        Truck tspeed = new Truck();
        int truckSpeed = tspeed.getSpeed();
        JRadioButton wide = new JRadioButton();
        JLabel tbuttons = new JLabel();
        JPanel topPane = new JPanel();
        tirebg.add(tirebg1);
        tirebg.add(tirebg2);
        tirebg.add(tirebg3);
        JFrame GUIframe = new JFrame();
        JLabel label1 = new JLabel();
        label1.setLayout(new FlowLayout());
        JLabel tireLabel = new JLabel();
        String[] names = new String[5];
        names[0] = "Car";
        names[1] = "Truck";
        names[2] = "Motorcycle";
        String[] hello = new String[5];
        GUIframe.setSize(500, 500);
        GUIframe.setDefaultCloseOperation(EXIT_ON_CLOSE);
        JList list = new JList(names);
        list.setBorder(BorderFactory.createRaisedSoftBevelBorder());
        label1.add(list);
        tireLabel.add(tirebg1);
        tireLabel.add(tirebg2);
        tireLabel.add(tirebg3);
        topPane.add(tbuttons);
        topPane.add(tireLabel);
        topPane.setLayout(new FlowLayout());

        label1.setBackground(Color.cyan);
        GUIframe.add(topPane);
        GUIframe.validate();
        GUIframe.setBackground(Color.GREEN);
        GUIframe.setVisible(true);
    }
}

最佳答案

由于您发布了很多代码,但我不确定您要实现什么目标,因此我修改了代码,在JLabel处添加了3个topPane。然后在第二个JRadioButtons的下面添加3个ButtonGroup(我没有添加JPanel),我评论了如何使它们出现在垂直和水平对齐方式上。

您应该考虑的是:


不要从JFrame扩展和创建对象(一个或另一个,而不是两者,我建议您创建对象)。
您在向JPanel添加组件后为其提供了一个布局,应该在此之前完成。
从以上的角度来看,您还将布局分配给了JLabel而不是JPanel
您正在将JList添加到JLabel
您也错过了一个类构造函数。
没有多个JFrames有关更多信息,请参见The use of multiple JFrames, Good / Bad practice
下次发布一个没有依赖性的代码,例如您的TruckCarMotorcycle类(即Runnable example)。并改用纯文本,以便我们可以复制粘贴代码并查看问题。另外,请尝试发布图片(或链接,我们可以对其进行编辑以添加图片)。


现在,我自己的程序的输出是:

java - JLabel没有显示-LMLPHP java - JLabel没有显示-LMLPHP

并使用以下代码完成。

import javax.swing.*;
import java.awt.*;
public class GUIExample {
    JFrame frame;
    JLabel label1, label2, label3;
    JPanel topPane, radioPane;
    JRadioButton radio1, radio2, radio3;
    public static void main(String[] args) {
        new GUIExample();
    }

    GUIExample () {
        frame = new JFrame();

        topPane = new JPanel();
        radioPane = new JPanel();
        topPane.setLayout(new FlowLayout());
        // radioPane.setLayout(new BoxLayout(radioPane, BoxLayout.PAGE_AXIS)); //Vertical align
        radioPane.setLayout(new FlowLayout()); //Horizontal align

        label1 = new JLabel("Car");
        label2 = new JLabel("Motorcycle");
        label3 = new JLabel("Truck");

        radio1 = new JRadioButton("Radio1");
        radio2 = new JRadioButton("Radio2");
        radio3 = new JRadioButton("Radio3");

        topPane.add(label1);
        topPane.add(label2);
        topPane.add(label3);

        radioPane.add(radio1);
        radioPane.add(radio2);
        radioPane.add(radio3);

        frame.add(topPane, BorderLayout.PAGE_START);
        frame.add(radioPane, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}

10-07 19:42
查看更多