簡體   English   中英

如何為我的JTextArea添加JScrollPane?

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

要在我的秒表程序中添加一個JScrollpane,因此,如果轉圈時間越來越長,則應該顯示滾動窗格並允許用戶滾動。 我試圖在滾動窗格中添加想要包含的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);

您正在將滾動窗格添加到容器中( frame.add等效於frame.getContentPane().add ),然后更改容器的布局管理器,這不是一個好主意。

去掉

frame.add(vertical);

並更換

c.add(textArea, BorderLayout.CENTER);

c.add(vertical, BorderLayout.CENTER);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM