如何向 Swing 中的 JTextArea 添加垂直滚动条?
我有这个 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 的尺寸以便用户可以调整大小?
I have this Gui class:
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. Also, how can I lock the dimensions of a JFrame so users can adjest the size?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
JTextArea
实例设置为JScrollPane
实例的视口视图,如 如何使用滚动窗格。将此滚动窗格添加到框架的内容窗格中。Set the
JTextArea
instance as the viewport view of aJScrollPane
instance, as shown in How to Use Scroll Panes. Add this scroll pane to the frame's content pane.