You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从Dashboard JFrame的TextArea读取数据到main类并执行任务?

解决方案

核心需求实现步骤

  • 从TextArea提取链接数据并按5行分组
  • 启动浏览器打开链接,通过Selenium实现元素等待与点击
  • 在独立线程中执行耗时操作,避免阻塞Swing事件调度线程(EDT)
  • 通过SwingUtilities.invokeLater同步更新表格UI

依赖准备

需要引入Selenium依赖(以Maven为例):

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>4.15.0</version>
</dependency>

修改后的Dashboard代码

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

public class Dashboard extends JFrame {
    private JTextArea textarea;
    private JLabel v1, v2, v3, v4, v5;
    private JLabel x1, x2, x3, x4, x5;
    private volatile boolean isRunning = true;

    public Dashboard() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 679, 500);
        JPanel contentPane = new JPanel();
        contentPane.setBackground(Color.BLACK);
        contentPane.setBorder(null);
        setContentPane(contentPane);
        contentPane.setLayout(null);

        // 初始化TextArea和滚动面板
        textarea = new JTextArea();
        textarea.setBackground(Color.WHITE);
        textarea.setFont(new Font("SansSerif", Font.PLAIN, 12));
        textarea.setBounds(0, 0, 250, 284);
        textarea.setBorder(null);

        JScrollPane scrollPane = new JScrollPane(textarea);
        scrollPane.setBounds(44, 69, 250, 284);

        JScrollBar scrollBar = new JScrollBar(JScrollBar.VERTICAL);
        scrollBar.setBounds(277, 69, 17, 284);
        scrollPane.setVerticalScrollBar(scrollBar);

        contentPane.add(scrollPane);

        // 初始化表格标签
        JPanel table = new JPanel();
        table.setBorder(new LineBorder(Color.GRAY, 2, true));
        table.setBackground(Color.BLACK);
        table.setBounds(307, 69, 350, 386);
        contentPane.add(table);
        table.setLayout(null);

        JPanel x = new JPanel();
        x.setBackground(Color.GRAY);
        x.setBounds(175, 0, 2, 386);
        table.add(x);

        JPanel xxx = new JPanel();
        xxx.setBackground(Color.GRAY);
        xxx.setBounds(0, 64, 350, 2);
        table.add(xxx);

        JPanel xxxx = new JPanel();
        xxxx.setBackground(Color.GRAY);
        xxxx.setBounds(0, 128, 350, 2);
        table.add(xxxx);

        JPanel xxxxx = new JPanel();
        xxxxx.setBackground(Color.GRAY);
        xxxxx.setBounds(0, 192, 350, 2);
        table.add(xxxxx);

        JPanel xxxxxx = new JPanel();
        xxxxxx.setBackground(Color.GRAY);
        xxxxxx.setBounds(0, 256, 350, 2);
        table.add(xxxxxx);

        JPanel xxxxxxx = new JPanel();
        xxxxxxx.setBackground(Color.GRAY);
        xxxxxxx.setBounds(0, 320, 350, 2);
        table.add(xxxxxxx);

        JLabel line = new JLabel("L I N E");
        line.setFont(new Font("SansSerif", Font.BOLD, 11));
        line.setForeground(Color.LIGHT_GRAY);
        line.setHorizontalAlignment(SwingConstants.CENTER);
        line.setBounds(0, 0, 175, 64);
        table.add(line);

        x1 = new JLabel("");
        x1.setLabelFor(line);
        x1.setFont(new Font("SansSerif", Font.BOLD, 12));
        x1.setForeground(Color.LIGHT_GRAY);
        x1.setHorizontalAlignment(SwingConstants.CENTER);
        x1.setBounds(0, 64, 175, 64);
        table.add(x1);

        x2 = new JLabel("");
        x2.setLabelFor(line);
        x2.setFont(new Font("SansSerif", Font.BOLD, 11));
        x2.setForeground(Color.LIGHT_GRAY);
        x2.setHorizontalAlignment(SwingConstants.CENTER);
        x2.setBounds(0, 128, 175, 64);
        table.add(x2);

        x3 = new JLabel("");
        x3.setLabelFor(line);
        x3.setForeground(Color.LIGHT_GRAY);
        x3.setFont(new Font("SansSerif", Font.BOLD, 11));
        x3.setHorizontalAlignment(SwingConstants.CENTER);
        x3.setBounds(0, 192, 175, 64);
        table.add(x3);

        x4 = new JLabel("");
        x4.setLabelFor(line);
        x4.setFont(new Font("SansSerif", Font.BOLD, 11));
        x4.setForeground(Color.LIGHT_GRAY);
        x4.setHorizontalAlignment(SwingConstants.CENTER);
        x4.setBounds(0, 256, 175, 64);
        table.add(x4);

        x5 = new JLabel("");
        x5.setLabelFor(line);
        x5.setFont(new Font("SansSerif", Font.BOLD, 11));
        x5.setForeground(Color.LIGHT_GRAY);
        x5.setHorizontalAlignment(SwingConstants.CENTER);
        x5.setBounds(0, 320, 175, 64);
        table.add(x5);

        JLabel link = new JLabel("L I N K");
        link.setHorizontalAlignment(SwingConstants.CENTER);
        link.setForeground(Color.LIGHT_GRAY);
        link.setFont(new Font("SansSerif", Font.BOLD, 11));
        link.setBounds(175, 0, 175, 64);
        table.add(link);

        v1 = new JLabel("");
        v1.setLabelFor(link);
        v1.setFont(new Font("SansSerif", Font.BOLD, 11));
        v1.setForeground(Color.LIGHT_GRAY);
        v1.setHorizontalAlignment(SwingConstants.CENTER);
        v1.setBounds(175, 64, 175, 64);
        table.add(v1);

        v2 = new JLabel("");
        v2.setLabelFor(link);
        v2.setFont(new Font("SansSerif", Font.BOLD, 11));
        v2.setForeground(Color.LIGHT_GRAY);
        v2.setHorizontalAlignment(SwingConstants.RIGHT);
        v2.setBounds(175, 128, 175, 64);
        table.add(v2);

        v3 = new JLabel("");
        v3.setLabelFor(link);
        v3.setFont(new Font("SansSerif", Font.BOLD, 11));
        v3.setForeground(Color.LIGHT_GRAY);
        v3.setHorizontalAlignment(SwingConstants.CENTER);
        v3.setBounds(175, 192, 175, 64);
        table.add(v3);

        v4 = new JLabel("");
        v4.setLabelFor(link);
        v4.setFont(new Font("SansSerif", Font.BOLD, 11));
        v4.setForeground(Color.LIGHT_GRAY);
        v4.setHorizontalAlignment(SwingConstants.CENTER);
        v4.setBounds(175, 256, 175, 64);
        table.add(v4);

        v5 = new JLabel("");
        v5.setLabelFor(link);
        v5.setFont(new Font("SansSerif", Font.BOLD, 11));
        v5.setForeground(Color.LIGHT_GRAY);
        v5.setHorizontalAlignment(SwingConstants.CENTER);
        v5.setBounds(175, 320, 175, 64);
        table.add(v5);

        // Start按钮
        JButton start = new JButton("S T A R T");
        start.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                isRunning = true;
                String text = textarea.getText();
                String[] lines = text.split("\\r?\\n");

                // 启动新线程执行耗时操作,避免阻塞EDT
                new Thread(() -> {
                    for (int i = 0; i < lines.length && isRunning; i += 5) {
                        List<String> list = new ArrayList<>();
                        for (int j = i; j < Math.min(i + 5, lines.length); j++) {
                            list.add(lines[j]);
                        }

                        String value1 = list.size() > 0 ? list.get(0) : "";
                        String value2 = list.size() > 1 ? list.get(1) : "";
                        String value3 = list.size() > 2 ? list.get(2) : "";
                        String value4 = list.size() > 3 ? list.get(3) : "";
                        String value5 = list.size() > 4 ? list.get(4) : "";

                        // 更新表格UI,必须在EDT执行
                        SwingUtilities.invokeLater(() -> {
                            x1.setText(String.valueOf(i/5 + 1));
                            x2.setText(String.valueOf(i/5 + 2));
                            x3.setText(String.valueOf(i/5 + 3));
                            x4.setText(String.valueOf(i/5 + 4));
                            x5.setText(String.valueOf(i/5 + 5));
                            v1.setText(value1);
                            v2.setText(value2);
                            v3.setText(value3);
                            v4.setText(value4);
                            v5.setText(value5);
                        });

                        // 处理每个链接的自动化操作
                        for (String url : list) {
                            if (!url.trim().isEmpty() && isRunning) {
                                Main.handleLink(url);
                            }
                        }
                    }
                }).start();
            }
        });
        start.setBackground(Color.BLACK);
        start.setForeground(Color.WHITE);
        start.setFont(new Font("SansSerif", Font.BOLD, 11));
        start.setBorder(new LineBorder(Color.WHITE, 1, true));
        start.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        start.setFocusPainted(false);
        start.setContentAreaFilled(false);
        start.setOpaque(true);
        start.setBounds(44, 364, 120, 40);
        contentPane.add(start);

        // Stop按钮
        JButton stop = new JButton("S T O P");
        stop.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                isRunning = false;
            }
        });
        stop.setBackground(Color.BLACK);
        stop.setForeground(Color.WHITE);
        stop.setFont(new Font("SansSerif", Font.BOLD, 11));
        stop.setFocusPainted(false);
        stop.setContentAreaFilled(false);
        stop.setOpaque(true);
        stop.setBorder(new LineBorder(Color.WHITE, 1, true));
        stop.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        stop.setBounds(174, 364, 120, 40);
        contentPane.add(stop);

        // Reset按钮
        JButton reset = new JButton("R E S E T");
        reset.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                isRunning = false;
                textarea.setText("");
                SwingUtilities.invokeLater(() -> {
                    x1.setText("");
                    x2.setText("");
                    x3.setText("");
                    x4.setText("");
                    x5.setText("");
                    v1.setText("");
                    v2.setText("");
                    v3.setText("");
                    v4.setText("");
                    v5.setText("");
                });
            }
        });
        reset.setBorder(new LineBorder(Color.WHITE, 1, true));
        reset.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        reset.setFocusPainted(false);
        reset.setContentAreaFilled(false);
        reset.setOpaque(true);
        reset.setBackground(Color.BLACK);
        reset.setForeground(Color.WHITE);
        reset.setFont(new Font("SansSerif", Font.BOLD, 11));
        reset.setBounds(44, 415, 250, 40);
        contentPane.add(reset);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new Dashboard().setVisible(true);
        });
    }
}

Main类实现

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

public class Main {
    public static void handleLink(String url) {
        WebDriver driver = new ChromeDriver();
        try {
            // 1. 打开链接
            driver.get(url);

            // 2. 等待第一个可点击元素并点击(示例选择器,需替换为实际元素选择器)
            WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
            WebElement firstElement = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.first-btn")));
            firstElement.click();

            // 3. 带try-catch的Thread.sleep
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                System.err.println("睡眠被中断: " + e.getMessage());
            }

            // 4. 等待第二个可点击元素并点击(示例选择器,需替换为实际元素选择器)
            WebElement secondElement = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.second-btn")));
            secondElement.click();

            // 可添加额外操作,如等待页面加载完成
            Thread.sleep(1000);
        } catch (Exception e) {
            System.err.println("处理链接时出错: " + url + ", 错误信息: " + e.getMessage());
        } finally {
            // 关闭浏览器
            driver.quit();
        }
    }
}

关键说明

  1. 线程处理:所有耗时的浏览器操作都放在独立线程中执行,避免阻塞Swing的EDT,防止界面卡顿。
  2. UI更新:通过SwingUtilities.invokeLater确保所有表格UI更新操作在EDT中执行,符合Swing的线程安全要求。
  3. 元素等待:使用Selenium的WebDriverWait结合ExpectedConditions.elementToBeClickable实现可靠的元素等待,比单纯的Thread.sleep更稳定。
  4. 停止控制:通过volatile boolean isRunning变量控制循环执行,实现Stop按钮的终止功能。

内容的提问来源于stack exchange,提问作者darwisht277

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 12:24:54