简体   繁体   English

如何为我的JTextArea添加JScrollPane?

[英]How can I add a JScrollPane for my JTextArea?

In want to add a JScrollpane to my stopwatch program, so if the laptime become more and more the scrollpane should show up and allow the user to scroll. 要在我的秒表程序中添加一个JScrollpane,因此,如果转圈时间越来越长,则应该显示滚动窗格并允许用户滚动。 I tried to add the textArea which I want to have in the scrollpane, but it doesn't show a scrollpane. 我试图在滚动窗格中添加想要包含的textArea,但没有显示滚动窗格。

public class GUI_VA6 implements ActionListener {
    private JPanel pan;
    private JFrame frame;
    private JScrollPane vertical;
    private JButton Laptime;
    private JButton Start;
    private JButton Stop;
    private JLabel label;
    private JTextArea textArea;

    SimpleStopwatch simple = new SimpleStopwatch();
    SimpleStopwatch.StopwatchUpdateListener sss;
    double ghz;

    String lapTimeString;
    int countLap=1;


    public GUI_VA6() {

        frame = new JFrame();
        frame.setTitle("Stopwatch");

        label = new JLabel("Current time:");

        textArea = new JTextArea();
        pan = new JPanel();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Laptime = new JButton("Laptime");
        Laptime.setEnabled(false);
        Start = new JButton("Start");
        Stop = new JButton("Stop");
        Stop.setVisible(false);
        Start.setVisible(true);
        Laptime.addActionListener(this);
        Start.addActionListener(this);
        Stop.addActionListener(this);
        pan.add(Laptime);
        pan.add(Stop);
        pan.add(Start);
        frame.add(pan);

        vertical=new JScrollPane(textArea);
        vertical.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        frame.add(vertical);
        Container c;
        c = frame.getContentPane();
        c.setLayout(new java.awt.BorderLayout()); // immer so
        c.add(pan, BorderLayout.SOUTH);
        c.add(textArea, BorderLayout.CENTER);
        c.add(label, BorderLayout.NORTH);


        frame.setVisible(true);
        frame.setAlwaysOnTop(true);
        frame.setSize(500, 500);

You are adding your scrollpane to the container ( frame.add is equivalent to frame.getContentPane().add ), THEN changing the layout manager of the container, which isn't a good idea. 您正在将滚动窗格添加到容器中( frame.add等效于frame.getContentPane().add ),然后更改容器的布局管理器,这不是一个好主意。

Remove 去掉

frame.add(vertical);

And replace 并更换

c.add(textArea, BorderLayout.CENTER);

with

c.add(vertical, BorderLayout.CENTER);

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

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