【发布时间】:2026-01-15 10:00:01
【问题描述】:
如何显示jlabel文本
JFrame frame = new JFrame("number adition");
JButton button1 = new JButton("add");
button1.setBounds(110, 250, 70, 20);
frame.add(button1);
frame.setSize(500, 400);
frame.setLayout(null);
frame.setVisible(true);
JButton button2 = new JButton("Clear");
button2.setBounds(210, 250, 70, 20);
frame.add(button2);
JButton button3 = new JButton("Exit");
button3.setBounds(210, 280, 70, 20);
frame.add(button3);
JLabel label1= new JLabel("First", JLabel.NORTH_EAST);
label1.setAlignmentX(0);
label1.setAlignmentY(0);
frame.add(label1);
【问题讨论】:
-
与所有与此主题相关的问题一样,应在建立 UI 后最后调用
frame.setVisible(true);。否则你需要调用revalidate和repaint,但是由于你已经扔掉了布局管理器,revalidate将不起作用 -
避免使用
null布局,像素完美的布局是现代用户界面设计中的一种错觉。影响组件单个尺寸的因素太多,您无法控制。 Swing 旨在与核心布局管理器一起工作,丢弃这些将导致无穷无尽的问题和问题,您将花费越来越多的时间来尝试纠正