使JScrollPane控制多个组件 [英] Make JScrollPane control multiple components

查看:87
本文介绍了使JScrollPane控制多个组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的应用程序,我正在设计一个脚本编辑器。目前我有一个 JPanel ,其中包含另一个 JPanel ,其中包含行号(位于左侧),以及a JTextArea ,用于允许用户在(位于右侧)键入其代码。

For my application I am designing a script editor. At the moment I have a JPanel which contains another JPanel that holds the line number (positioned to the left), and a JTextArea which is used to allow users to type their code in (positioned to the right).

目前,我已经在 JTextArea JScrollPane $ c>允许用户滚动他们的代码。

At the moment, I have implemented a JScrollPane on the JTextArea to allow the user to scroll through their code.

对于包含行号的 JPanel ,他们每次用户按下回车键时都会递增。

For the JPanel containing the line numbers, they increment every time the user presses the enter key.

然而,问题是我需要相同的JScrollPane(在 JTextArea上实现的那个) / code>)也可以控制行号JPanel的滚动;即,当用户在JTextArea上滚动时,行号JPanel也应该滚动。但由于行号保存在JPanel中,我无法将该组件添加到JTextArea。

However, the problem is that I want the same JScrollPane (the one implemented on the JTextArea) to control the scrolling of the line number JPanel as well; i.e. when the user scrolls on the JTextArea, the line number JPanel should also scroll. But since the line numbers are held in a JPanel, I cant add that component to a JTextArea.

包含JTextArea和行号JPanel的JPanel类的构造函数:

The constructor for the JPanel class containing the JTextArea and the line number JPanel:

private ScriptEditor() {

    setBackground(Color.WHITE);

    lineNumPanel = new LineNumberPanel();

    scriptArea = new JTextArea();
    scriptArea.setLineWrap(true);
    scriptArea.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 15));
    scriptArea.setMargin(new Insets(3, 10, 0, 10));

    JScrollPane scrollPane = new JScrollPane(scriptArea);
    scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    scrollPane.setPreferredSize(new Dimension(width, height));

    scriptArea.addKeyListener(this);

    add(lineNumPanel);
    add(scrollPane);
}

行号JPanel的构造函数,它在自身中添加JLabel来表示行数字:

The constructor for the line number JPanel which adds JLabels within itself to represent the line numbers:

public LineNumberPanel() {

    setPreferredSize(new Dimension(width, height));

    box = Box.createVerticalBox();
    add(box);

    //setup the label
    label = new JLabel(String.valueOf(lineCount));
    label.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 15));

    //setup the label alignment
    label.setVerticalAlignment(JLabel.TOP);
    label.setHorizontalAlignment(JLabel.CENTER);
    label.setVerticalTextPosition(JLabel.TOP);
    setAlignmentY(TOP_ALIGNMENT);

    box.add(label);
}


推荐答案

创建一个外部面板行号面板和文本区域。

Create an Outer Panel which holds the Line Number panel and Text Area.

然后将这个新面板放入滚动窗格中,以便最终得到这种安排:

Then put this new panel into the Scroll Pane so you end up with this arrangement:

代码中的内容类似于:

private ScriptEditor() {

    setBackground(Color.WHITE);

    JPanel outerPanel = new JPanel();

    lineNumPanel = new LineNumberPanel();

    scriptArea = new JTextArea();
    scriptArea.setLineWrap(true);
    scriptArea.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 15));
    scriptArea.setMargin(new Insets(3, 10, 0, 10));

    outerPanel.add(lineNumPanel, BorderLayout.WEST)
    outerPanel.add(scriptArea, BorderLayout.CENTER)

    JScrollPane scrollPane = new JScrollPane(outerPanel);
    scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    scrollPane.setPreferredSize(new Dimension(width, height));

    scriptArea.addKeyListener(this);

    add(lineNumPanel);
    add(scrollPane);
}

这篇关于使JScrollPane控制多个组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆