gpt4 book ai didi

Java终端-开发

转载 作者:行者123 更新时间:2023-12-02 05:50:51 34 4
gpt4 key购买 nike

请耐心等待。我正在制作一个 java 控制台,类似于此处找到的 DragonConsole https://code.google.com/p/dragonconsole/ 。一切都按计划进行,但我想实现 DragonConsole 中的一个功能。我想添加让垂直滚动条延伸到框架底部的功能,就像在 DC 中一样。

DragonConsole example

这是我的。

My console

正如你所看到的,垂直滚动条没有延伸到底部,在我看来,它看起来不专业。但我仍然是一个业余爱好者:D

以下是我的程序的组织方式:输出是 jScrollPane,输入是简单的 jTextField。用户输入内容,然后显示结果输出。

现在,我已经浏览了 DragonConsole 的源代码,它的方式对我来说很复杂,无法告诉我这个东西是如何工作的。据我所知,有一个 jTextArea,用户可以在其中输入命令,但我不知道垂直滚动条的功能如何。

有人可以帮我模仿 DragonConsole 的滚动条吗?

正如您所看到的,DC 上的输入随着垂直滚动条移动。我怎样才能做到这一点。

enter image description here

看不到输入区域...

<小时/>

“cls”命令调用此函数:

public void clear() {
console.setText("");
print(""); // printing will automatically print the '> ' at the beginning
startIndex = console.getText().length();
}

打印功能:

public void print(String s) { // prints output to the console
console.append(System.lineSeparator() + prompt + s);
}

产生错误:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: bad position: 110
at javax.swing.text.JTextComponent.setCaretPosition(JTextComponent.java:1678) at javax.swing.text.JTextComponent.setCaretPosition(JTextComponent.java:1678)
at Main.Terminal.caretUpdate(Terminal.java:198)
at javax.swing.text.JTextComponent.fireCaretUpdate(JTextComponent.java:407)
at javax.swing.text.JTextComponent$MutableCaretEvent.fire(JTextComponent.java:4417)
at javax.swing.text.JTextComponent$MutableCaretEvent.stateChanged(JTextComponent.java:4439)
at javax.swing.text.DefaultCaret.fireStateChanged(DefaultCaret.java:798)
at javax.swing.text.DefaultCaret.changeCaretPosition(DefaultCaret.java:1273)
at javax.swing.text.DefaultCaret.handleSetDot(DefaultCaret.java:1169)
at javax.swing.text.DefaultCaret.setDot(DefaultCaret.java:1150)
at javax.swing.text.DefaultCaret$Handler.removeUpdate(DefaultCaret.java:1796)
at javax.swing.text.AbstractDocument.fireRemoveUpdate(AbstractDocument.java:260)
at javax.swing.text.AbstractDocument.handleRemove(AbstractDocument.java:623)
at javax.swing.text.AbstractDocument.remove(AbstractDocument.java:591)
at javax.swing.text.AbstractDocument.replace(AbstractDocument.java:667)
at javax.swing.text.JTextComponent.setText(JTextComponent.java:1718)
at Main.Terminal.doCommand(Terminal.java:218)
at Main.Terminal.keyPressed(Terminal.java:168)
at java.awt.Component.processKeyEvent(Component.java:6463)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2829)
at java.awt.Component.processEvent(Component.java:6282)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1895)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:762)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1027)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:899)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:727)
at java.awt.Component.dispatchEventImpl(Component.java:4731)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.awt.EventQueue$3.run(EventQueue.java:686)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:702)
at java.awt.EventQueue$4.run(EventQueue.java:700)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

这似乎指出了一个问题:

@Override
public void caretUpdate(CaretEvent e) {
// Ensure that the caret position can only be a valid location
if (e.getDot() < startIndex) {
console.setCaretPosition(startIndex);
Toolkit.getDefaultToolkit().beep();
}
}
<小时/>

修复错误的代码

public void clear() {
startIndex = 0; // set it to zero to prevent error from happening
console.setText(""); // this will erase anything on screen, but start the input seqence on the next line
}

enter image description here

再次感谢您的帮助@amurka:)

最佳答案

查看DragonConsole源码,它支持两种模式:内联和非内联。您看到的第一张滚动条延伸到底部的图片是内联模式下的 DragonConsole。

在此模式下,它只是一个 JTextPane,然后将其添加到 JScrollPane。这就是滚动条延伸到底部的原因。然后,它向 JTextPane 添加一个 KeyListener(keyPressed 方法)和一个 CaretListener(caretUpdate 函数)。最后,它使用在 InputController 类中实现的自定义 DocumentFilter 来获取您看到的控制台行为。

总结一下:

  • 创建JTextPane。看看DragonConsole.java中的initializeConsole()
  • CaretListenerKeyListener 添加到该文本 Pane 。查看 DragonConsole.java 中的 caretUpdate()keyPressed()
  • 设置自定义DocumentFilter。看InputController.java(实际上设置文档样式是在inializeConsole方法中完成的。)

您的操作方式实际上与 DragonConsole 在不处于内联模式时的操作方式相同。在本例中,它使用 JTextPane 作为输出区域,使用 JTextArea 作为输入区域,并且在滚动条方面应该看起来相同。

编辑:这是一个 super 简单的内联控制台示例。

public class Console extends JPanel implements KeyListener, CaretListener {

private static final String PROMPT = ">>";

private JScrollPane scrollPane;
private JTextArea consoleTextPane;

private int startIndex;

public Console() {
super();

// Create a text area
consoleTextPane = new JTextArea();
consoleTextPane.setText(PROMPT);
consoleTextPane.setBorder(null);
// Wraps the text if it goes longer than a line, but NOT on word boundary
// like a normal console
consoleTextPane.setLineWrap(true);
consoleTextPane.setWrapStyleWord(false);

// Set the initial caret position
startIndex = consoleTextPane.getText().length();
consoleTextPane.setCaretPosition(startIndex);

// Add the caret and key listeners
consoleTextPane.addCaretListener(this);
consoleTextPane.addKeyListener(this);

// Scrollbar, always show the vertical one
scrollPane = new JScrollPane(consoleTextPane);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setBorder(null);

JPanel panelCenter = new JPanel(new BorderLayout());
panelCenter.setPreferredSize(new Dimension(400, 200));
panelCenter.add(scrollPane, BorderLayout.CENTER);

add(panelCenter, BorderLayout.CENTER);
}

public static void main(String[] args) {
JFrame frame = new JFrame("Console");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(new Console());
frame.pack();
frame.setVisible(true);
}

@Override
public void keyTyped(KeyEvent e) {
// All processing in keyPressed
}

@Override
public void keyReleased(KeyEvent e) {
// All processing in keyPressed
}

@Override
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode()) {
case KeyEvent.VK_ENTER:
// ENTER key was pressed
// Get the "Command"
String command = consoleTextPane.getText().substring(startIndex);

if (!command.isEmpty()) {
// TODO: do something with the command
consoleTextPane.append(System.lineSeparator()
+ "Command Entered: " + command);
}

// Update the start index and append a new prompt
consoleTextPane.append(System.lineSeparator() + PROMPT);
startIndex = consoleTextPane.getText().length();

// Consume the ENTER key event so further processing is not
// performed
e.consume();
break;
case KeyEvent.VK_BACK_SPACE:
// Make sure this is a valid delete
if (consoleTextPane.getCaretPosition() <= startIndex) {
e.consume();
Toolkit.getDefaultToolkit().beep();
}

break;
// TODO: add key presses here as desired
default:
//System.out.println("Unhandled: " + e.getKeyCode());
break;
}
}

@Override
public void caretUpdate(CaretEvent e) {
// Ensure that the caret position can only be a valid location
if (e.getDot() < startIndex) {
consoleTextPane.setCaretPosition(startIndex);
Toolkit.getDefaultToolkit().beep();
}
}
}

关于Java终端-开发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23554242/

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