我搜索了一半的互联网,我找不到任何有同样问题的人。
我尝试了几种不同的方法来添加垂直滚动条,但是徒劳无功。我见过的所有其他帖子都描述了这个问题:
JTextArea ta = new JTextArea();
JScrollPane sc = new JScrollPane(ta);
这似乎对他们有用。但我运气不好,至少尝试过三十种不同的方式。我是java的新手,而我所在的课程正在扼杀我。谢谢你的时间。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Gui extends JFrame
private JTextArea outputArea {
private JButton eastButton;
private JButton westButton;
private JButton northButton;
private JButton southButton;
private JButton helpButton;
private JButton pickupButton;
private JButton dropButton;
private JButton eatButton;
private JButton lookButton;
private JButton listButton;
private JScrollPane scroll;
Gui() {
int x = 0;
int y = 0;
int Width = 1;
int Height = 2;
int anchor;
GridBagConstraints gbc = new GridBagConstraints();
JLabel actionsLabel = null;
JLabel directionsLabel = null;
getContentPane().setBackground(Color.black);
setTitle("Castle Quest");
actionsLabel = new JLabel("Actions");
actionsLabel.setForeground(Color.red);
directionsLabel = new JLabel("Directions");
directionsLabel.setForeground(Color.red);
outputArea = new JTextArea(25, 35);
scroll = new JScrollPane(outputArea);
eastButton = new JButton("east");
eastButton.setSize(100, 30);
westButton = new JButton("west");
westButton.setSize(100, 30);
northButton = new JButton("north");
northButton.setSize(100, 30);
southButton = new JButton("south");
southButton.setSize(100, 30);
helpButton = new JButton("help");
helpButton.setSize(100, 30);
pickupButton = new JButton("pickup");
pickupButton.setSize(100, 30);
dropButton = new JButton("Drop");
dropButton.setSize(100, 30);
eatButton = new JButton("eat");
lookButton = new JButton("look");
lookButton.setSize(100, 30);
listButton = new JButton("list");
listButton.setSize(100, 30);
outputArea.setEditable(true);
GridBagLayout gbl = new GridBagLayout();
setLayout(gbl);
gbc.insets = new Insets(10, 10, 10, 10);
gbc.gridx = 1;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.gridheight = 2;
gbc.anchor = GridBagConstraints.NORTHWEST;
add(actionsLabel, gbc);
gbc.gridy += 2;
add(helpButton, gbc);
gbc.gridy += 2;
add(pickupButton, gbc);
gbc.gridy += 2;
add(dropButton, gbc);
gbc.gridy += 2;
add(eatButton, gbc);
gbc.gridy += 2;
add(lookButton, gbc);
gbc.gridy += 2;
add(listButton, gbc);
gbc.gridy = 1;
gbc.gridx = 2;
gbc.gridheight = 50;
outputArea.setBackground(Color.red);
add(outputArea, gbc);
gbc.gridheight = 2;
gbc.gridx = 3;
gbc.gridy = 1;
add(directionsLabel, gbc);
gbc.gridy = 3;
add(eastButton, gbc);
gbc.gridy += 2;
add(westButton, gbc);
gbc.gridy += 2;
add(northButton, gbc);
gbc.gridy += 2;
add(southButton, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
}
public static void main(String[] args) {
Gui newGame = new Gui();
Game funTime = new Game();
newGame.setSize(400, 150);
newGame.setBackground(Color.black);
newGame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
newGame.pack();
newGame.setVisible(true);
newGame.outputArea.setLineWrap(true);
newGame.outputArea.setWrapStyleWord(true);
newGame.outputArea.append(funTime.getMessage() + "\n");
newGame.eastButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
funTime.move("east");
newGame.outputArea.append(funTime.getMessage() + "\n");
}
});
newGame.westButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
funTime.move("west");
newGame.outputArea.append(funTime.getMessage() + "\n");
}
});
newGame.northButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
funTime.move("north");
newGame.outputArea.append(funTime.getMessage() + "\n");
}
});
newGame.southButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
funTime.move("south");
newGame.outputArea.append(funTime.getMessage() + "\n");
}
});
newGame.helpButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
funTime.getHelp();
newGame.outputArea.append(funTime.getMessage() + "\n");
}
});
newGame.pickupButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (funTime.currentLocation.hasItem()) {
funTime.pickupItem();
newGame.outputArea.append(funTime.getMessage() + "\n");
}
}
});
newGame.listButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < funTime.items.size(); i++) {
System.out.println(funTime.items.get(i));
}
System.out.println(funTime.items);
System.out.println(funTime.currentLocation.getRoomItem1() + " & " + funTime.currentLocation.getRoomItem2());
}
});
}
}
答案 0 :(得分:2)
您面临的问题(JTextArea
扩展)是由于您将JTextArea
添加到容器而不是JScrollPane
。
以下是你应该做的事情背后的逻辑:
JcxtArea包含在JScrollPane中,而JScrollPane包含在JFrame中。
更改此行
add(outputArea, gbc);
到这个
add(scroll, gbc);