[英]How to a add a vertical scroll bar to a JTextArea in Swing?
I have this Gui class: 我有这个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);
}
And it is annoying when a bunch of text is in the text field because it keeps making the window larger. 当一堆文本在文本字段中时,它很烦人,因为它会使窗口变大。 I need to know how to make a scrollbar so the user doesn't have to keep resizing his JFrame. 我需要知道如何制作滚动条,以便用户不必继续调整其JFrame的大小。 Also, how can I lock the dimensions of a JFrame so users can adjest the size? 另外,如何锁定JFrame的尺寸以便用户可以调整尺寸?
Set the JTextArea
instance as the viewport view of a JScrollPane
instance, as shown in How to Use Scroll Panes . 将JTextArea
实例设置为JScrollPane
实例的视口视图,如如何使用滚动窗格中所示 。 Add this scroll pane to the frame's content pane. 将此滚动窗格添加到框架的内容窗格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.