java - 滚动条 JTextarea 不工作

标签 java swing jframe jtextarea setbounds

由于某种原因,我的滚动条出现了,但它不起作用。应该发生的是使用滚动条滚动文本区域的文本。有人可以解释一下为什么这不起作用吗?

import java.io.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class App extends JFrame{
    private JPanel paneel;

    public App(){   
        paneel = new AppPaneel();
        setContentPane(paneel);
    }

    public static void main(String args[]){
        JFrame frame = new App();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.setTitle("Auto Clicker");
        frame.setVisible(true);
    }
}

class AppPaneel extends JPanel{
    private JTextField delayField, xLocation, yLocation;
    private JTextArea listArea;
    private JButton addButton, saveButton, runButton;
    private JScrollPane scroll;

    public AppPaneel(){
        setLayout(null);

        delayField = new JTextField();
        delayField.setBounds(10, 10, 85, 25);
        delayField.setText("delay in ms"); 

        xLocation = new JTextField();
        xLocation.setBounds(105, 10, 85, 25);
        xLocation.setText("X position"); 

        yLocation = new JTextField();
        yLocation.setBounds(200, 10, 85, 25);
        yLocation.setText("Y position");

        addButton = new JButton("Add");
        addButton.setBounds(295, 10, 75, 24);
        addButton.addActionListener(new AddHandler());

        listArea = new JTextArea();
        listArea.setBounds(10, 45, 360, 180);
        scroll = new JScrollPane(listArea);
        scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scroll.setBounds(370, 45, 20, 180);

        saveButton = new JButton("Save");
        saveButton.setBounds(10, 230, 85, 24);

        runButton = new JButton("Run (F1)");
        runButton.setBounds(105, 230, 85, 24);
        runButton.addActionListener(new RunHandler());

        add(delayField);
        add(xLocation);
        add(yLocation);
        add(addButton);
        add(listArea);
        add(saveButton);
        add(runButton);
        add(scroll);
    }

    class AddHandler implements ActionListener{
        public void actionPerformed(ActionEvent a){
            listArea.setText(listArea.getText() + delayField.getText() + ",     " + xLocation.getText() + ", " + yLocation.getText() + ", " + "click;" + "\n");
        }
    }

    class RunHandler implements ActionListener{
        private Robot bot;
        private String text;
        int foo = Integer.parseInt("1234");
        public void actionPerformed(ActionEvent b) {
            try{
                text  = listArea.getText();
                bot = new Robot();
                for(String line : text.split("\\n")){
                int delay = Integer.parseInt((line.substring(0, 4)));
                int xpos = Integer.parseInt((line.substring(6, 10)));
                int ypos = Integer.parseInt((line.substring(12, 16)));
                bot.mouseMove(xpos, ypos);
                Thread.sleep(delay);
                bot.mousePress(InputEvent.BUTTON1_MASK);
                bot.mouseRelease(InputEvent.BUTTON1_MASK);
                }
            } 
            catch (AWTException | InterruptedException e){
                e.printStackTrace();
            }
        }
    }
}

最佳答案

不要使用 null 布局和 setBounds,因为它会弄乱你的程序。我们一次又一次地告诉人们不要这样做,这是有原因的——通过设置 JTextArea 的边界,您可以限制其大小,以便它在需要时不会增长。与往常一样,解决方案是学习并使用布局管理器。设置 JTextArea 的列和行属性,但不设置其边界、大小或首选大小。

接下来不要在 Swing 应用程序中执行以下操作:Thread.sleep(delay);,因为它会使整个应用程序进入休眠状态。对于任何延迟,请使用 Swing Timer 代替。这些教程可以帮助您使用它。

对于非功能性布局示例:

import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.*;

@SuppressWarnings("serial")
public class App2 extends JPanel {
    private static final int GAP = 5;
    private JTextField textField1 = new JTextField(GAP);
    private JTextField textField2 = new JTextField(GAP);
    private JTextField textField3 = new JTextField(GAP);
    private JTextArea textArea = new JTextArea(15, 30);

    public App2() {
        JPanel topPanel = new JPanel(new GridLayout(1, 0, GAP, GAP));
        topPanel.add(textField1);
        topPanel.add(textField2);
        topPanel.add(textField3);
        topPanel.add(new JButton("Add"));

        textArea.setWrapStyleWord(true);
        textArea.setLineWrap(true);
        JScrollPane scrollPane = new JScrollPane(textArea);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        JPanel bottomPanel = new JPanel(new GridLayout(1, 0, GAP, GAP));
        bottomPanel.add(new JButton("Save"));
        bottomPanel.add(new JButton("Run (F1)"));
        bottomPanel.add(new JLabel(""));
        bottomPanel.add(new JLabel(""));

        setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
        setLayout(new BorderLayout(GAP, GAP));
        add(topPanel, BorderLayout.PAGE_START);
        add(scrollPane, BorderLayout.CENTER);
        add(bottomPanel, BorderLayout.PAGE_END);
    }

    private static void createAndShowGui() {
        JFrame frame = new JFrame("Auto Clicker");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new App2());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}

关于java - 滚动条 JTextarea 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34932983/

相关文章:

java - 正在将 Java 项目导出为可运行的 Jar : not doing anything

java - 如何更改 Maven2 中的 JUnit 转换

java图像加载与效果

java - 我 setEnabled(false) 后复选框没有被禁用

java - 嵌入式 Neo4j SPI 类 (lucene PostingsFormat) 错误

java - 组合框的 ActionListener 仅适用于声明后的第一列

java - 如何在 Java 中设置文本框的高度和宽度?

Java 在资源文件夹上创建一个空的 .ser 文件(如果尚不存在)

java - 数据添加到数据库后如何清除JFrame中选定的用户输入组件

java - 如何在 Scala 中获取异常的堆栈跟踪以打印它?