gpt4 book ai didi

java - JScrollPane 困惑

转载 作者:行者123 更新时间:2023-12-02 04:42:43 24 4
gpt4 key购买 nike

因此,我尝试将 JScrollPane 添加到 JTextArea 中,以便可以向下滚动,但无论我做什么,该 Pane 都不会显示。我是否违反了某些 Swing 规则,或者只是失败了?另外,这是一个学校项目,所以我真的需要尽快得到明确的答案。谢谢!下面是实现 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.");
}

}

我想您还想查看控制台所基于的类:

package nate.historicalcontext.gui;

import javax.swing.JTextArea;

@SuppressWarnings("serial")
public class ConsoleJTextArea extends JTextArea {

public void printToConsole(String message) {
this.append("\n"+ message);
}

}

另外,请不要向我推荐另一个问题,因为没有两个问题是完全相似的,而且我的情况可能与该问题的作者不同。

最佳答案

  1. 过度使用静态static 不是跨对象通信的手段。提供允许您访问其他对象所需的对象属性的方法。尝试保护您的数据/字段
  2. null 布局。您无法控制渲染过程的许多方面,这些方面可能会更改各个组件的需求并影响它们与其他组件的关系。
  3. 在之前将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 布局对您没有帮助...

Layout

  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);
}

关于java - JScrollPane 困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29997022/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com