java里带下滑的text,java-即使我有JScrollPane,当我输入文本时JText...

我有这个JTextArea,随着输入文本,它一直在扩展.我知道这看起来像是一个重复的问题,但是我已经遵循了答案,但它们对我没有用.我还附加了一个JScrollPane,但没有滚动条显示.我希望JTextArea保持恒定大小并使其可滚动.请看一下我的代码,并告诉我出了什么问题,因为我不知道!

这是我的代码:

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

line

line

line

line

line

line

line

line

line

line

line

line

line

line

line

line

line

line

line

line

line

line

line

line

line

line

line

line

line

line

line

line

line

line

line

line

line

line123");

//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() {

}

}

非常感谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值