无法将 JScrollPane 添加到 JTextArea
Can't add JScrollPane to JTextArea
我搜索了一半的互联网,但找不到遇到同样问题的人。
我尝试了几种不同的方法来添加垂直滚动条,但都没有用。我见过的每个 post 描述此问题的人都做了以下事情:
JTextArea ta = new JTextArea();
JScrollPane sc = new JScrollPane(ta);
这似乎对他们有用。但我运气不好,已经尝试了至少 30 种我发现的不同方法。我是 java 的新手,我所在的 class 快要死了。谢谢你的时间。
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());
}
});
}
}
您遇到的问题(JTextArea
扩展)是由于您将 JTextArea
添加到容器而不是 JScrollPane
。
以下是您应该做的事情背后的逻辑:
JTextArea 包含在 JScrollPane 中,JScrollPane 包含在 JFrame 中。
更改此行
add(outputArea, gbc);
至此
add(scroll, gbc);
我搜索了一半的互联网,但找不到遇到同样问题的人。
我尝试了几种不同的方法来添加垂直滚动条,但都没有用。我见过的每个 post 描述此问题的人都做了以下事情:
JTextArea ta = new JTextArea();
JScrollPane sc = new JScrollPane(ta);
这似乎对他们有用。但我运气不好,已经尝试了至少 30 种我发现的不同方法。我是 java 的新手,我所在的 class 快要死了。谢谢你的时间。
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());
}
});
}
}
您遇到的问题(JTextArea
扩展)是由于您将 JTextArea
添加到容器而不是 JScrollPane
。
以下是您应该做的事情背后的逻辑:
JTextArea 包含在 JScrollPane 中,JScrollPane 包含在 JFrame 中。
更改此行
add(outputArea, gbc);
至此
add(scroll, gbc);