简体   繁体   English

即使我有JScrollPane,当我输入文本时,JTextArea也会扩展,但是滚动条也不会显示

[英]JTextArea expands as I enter text even though I have a JScrollPane, but the scrollbar doesn't show up either

I have this JTextArea and it keeps expanding as text is entered. 我有这个JTextArea,随着输入文本,它一直在扩展。 I know this looks like a repeat question but I've followed the answers and they haven't worked for me. 我知道这看起来像是一个重复的问题,但是我已经遵循了答案,但它们对我没有用。 I also have a JScrollPane attached to it but no scrollbar is showing up. 我还附加了一个JScrollPane,但没有滚动条显示。 I want the JTextArea to remain a constant size and to have it scrollable. 我希望JTextArea保持恒定大小并使其可滚动。 Please take a look at my code and tell me what's wrong, because I have no idea! 请看一下我的代码,并告诉我出了什么问题,因为我不知道!

Here is my code: 这是我的代码:

package com.robot;

import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.*;

public class GUI extends JFrame implements Runnable {

//start of the constructor method for GUI
public GUI() {

    //defines the line break
    String newline = System.getProperty("line.separator");

    //defines objects
    Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension dim = tk.getScreenSize();

    //sets the size of the GUI
    this.setSize(600, 400);

    //centers the GUI
    int xPos = (dim.width / 2) - (this.getWidth() /2);
    int yPos = (dim.height / 2) - (this.getHeight() /2);

    this.setLocation(xPos, yPos);

    //makes the program unable to be resized
    this.setResizable(false);

    //allows the user to close the program with the x button
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //sets the title of the program
    this.setTitle("ROBOT Alpha Alfred Version 3.0");

    //creates panels to hold the elements of the GUI
    JPanel mainPanel = new JPanel();
    JPanel buttonPanel = new JPanel();
    JPanel consolePanel = new JPanel();

    //creates buttons
    JButton runDemo = new JButton("Run Demo");
    JButton runLive = new JButton("Run Live");
    JButton scan = new JButton("Scan Market");
    JButton findPatterns = new JButton("Find Patterns");
    JButton cleanFolder = new JButton("Clean Up Folder");
    JButton configureSettings = new JButton("Configure Settings");

    //creates the console
    JTextArea console = new JTextArea(6, 40);

    //sets the default text of the console
    console.setText("----------------------- ROBOT Console -----------------------" + newline);

    //makes the console unable to be edited
    console.setEditable(false);
    console.append("3sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs22323sfasfs\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline123");

    //sets the line wrapping of the console
    console.setLineWrap(true);
    console.setWrapStyleWord(true);
    JScrollPane scrollBar = new JScrollPane(console);
    scrollBar.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);

    //adds buttons to the buttonPanel
    buttonPanel.add(runDemo);
    buttonPanel.add(runLive);
    buttonPanel.add(scan);
    buttonPanel.add(findPatterns);
    buttonPanel.add(cleanFolder);
    buttonPanel.add(configureSettings);

    //adds the console to the console panel
    consolePanel.add(console);
    consolePanel.add(scrollBar);

    //adds panels to the main panel
    mainPanel.add(buttonPanel);
    mainPanel.add(consolePanel);

    //adds the main panel to the frame
    this.add(mainPanel);

    //sets the GUI to be visible
    this.setVisible(true);

}

public void run() {

}

}

Thank you so much! 非常感谢!

Your problem occurs here... 您的问题出现在这里...

consolePanel.add(console);
consolePanel.add(scrollBar);

Basically a component can only belong to a single parent, by adding the console to the consolePanel you are removing it from the scroll pane. 基本上,一个组件只能属于一个父组件,方法是将console添加到consolePanel中,然后将其从滚动窗格中删除。

The console is already contained within a container, so you simply only need to add the parent container (the scroll pane) to consolePane ... console已经包含在容器中,因此您只需要将父容器(滚动窗格)添加到consolePane ...

//consolePanel.add(console);
consolePanel.add(scrollBar);

On a side note, you should be careful of Tookit#getScreenSize is it returns the "whole" screen and not the viewable screen size (that area which the application can safely use). 附带说明一下,您应该注意Tookit#getScreenSize因为它返回的是“整个”屏幕,而不是可见的屏幕尺寸(应用程序可以安全使用的区域)。 So instead of ... 所以代替...

Toolkit tk = Toolkit.getDefaultToolkit();
Dimension dim = tk.getScreenSize();

//sets the size of the GUI
this.setSize(600, 400);

//centers the GUI
int xPos = (dim.width / 2) - (this.getWidth() /2);
int yPos = (dim.height / 2) - (this.getHeight() /2);

this.setLocation(xPos, yPos);

You could use setLocationRelativeTo(null) and achieve a better result 您可以使用setLocationRelativeTo(null)并获得更好的结果

I would also suggest sizing the component after you have added the content and before you make it visible, for example... 我还建议您在添加内容之后并且在使其可见之前调整组件的大小,例如...

this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);

This ensures that the window takes into consideration in difference between different platforms more accurately... 这样可以确保窗口更准确地考虑到不同平台之间的差异...

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

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