设置大小不适用于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应用程序。 (服务器等,由其他人完成)。

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

例如:

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

不行。

谢谢您的帮助。

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

调用setSize()仅在您不使用LayoutManager时才有效。 由于您使用的是GridLayout您必须使用其他方式来指定所需内容。

尝试调用setPreferredSize()setMinimumSize()

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

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

但是如果从setSize() (对于TopLayoutContainer)更改为setPreferredSize()并且必须在setVisible()之前调用pack() ,则setXxxSize (对于ContainersChilds)将起到chaims的作用。