【发布时间】:2016-02-25 04:13:47
【问题描述】:
我很新,还有很多东西要学。非常感谢任何花时间帮助我的人。我尝试了多种不同的方法来将我的按钮定位在与框架的特定坐标处,但由于某种原因,用于放置 JButtons 的常用语句都不起作用。有什么建议吗?
这是我当前的工作代码
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class GUI extends JFrame
{
JButton b1; // Declares Swing button variable and gives identifier (b1)
JButton b2; // Declares Swing label variable and gives identifier (l1)
public GUI()
{
setTitle("Virus Explortation");
setSize(400,400);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setLayout(new BorderLayout());
setContentPane(new JLabel(new ImageIcon("C:\\Pictures\\matrix.gif")));
setLayout(new FlowLayout());
b1=new JButton("I am a button");
add(b1);
b2=new JButton("I am also a button");
add(b2);
setSize(399,399);
setSize(400,400);
}
public static void main(String args[])
{
new GUI();
}
}
【问题讨论】:
-
您需要了解布局管理器为您做什么,更多详情请参阅Laying Out Components Within a Container。
-
在有人告诉你之前,我也会避免使用
null布局,像素完美的布局是现代 UI 设计中的一种错觉。影响组件单个尺寸的因素太多,您无法控制。 Swing 旨在与核心布局管理器一起工作,丢弃这些将导致无穷无尽的问题和问题,您将花费越来越多的时间来尝试纠正 -
作为one possible example 另一个是制作自己的布局管理器
-
@Jones 你可以看看下面我的解决方案。
标签: java swing jbutton layout-manager