我有标签和 JButton,我想在 JFrame 中定义位置。
import java.awt.*;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.swing.*;
public class GuiFrame extends JFrame {
public static void main(String[] args) throws UnknownHostException {
JFrame f = new JFrame("This is a test");
f.setSize(400, 150);
JRadioButton ButtonServer = new JRadioButton("Server");
JRadioButton ButtonClient = new JRadioButton("Client");
InetAddress thisIp = InetAddress.getLocalHost();
Label lip = new Label("Your IP is : " + thisIp.getHostAddress());
Label setup = new Label("Setup as ");
JButton ButtonOk = new JButton("OK");
Container content = f.getContentPane();
content.setBackground(Color.white);
content.setLayout(new FlowLayout());
content.add(lip);
content.add(setup);
content.add(ButtonServer);
content.add(ButtonClient);
content.add(ButtonOk);
// f.addWindowListener(new ExitListener());
f.setVisible(true);
}
}
set Location() 在这里似乎不起作用。如何管理对象在JFrame中的位置?
最佳答案
使用正确的 LayoutManager。例如。 GridBagLayout
。
或者您可以组合多个嵌套面板,为每个面板分配自己的 LayoutManager。
最糟糕的方法是将布局设置为 null 并使用 setBounds()
关于java - 如何设置对象在JFrame 中的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8005484/