java 设置大小在java中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5714214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:26:17  来源:igfitidea点击:

set size wont work in java

javaswing

提问by amana

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);
}

I am working on a simple java chat client gui application. (the server etc, is done by others).

我正在开发一个简单的 java 聊天客户端 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.

这不是一个大项目,但我唯一的问题是,无论我尝试调整上述 GUI 上任何组件的大小,都将不起作用。

For example:

例如:

JTextArea client_text_input = new JTextArea();
client_text_input.setSize(100,200);

Won't work.

不会工作。

Thanks for the help.

谢谢您的帮助。

回答by Joachim Sauer

In Swing, you have two options for layout: do everything manually or let a LayoutManagerhandle it for you.

在 Swing 中,您有两种布局选项:手动完成所有操作或让LayoutManager您处理。

Calling setSize()will only work when you're not using a LayoutManager. Since you're using a GridLayoutyou'll have to use other ways to specify what you want.

调用setSize(),当你不使用才有效LayoutManager。由于您使用的是 a ,因此GridLayout您必须使用其他方式来指定您想要的内容。

Try calling setPreferredSize()and setMinimumSize().

尝试调用setPreferredSize()setMinimumSize()

回答by Bastardo

Two things - firstly you should be setting the preferredSize of the scrollpane, but secondly, trying to resize it inside the componentResized handler isn't a very effective technique because the 'resized' events aren't continuous.

两件事 - 首先,您应该设置滚动窗格的 preferredSize,但其次,尝试在 componentResized 处理程序中调整它的大小并不是一种非常有效的技术,因为 'resized' 事件不是连续的。

check resizing text area in a JFrame

检查JFrame 中调整文本区域的大小

回答by mKorbel

but setXxxSize(for ContainersChilds) works as chaims if you change from setSize()(for TopLayoutContainer) to setPreferredSize()and you have to call pack()before setVisible()

但是setXxxSize(对于ContainersChilds)如果你从setSize()(对于TopLayoutContainer)更改为setPreferredSize()并且你必须在pack()之前调用setVisible()