将滚动条添加到JFrame

Attilio

我在寻找一种在中添加滚动条的方法时遇到了一些问题JFrame我找到了一种重定向System.{in,out,err}的方法,JTextArea但添加滚动条失败。我希望这个问题不是多余的。

public class console extends JTextArea {    

 public static JTextArea console(final InputStream out, final PrintWriter in) {
    final JTextArea area = new JTextArea();
        new SwingWorker<Void, String>() {
        @Override protected Void doInBackground() throws Exception {
            Scanner s = new Scanner(out);
            while (s.hasNextLine()) publish(s.nextLine() + "\n");
            return null;
        }
        @Override protected void process(List<String> chunks) {
            for (String line : chunks) area.append(line);
        }
    }.execute();
    area.addKeyListener(new KeyAdapter() {
        private StringBuffer line = new StringBuffer();
        public void keyTyped(KeyEvent e) {
            char c = e.getKeyChar();
            if (c == KeyEvent.VK_ENTER) {
                in.println(line);
                line.setLength(0); 
            } else if (c == KeyEvent.VK_BACK_SPACE) { 
                line.setLength(line.length() - 1); 
            } else if (!Character.isISOControl(c)) {
                line.append(e.getKeyChar());
            }
        }
    });

    return area;
}
public static void main(String[] args) throws IOException {
    PipedInputStream inPipe = new PipedInputStream();
    PipedInputStream outPipe = new PipedInputStream();
    System.setIn(inPipe);
    System.setOut(new PrintStream(new PipedOutputStream(outPipe), true));

    PrintWriter inWriter = new PrintWriter(new PipedOutputStream(inPipe), true);

    JFrame frame = new JFrame("\"Console\"");

    frame.getContentPane().add(console(outPipe, inWriter));
    frame.setSize(500, 500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

   // my code
  }
亚辛·哈杰(Yassin Hajaj)

创建一个新JScrollPane对象并设置ScrollBarPolicies

如果您不想让滚动条始终显示,只需删除策略。

编辑

另外,不要在文本组件上使用KeyListener,而应结合使用DocumentListner和键绑定。(感谢@MadProgrammer)


解决方案

JScrollPane jsp = new JScrollPane(console(outPipe, inWriter));
jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
frame.getContentPane().add(jsp);

输出

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将滚动条添加到<textarea>

来自分类Dev

django-tables2:将滚动条添加到表中

来自分类Dev

将垂直滚动条添加到WPF网格

来自分类Dev

将Libgdx滚动条添加到TextArea

来自分类Dev

将QListView添加到QComboBox以正确显示滚动条

来自分类Dev

如何将垂直滚动条添加到Bootstrap工具提示中显示的图像?

来自分类Dev

将滚动条添加到Kivy中的Boxlayout中

来自分类Dev

JFrame添加滚动条

来自分类Dev

如何将垂直滚动条添加到gridview?

来自分类Dev

通过Ajax使用自定义滚动条将内容添加到元素

来自分类Dev

如何将滚动条添加到网格

来自分类Dev

将滚动条添加到表格并限制最多10个结果

来自分类Dev

将滚动条添加到Qwidget

来自分类Dev

将滚动条添加到不需要它的页面?

来自分类Dev

将滚动条添加到顶部按钮(Ionic 2 | Typescript)

来自分类Dev

如何将滚动条按钮添加到默认的肉桂主题?

来自分类Dev

将滚动条添加到div而不是滚动整个页面

来自分类Dev

将描边/轮廓添加到自定义滚动条

来自分类Dev

如何将滚动条添加到tinker-gui工具?

来自分类Dev

如何将滚动条添加到下拉菜单?

来自分类Dev

如何将鼠标滚轮滚动添加到垂直滚动条或滚动区域?

来自分类Dev

将水平填充添加到TextEditor,但防止其将滚动条向内移动

来自分类Dev

如何将蒙版渐变添加到水平滚动条

来自分类Dev

将垂直滚动条添加到JTextArea

来自分类Dev

将滚动条添加到FlowLayout

来自分类Dev

将滚动条添加到表

来自分类Dev

CSS将滚动条添加到模式窗口

来自分类Dev

JFrame添加滚动条

来自分类Dev

将滚动条添加到警报中