如何更改BoxLayout中元素的大小?
作者:互联网
我有这门课:
package com.erikbalen.game.rpg;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Gui extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = -384241835772507459L;
JLabel playerInfo;
JTextField textField;
private final static String newline = "\n";
JTextArea feed;
JScrollPane scrollPane;
Player player;
public Gui() {
super("Erik's RPG");
//setLayout(new FlowLayout());
Container contentPane = this.getContentPane();
contentPane.setLayout(new BoxLayout(contentPane,BoxLayout.PAGE_AXIS));
textField = new JTextField(30);
textField.addActionListener(this);
feed = new JTextArea(15, 30);
feed.setEditable(false);
scrollPane = new JScrollPane(feed);
}
当你运行它时,它会使textField真的很高,如果我扩展它,即使我只希望它是一定的高度.我怎么能够
解决方法:
BoxLayout扩展组件直到它们的最大大小,JTextField在返回Short.Max(或Integer.Max,忘记)的最大高度时行为异常.出路是让场地行为:
@Override
public Dimension getMaximumSize() {
// TODO Auto-generated method stub
Dimension dim = super.getMaximumSize();
dim.height = getPreferredSize().height;
return dim;
}
或者,使用另一个LayoutManager
标签:java,user-interface,swing,boxlayout 来源: https://codeday.me/bug/20190610/1209700.html