我正在使用Spring Layout为我正在构建的应用程序设计一个gui。我有弹簧布局的问题;组件之间没有正确的间距,特别是消息框和发送按钮,如图所示:
以下是该应用程序的代码:
package com.cliff777.chat;
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
public class ChatMain
{
final int WIDTH = 800;
final int HEIGHT = 600;
JTextField messageBox = new JTextField(); //write a message in
JTextArea chatBox = new JTextArea(); //displays the messages
JButton sendButton = new JButton("Send");
void setupGui(Container container)
{
container.setPreferredSize(new Dimension(WIDTH, HEIGHT));
SpringLayout layout = new SpringLayout();
container.setLayout(layout);
container.add(messageBox);
container.add(chatBox);
container.add(sendButton);
layout.putConstraint(SpringLayout.NORTH, chatBox, 5, SpringLayout.NORTH, container);
layout.putConstraint(SpringLayout.WEST, chatBox, 5, SpringLayout.WEST, container);
layout.putConstraint(SpringLayout.EAST, chatBox, 5, SpringLayout.EAST, container);
layout.putConstraint(SpringLayout.SOUTH, chatBox, 5, SpringLayout.NORTH, messageBox);
layout.putConstraint(SpringLayout.SOUTH, chatBox, 5, SpringLayout.NORTH, sendButton);
layout.putConstraint(SpringLayout.WEST, messageBox, 5, SpringLayout.WEST, container);
layout.putConstraint(SpringLayout.SOUTH, messageBox, 5, SpringLayout.SOUTH, container);
layout.putConstraint(SpringLayout.EAST, messageBox, 5, SpringLayout.WEST, sendButton);
layout.putConstraint(SpringLayout.EAST, sendButton, 5, SpringLayout.EAST, container);
layout.putConstraint(SpringLayout.SOUTH, sendButton, 5, SpringLayout.SOUTH, container);
//container.add(messageBox);
//container.add(chatBox);
//container.add(sendButton);
}
public ChatMain()
{
JFrame frame = new JFrame("Chat");
setupGui(frame.getContentPane());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
chatBox.setEditable(false);
while(true)
{
chatBox.setText(chatBox.getText() + messageBox.getText());
messageBox.setText("");
try
{
Thread.sleep(5000);
}
catch (Exception e)
{
}
}
}
public static void main(String[] args)
{
new ChatMain();
}
}
答案 0 :(得分:0)
声明首选大小container.setPreferredSize(new Dimension(WIDTH, HEIGHT));
并不意味着布局知道容器的宽度和高度。实际上布局知道它们(宽度和高度),但它也想知道如何在窗口大小调整上表现。 SpringLayout喜欢忽略大小并且每次都可以做到。
您可能需要以下约束:
// set the borders of chatBox
layout.putConstraint(SpringLayout.NORTH, chatBox, 5, SpringLayout.NORTH, container);
layout.putConstraint(SpringLayout.WEST, chatBox, 5, SpringLayout.WEST, container);
layout.putConstraint(SpringLayout.EAST, chatBox, 5, SpringLayout.EAST, container);
// set the borders of messageBox
layout.putConstraint(SpringLayout.NORTH, messageBox, 5, SpringLayout.SOUTH, chatBox);
layout.putConstraint(SpringLayout.WEST, messageBox, 5, SpringLayout.WEST, container);
// set the borders of sendButton
layout.putConstraint(SpringLayout.NORTH, sendButton, 5, SpringLayout.SOUTH, chatBox);
layout.putConstraint(SpringLayout.WEST, sendButton, 5, SpringLayout.EAST, messageBox);
// the most importent part set the borders of container (you need only bottom-right)
layout.putConstraint(SpringLayout.EAST, container, 5, SpringLayout.EAST, sendButton);
layout.putConstraint(SpringLayout.SOUTH, container, 5, SpringLayout.SOUTH, sendButton);
程序中的主要问题是容器的边框不清晰,因此会出现奇怪的重叠。
对于更复杂的任务,您可以朝GridBagLayout方向看。