Java 如何在JFrame中设置对象的位置?
我有标签和JButton,我想定义JFrame中的位置Java 如何在JFrame中设置对象的位置?,java,swing,jframe,layout-manager,Java,Swing,Jframe,Layout Manager,我有标签和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 {
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);
}
}
setLocation()在这里似乎不起作用。如何管理对象在JFrame中的位置?使用适当的布局管理器。例如,
GridBagLayout
也可以组合多个嵌套面板,为每个面板指定自己的LayoutManager
最糟糕的方法是将布局设置为null,并使用
setBounds()
FlowLayout
提供一些选项。看
比如说
FlowLayout layout = new FlowLayout();
layout.setAlignment(FlowLayout.CENTER);
c.setLayout(layout);
c.add(panel);
我总是用这个:
:)Netbeans GUI构建器很棒。我建议你调查一下
请参见。同意,setLayout null是一种不好的方法!我见过一些LayoutManager,但似乎没有一个LayoutManager具有在JFrame中设置位置(x,y)的功能。有吗?