JScrollPane 混乱
JScrollPane Confusion
因此,我尝试将 JScrollPane 添加到 JTextArea,以便我可以向下滚动,但无论我做什么,该窗格都不会显示。我是否违反了某些 Swing 内容,或者只是失败了?另外,这是一个学校项目,所以我真的需要尽快得到明确的答案。谢谢!
这是 class 实现 JScrollPane 的代码:
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.");
}
}
我想您还希望看到控制台基于的 class:
package nate.historicalcontext.gui;
import javax.swing.JTextArea;
@SuppressWarnings("serial")
public class ConsoleJTextArea extends JTextArea {
public void printToConsole(String message) {
this.append("\n"+ message);
}
}
此外,请不要让我参考另一个问题,因为没有两个问题是完全相同的,我的情况可能与那个问题的作者不同。
- 过度使用
static
。 static
不是跨对象通信的方式。提供允许您访问其他对象所需的对象属性的方法。努力保护您的 data/fields
null
布局。您无法控制渲染过程的许多方面,这些方面可能会改变各个组件的要求并影响它们与其他组件的关系。
- 在
console
之前分配给 JScrollPane
之后将其添加到 contentPane
。一个组件只能驻留在一个容器中,当你将它添加到另一个容器时,它会先从当前容器中移除
仔细查看 How to use scrollpanes 了解更多详情
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);
}
因此,我尝试将 JScrollPane 添加到 JTextArea,以便我可以向下滚动,但无论我做什么,该窗格都不会显示。我是否违反了某些 Swing 内容,或者只是失败了?另外,这是一个学校项目,所以我真的需要尽快得到明确的答案。谢谢! 这是 class 实现 JScrollPane 的代码:
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.");
}
}
我想您还希望看到控制台基于的 class:
package nate.historicalcontext.gui;
import javax.swing.JTextArea;
@SuppressWarnings("serial")
public class ConsoleJTextArea extends JTextArea {
public void printToConsole(String message) {
this.append("\n"+ message);
}
}
此外,请不要让我参考另一个问题,因为没有两个问题是完全相同的,我的情况可能与那个问题的作者不同。
- 过度使用
static
。static
不是跨对象通信的方式。提供允许您访问其他对象所需的对象属性的方法。努力保护您的 data/fields null
布局。您无法控制渲染过程的许多方面,这些方面可能会改变各个组件的要求并影响它们与其他组件的关系。- 在
console
之前分配给JScrollPane
之后将其添加到contentPane
。一个组件只能驻留在一个容器中,当你将它添加到另一个容器时,它会先从当前容器中移除
仔细查看 How to use scrollpanes 了解更多详情
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);
}