滚动文本窗格JFrame Java

时间:2015-08-13 03:15:28

标签: java swing jpanel jbutton keylistener

我试图创建所有按键的日志记录,目前我只需要弄清楚如何:

链接"文字的位置"到右边的滚动条

OR

添加一个更适合保存大量多行文本的其他组件。

我在这里做错了什么?谢谢!

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

}

1 个答案:

答案 0 :(得分:0)

使用JScrollPanel而不是JScrollBar。将其添加到contentPane,并将您的editorPane添加为JScrollPanel的一部分。