我有这个Gui课程:
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());
textField = new JTextField(30);
textField.addActionListener(this);
feed = new JTextArea(15, 30);
feed.setEditable(false);
}
public void setCurrentPlayer(Player currentPlayer) {
player = currentPlayer;
playerInfo = new JLabel("Health = " + currentPlayer.getHealth() + " | Mana = " + player.getMana());
playerInfo.setBorder(BorderFactory.createTitledBorder(currentPlayer.getName()));
add(playerInfo);
add(feed);
add(textField);
}
当一堆文本在文本字段中时,它很烦人,因为它会使窗口变大。我需要知道如何制作滚动条,以便用户不必继续调整其JFrame的大小。另外,如何锁定JFrame的尺寸以便用户可以控制尺寸?
答案 0 :(得分:10)
将JTextArea
实例设置为JScrollPane
实例的视口视图,如How to Use Scroll Panes所示。将此滚动窗格添加到框架的内容窗格中。