我想弄清楚为什么我看不到我的标签,就像当我尝试将 2 个标签放入 1 个面板时它们消失了,我似乎可以让它工作的唯一方法是将所有内容添加到 没有层次结构类型的 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);
}
}
最佳答案
由于您发布了很多代码,我不确定您想要实现什么,我修改了您的代码,在 topPane
中添加了 3 个 JLabel
。和 3 个 JRadioButtons
(我没有添加 ButtonGroup
)在下面的第二个 JPanel
上,我评论了如何让它们出现在垂直和水平对齐。
你应该考虑的是:
不要从
JFrame
扩展和创建对象(一个或另一个,而不是两个,我建议您创建对象)。您给
JPanel
布局 after 添加组件,它应该在 before 完成。从上面的观点来看,您还为
JLabel
而不是JPanel
提供了布局。您正在将
JList
添加到JLabel
中。您也错过了类构造函数。
不要有多个
JFrames
更多信息请参见 The use of multiple JFrames, Good / Bad practice下次发布没有依赖项的代码,例如您的
Truck
、Car
和Motorcycle
类(即 Runnable example ) .并改用纯文本,以便我们可以复制粘贴代码并查看问题。还可以尝试发布图片(或链接,我们可以编辑以添加它)。
现在,我自己程序的输出是:
它是用下面的代码完成的。
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);
}
}
关于java - JLabels 没有显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34210416/