简体   繁体   English

JScrollPane在JTextArea中代理

[英]JScrollPane acting wierd inside a JTextArea

Hi there fellow comrades! 嗨,同志们!
Thanks to you all I've managed to get my small experiment project going further but as you go further there are always obsticles so I again ran into one. 谢谢大家,我设法使我的小型实验项目更进一步,但是随着您的进一步发展,总会遇到麻烦,所以我再次遇到了麻烦。 I got a JTextArea outputting a list of running processes but JScrollPane isn't showing up but when I resize the window itself the JScrollPane hovers over the whole JTextArea. 我有一个JTextArea输出正在运行的进程的列表,但没有显示JScrollPane,但是当我调整窗口本身的大小时,JScrollPane会悬停在整个JTextArea上。
Looks like this: 看起来像这样: jscrollpane故障

After sever hours of scratching the back of my head I decided to ask you guys! 经过几个小时的挠挠,我决定问你们!
Code is: 代码是:

    import java.awt.BorderLayout;
    import java.awt.Component;
    import java.awt.Container;
    import java.awt.Font;
    import java.awt.GridBagLayout;
    import java.io.*;
    import javax.swing.*;

    public class JTask {

        JButton button = new JButton();
        JFrame frame = new JFrame();
        JTextArea area = new JTextArea();
        JScrollPane scrollPane = new JScrollPane();

        JTask() throws IOException{
            frame.setBounds(100, 100, 1000, 700);
            frame.setLayout(null);
            area.setFont(new Font("monospaced", Font.PLAIN, 14));
            area.setBounds(5,25,972,500);
            scrollPane.add(area);
            scrollPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,     JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
            scrollPane.setBounds(5,25,972,500);
            scrollPane.setViewportView(area);

            scrollPane.setEnabled(true);
            scrollPane.setVisible(true);
            scrollPane.repaint();
            scrollPane.revalidate();
            area.setVisible(true);  

            frame.add(scrollPane);
            frame.add(area);

            frame.setVisible(true);

            try {
                String line;
                Process p = Runtime.getRuntime().exec(System.getenv("windir") +"\\system32\\"+"tasklist.exe");
                BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
                while ((line = input.readLine()) != null) {
                    System.out.println(line);
                    area.append(line);
                    area.append("\n");
                }
                input.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        }

    public static void main(String args []) throws IOException{
        JTask task = new JTask();

    }
}

I apologize for a messy code and if anything could be done better but I hope you understand that I am just a beginner that is trying to understand the mechanics of Java programming language and sink in into the world of Java. 我为混乱的代码表示歉意,如果可以做得更好,但是我希望您理解我只是一个初学者,试图了解Java编程语言的原理并深入Java的世界。 Thank you all in advance! 谢谢大家!

Why are you using two JScrollPane ? 为什么要使用两个JScrollPane Simply add JScrollPane in JFrame and JTextArea in JScrollPane . 只需在JFrame添加JScrollPane并在JScrollPane添加JTextArea

sample code: 样例代码:

public JTask() throws IOException {
    JFrame  frame = new JFrame();
    JTextArea  area = new JTextArea();
    area.setFont(new Font("monospaced", Font.PLAIN, 14));
    JScrollPane  scrollPane = new JScrollPane(area);
    frame.add(scrollPane);
    ...
    // set text in JTextArea
    ...
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

It's worth reading Swing Tutorial on How to Use Text Areas to learn more and find more detail examples. 值得阅读有关如何使用文本区域的 Swing教程以了解更多信息并找到更多详细示例。

Some points 一些要点

You may try this code 您可以尝试此代码

JScrollPane scrollPane = new JScrollPane(area);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

not necessary to use view port in this case, the same is concerned to revalidation and repainting before frame is visible. 在这种情况下不必使用查看端口,在帧可见之前,重新验证和重新绘制也同样需要注意。

all you need is frame.getContentPane().add(scrollPane); 您只需要frame.getContentPane().add(scrollPane);

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

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