如何设置JFrame中对象的位置?
我有标签和 JButtons,我想在 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);
}
}
setLocation() 在这里似乎不起作用。如何管理JFrame中对象的位置?
I have Labels and JButtons i want to define the position in 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);
}
}
setLocation() doesnot seem to work here. How to manage the object's position in JFrame?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用正确的LayoutManager。例如
GridBagLayout
。或者,您可以组合多个嵌套面板,为每个面板分配自己的布局管理器。
最糟糕的方法是将布局设置为 null 并使用 setBounds()
Use proper LayoutManager. E.g.
GridBagLayout
.Or you can combine multiple nested panels assigning own LayoutManager for each panel.
The worst way is to set layout to null and use
setBounds()
FlowLayout
为您提供了一些选项。请查看此处。例如
FlowLayout
gives you some options. Look here .For Example
我总是用这个:
http://download.oracle.com/javase/tutorial/uiswing/layout /using.html
:)
I always use this:
http://download.oracle.com/javase/tutorial/uiswing/layout/using.html
:)
Netbeans GUI 生成器非常棒。我建议你调查一下。
http://netbeans.org/kb/docs/java/quickstart-gui.html
The Netbeans GUI Builder is great. I suggest you look into it.
http://netbeans.org/kb/docs/java/quickstart-gui.html