设置大小将无法在Java中工作 [英] set size wont work in java
问题描述
public void start_Gui() {
JFrame window = new JFrame("Client Program");
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
window.setContentPane(panel);
panel.setLayout(new GridLayout(1,2));
JLabel leftside = new JLabel();
leftside.setLayout(new GridLayout(2, 1));
JTextArea rightside = new JTextArea();
rightside.setEditable(false); //add scroll pane.
rightside.setBorder(BorderFactory.createLineBorder(Color.BLACK));
rightside.setLayout(new FlowLayout());
JTextArea client_text_input = new JTextArea();
client_text_input.setBorder(BorderFactory.createLineBorder(Color.BLACK));
leftside.add(client_text_input);
JLabel buttons_layer = new JLabel();
JButton login = new JButton("Login");
JButton logout = new JButton("Logout");
buttons_layer.setBorder(BorderFactory.createLineBorder(Color.BLACK));
buttons_layer.setLayout(new GridLayout(2, 1));
buttons_layer.add(login);
buttons_layer.add(logout);
leftside.add(buttons_layer);
panel.add(leftside);
panel.add(rightside);
window.setSize(300, 400);
window.setResizable(false);
window.setVisible(true);
}
我正在研究一个简单的Java聊天客户端gui应用程序.(服务器等,由其他人完成).
I am working on a simple java chat client gui application. (the server etc, is done by others).
这不是一个很大的项目,但是我唯一的问题是,无论如何我试图在上述GUI上调整任何组件的大小,都行不通.
It is not a big project, but my only problem is that whatever I do to try to resize any components on the above GUI, won't work.
例如:
JTextArea client_text_input = new JTextArea();
client_text_input.setSize(100,200);
不起作用.
感谢您的帮助.
推荐答案
在Swing中,您有两个布局选项:手动完成所有操作或让 LayoutManager
替您处理.
In Swing, you have two options for layout: do everything manually or let a LayoutManager
handle it for you.
仅当您不使用 LayoutManager
时,调用 setSize()
才有效.由于使用的是 GridLayout
,因此必须使用其他方法来指定所需的内容.
Calling setSize()
will only work when you're not using a LayoutManager
. Since you're using a GridLayout
you'll have to use other ways to specify what you want.
尝试调用 setMinimumSize()
.
Try calling setPreferredSize()
and setMinimumSize()
.
这篇关于设置大小将无法在Java中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!