简体   繁体   English

滚动文本窗格JFrame Java

[英]Scrolling Text Pane JFrame Java

I'm trying to create a sort of log of all the keys hit, at the moment I just need to figure out how to either: 我正在尝试创建所有已击键的日志,此刻我只需要弄清楚如何:

Link the position of the "text" to the scroll bar to the right 将“文本”的位置链接到右侧的滚动条

OR 要么

Add a different component which is suited better to hold large amounts of multiple line text. 添加一个不同的组件,该组件更适合于容纳大量的多行文本。

What am I doing wrong here? 我在这里做错了什么? Thanks! 谢谢!

public class MacroMakerGui extends JFrame {

public static final long serialVersionUID = 1L;
public static JPanel contentPane;
public static JTextField textField = new JTextField();;
public static MacroKeyListener keylistener = new MacroKeyListener(textField);

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                MacroMakerGui frame = new MacroMakerGui();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public MacroMakerGui() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 126, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(null);
    setContentPane(contentPane);

    JButton btnNewButton = new JButton("Record Macro");
    btnNewButton.setBounds(10, 220, 99, 30);

    contentPane.add(btnNewButton, null);

    textField.setBounds(10, 189, 99, 20);
    contentPane.add(textField);
    textField.setColumns(10);

    JEditorPane editorPane = new JEditorPane();
    editorPane.setBounds(10, 11, 84, 153);
    contentPane.add(editorPane);

    JScrollBar scrollBar = new JScrollBar();
    scrollBar.setBounds(93, 11, 17, 153);
    contentPane.add(scrollBar);

    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            btnNewButton.addKeyListener(keylistener);
        }
    });
}

} }

Instead of JScrollBar, use JScrollPanel. 代替JScrollBar,使用JScrollPanel。 Add that to the contentPane, and add your editorPane as a chiled of the JScrollPanel. 将其添加到contentPane,然后将您的editorPane添加为JScrollPanel的副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM