如何向 Swing 中的 JTextArea 添加垂直滚动条?

发布于 2024-12-04 16:19:45 字数 1001 浏览 1 评论 0原文

我有这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野却迷人 2024-12-11 16:19:45

JTextArea 实例设置为 JScrollPane 实例的视口视图,如 如何使用滚动窗格。将此滚动窗格添加到框架的内容窗格中。

Set the JTextArea instance as the viewport view of a JScrollPane instance, as shown in How to Use Scroll Panes. Add this scroll pane to the frame's content pane.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文