2011-09-15 96 views
4

我有这个桂类:如何在Swing中将垂直滚动条添加到JTextArea?

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的尺寸以便用户能够识别尺寸?

回答

10

JTextArea实例设置为JScrollPane实例的视口视图,如How to Use Scroll Panes所示。将此滚动窗格添加到框架的内容窗格。

+2

你能告诉我代码是什么吗?我有点困惑。 –

+0

如果你在你的问题中提供了[sscce](http://sscce.org/),那么建议代码会更容易。更多的例子可以在这里找到(http://download.oracle.com/javase/tutorial/uiswing/components/scrollpane.html#eg)。 – trashgod

+0

*“你能告诉我代码是什么吗?”*你可以发布[SSCCE](http://pscode.org/sscce.html)吗? (即使那样,也没有提供任何保证)。 –

相关问题