简体   繁体   English

滚动条没有出现

[英]Scrollbar is not appearing

I have added some code for scrollbar which I got from questions asked by some other people on stackoverflow but I am not getting any scrollbar added to my JTextArea. 我为滚动条添加了一些代码,这些代码是从其他人对stackoverflow提出的问题中获得的,但是我没有将任何滚动条添加到我的JTextArea中。 I want to add scrollbar to JTextArea area2 in f2 frame. 我想在f2框架中将滚动条添加到JTextArea area2。

import javax.swing.*;
import java.io.*;
import java.awt.event.*;
public class TextAreaExample implements ActionListener {
    JFrame f1 = new JFrame("INPUT WINDOW");
    JFrame f2 = new JFrame("FILE DATA OUTPUT");
    JTextArea area1;
    JTextArea area2;
    JButton b;
    TextAreaExample() {
        area1 = new JTextArea();
        area2 = new JTextArea();
        JScrollPane scroll = new JScrollPane (area2, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        b = new JButton("click Me");
        b.setBounds(100, 95, 80, 30);
        f1.add(b);

        area1.setBounds(10, 30, 200, 60);
        area2.setBounds(5, 5, 480, 480);
        f1.add(area1);
        f2.add(area2);
        f2.add(scroll);
        f1.setSize(300,140);
        f2.setSize(510, 510);  
        f1.setLayout(null);
        f2.setLayout(null);
        f1.setVisible(true);
        f2.setVisible(true);
        b.addActionListener(this);
        f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == b) {
            String s1 = area1.getText();

            String s2 = "";
            try {
                FileInputStream fin = new FileInputStream(s1);
                BufferedInputStream bin = new BufferedInputStream(fin);
                int i;
                while((i = bin.read()) != -1) {
                     s2 = s2 + (char)i;
                }
                bin.close();
                fin.close();
            }catch(Exception a) {
                System.out.println(a);
            }
            area2.setText(s2);
        }
    }
    public static void main(String args[]) {  
        new TextAreaExample();

    }
}
    JScrollPane scroll = new JScrollPane (area2, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    b = new JButton("click Me");
    b.setBounds(100, 95, 80, 30);
    f1.add(b);

    area1.setBounds(10, 30, 200, 60);
    area2.setBounds(5, 5, 480, 480);
    f1.add(area1);
    f2.add(area2);

First you create a JScrollPane using the JTextArea as a parameter, which is correct. 首先,使用JTextArea作为参数创建一个JScrollPane,这是正确的。

But then you add the text area to the frame, which is incorrect. 但是,然后将文本区域添加到框架,这是不正确的。 Swing components can only have a single parent so the text area is removed from the scroll pane. Swing组件只能有一个父级,因此将从滚动窗格中删除该文本区域。

The scroll pane must be added to the frame. 滚动窗格必须添加到框架。

f1.add(scroll);

Also, get rid of all the null layouts and setBounds() statements. 同样,摆脱所有的null布局和setBounds()语句。 Swing was designed to be used with layout managers. Swing旨在与布局管理器一起使用。 Read the section from the Swing tutorial on Layout Manager for more information and examples to get your started. 阅读Layout Manager上Swing教程中的这一节,以获取更多信息和示例以开始使用。

Now when you create the text area you can use: 现在,当您创建文本区域时,可以使用:

JTextArea textArea = new JTextArea(5, 20);

to suggest the original size for the text area. 建议文本区域的原始大小。 Scrollbars will then appear when more than 5 lines of data is added. 添加超过5行数据时,滚动条将出现。

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

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