Java JScrollPane混淆
因此,我试图将JScrollPane添加到JTextArea中,以便向下滚动,但无论我做什么,该窗格都不会显示。我是否违反了一些Swing规则,或者只是一个失败?此外,这是一个学校项目,所以我真的需要一个明确的答案尽快。谢谢 下面是实现JScrollPane的类的代码:Java JScrollPane混淆,java,swing,jscrollpane,jtextarea,Java,Swing,Jscrollpane,Jtextarea,因此,我试图将JScrollPane添加到JTextArea中,以便向下滚动,但无论我做什么,该窗格都不会显示。我是否违反了一些Swing规则,或者只是一个失败?此外,这是一个学校项目,所以我真的需要一个明确的答案尽快。谢谢 下面是实现JScrollPane的类的代码: package BLANK.historicalcontext; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import j
package BLANK.historicalcontext;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.WindowConstants;
import javax.swing.border.Border;
import BLANK.historicalcontext.gui.ConsoleJTextArea;
import BLANK.historicalcontext.keys.ConsoleKeyListener;
import BLANK.historicalcontext.story.Backstory;
import BLANK.historicalcontext.story.Story;
public class Window extends JFrame {
public static ConsoleJTextArea console = new ConsoleJTextArea();
public static JTextField inputField = new JTextField();
public static Story currentStory;
public static Font font = new Font("Avenir-Light", Font.PLAIN, 12);
private void addComponentsToPane() {
setLayout(null);
Border border = BorderFactory.createLineBorder(Color.BLACK);
inputField.addKeyListener(new ConsoleKeyListener());
console.setEditable(false);
console.setBackground(new Color(230, 230, 250));
console.setBorder(BorderFactory.createCompoundBorder(border, BorderFactory.createEmptyBorder(5, 5, 5, 5)));
console.setFont(font);
inputField.setFont(font);
Insets insets = getContentPane().getInsets();
JScrollPane consoleScrollPane = new JScrollPane(console);
consoleScrollPane.setPreferredSize(new Dimension(20 + insets.left, 800 - 20 + insets.top));
getContentPane().add(inputField);
getContentPane().add(consoleScrollPane);
console.setBounds(10 + insets.left, 10 + insets.top, 800 - 20, 600 - 65);
inputField.setBounds(7 + insets.left, (600 - 70) + 20 + insets.top, 800 - 14, 20);
}
private static void createAndShowGUI() {
Window frame = new Window("Historical Context Project");
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.addComponentsToPane();
Insets insets = frame.getInsets();
frame.setSize(800 + insets.left + insets.right, 600 + insets.top + insets.bottom);
frame.setVisible(true);
inputField.requestFocusInWindow();
}
public Window(String name) {
super(name);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
console.printToConsole("UnsupportedLookAndFeelException caught: \n" + ex.getStackTrace().toString() + "\n");
} catch (IllegalAccessException ex) {
console.printToConsole("IllegalAccessException caught: \n" + ex.getStackTrace().toString() + "\n");
} catch (InstantiationException ex) {
console.printToConsole("InstantiationException caught: \n" + ex.getStackTrace().toString() + "\n");
} catch (ClassNotFoundException ex) {
console.printToConsole("ClassNotFoundException caught: \n" + ex.getStackTrace().toString() + "\n");
}
UIManager.put("swing.boldMetal", Boolean.FALSE);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
console.append("Hello! This is a text-based game about the creation of the creation of Java.");
console.printToConsole("Your name is Patrick Naughton.");
console.printToConsole("Here is a bit of backstory:");
currentStory = new Backstory();
if(currentStory != null) {
currentStory.executeStory();
}
}
public static void failGame() {
console.printToConsole("You failed the game. This application will close once you click the \"Enter\"/\"Return\" key.");
}
}
我想您还希望看到控制台所基于的类:
package nate.historicalcontext.gui;
import javax.swing.JTextArea;
@SuppressWarnings("serial")
public class ConsoleJTextArea extends JTextArea {
public void printToConsole(String message) {
this.append("\n"+ message);
}
}
另外,请不要让我再问另一个问题,因为没有两个问题是完全相同的,我的情况可能与问题的作者不同
静态
<代码>静态不是跨对象通信的手段。提供允许您访问其他对象所需的对象属性的方法。尽量保护您的数据/字段null
布局。您无法控制渲染过程的许多方面,这些方面可能会更改单个组件的要求,并影响它们与其他组件的关系JScrollPane
之后,将控制台添加到contentPane
。组件只能驻留在单个容器中,当您将其添加到另一个容器中时,它将首先从当前容器中删除
JScrollPane consoleScrollPane = new JScrollPane(console);
//...
// Add the scroll pane...
getContentPane().add(consoleScrollPane);
// Add the console, which WAS on the scrollpane to the content pane
getContentPane().add(console);
使用null
布局无助于您
private void addComponentsToPane() {
JPanel panel = new JPanel();
panel.setBorder(new EmptyBorder(10, 10, 10, 10));
setContentPane(panel);
setLayout(new BorderLayout());
Border border = BorderFactory.createLineBorder(Color.BLACK);
// inputField.addKeyListener(new ConsoleKeyListener());
console.setEditable(false);
JScrollPane consoleScrollPane = new JScrollPane(console);
console.setBackground(new Color(230, 230, 250));
console.setBorder(BorderFactory.createCompoundBorder(border, BorderFactory.createEmptyBorder(5, 5, 5, 5)));
console.setFont(font);
inputField.setFont(font);
getContentPane().add(inputField, BorderLayout.SOUTH);
getContentPane().add(consoleScrollPane);
// getContentPane().add(console);
Insets insets1 = getContentPane().getInsets();
// console.setBounds(10 + insets1.left, 10 + insets1.top, 800 - 20, 600 - 65);
// inputField.setBounds(7 + insets1.left, (600 - 70) + 20 + insets1.top, 800 - 14, 20);
}
我是否仍然将
滚动窗格添加到内容窗格中
或否?首先,您需要执行getContentPane().add(控制台滚动窗格)代码>哪一个好,然后执行getContentPane().add(控制台)
这是坏的,所以我应该去掉getContentPane().add(控制台)代码>?因为控制台不会出现在屏幕上。同样,如果控制台不会出现在屏幕上,我建议这是您的null
布局的问题