java_JFrame_demo

不要见笑,cs基本入行很少做

留个demo备忘

/*   
 * Copyright (c) 2014-2024 . All Rights Reserved.   
 *   
 * This software is the confidential and proprietary information of   
 * LoongTao. You shall not disclose such Confidential Information   
 * and shall use it only in accordance with the terms of the agreements   
 * you entered into with LoongTao.   
 *   
 */
package com.loongtao.main;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.**.utils.RegexUtils;

@SuppressWarnings("serial")
public class TestJFrame extends JFrame implements ActionListener {
    private static int x = 80;
    private static int y = 80;
    private static int w = 1000;
    private static int h = 750;

    private JLabel htmlLabel = new JLabel();
    private JLabel resultLabel = new JLabel();

    private JTextField urlField = new JTextField("", 82);
    private JTextField cssPathField = new JTextField("", 82);
    private JTextArea htmlArea = new JTextArea(20, 80);
    private JScrollPane htmlScrollPane = new JScrollPane(htmlArea);

    private JTextArea resultArea = new JTextArea(30, 80);
    private JScrollPane resultScrollPane = new JScrollPane(resultArea);

    private JButton jbGetSource = new JButton("转换");
    private JButton jbResetAll = new JButton("重置");

    public TestJFrame() {
        this.setTitle("转换工具 1.3 @email cphmvp@163.com");
        this.setLayout(new FlowLayout());

        // 设置标签
        this.add(htmlLabel);
        this.htmlLabel.setText("源文本 : ");
        // 设置文本域
        this.htmlArea.setAutoscrolls(false);
        this.htmlArea.setLineWrap(true);
        // 分别设置水平和垂直滚动条总是出现
        htmlScrollPane
                .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        htmlScrollPane
                .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        this.add(htmlScrollPane, BorderLayout.CENTER);
        // 设置大小不会无限下滑
        this.htmlScrollPane.setPreferredSize(new Dimension(900, 200));
        this.add(htmlScrollPane);

        // 设置标签
        this.add(resultLabel);
        this.resultLabel.setText("结果集 :");
        // 设置文本域
        this.resultArea.setAutoscrolls(false);
        this.resultArea.setLineWrap(true);
        // 分别设置水平和垂直滚动条总是出现
        resultScrollPane
                .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        resultScrollPane
                .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        this.add(resultScrollPane, BorderLayout.CENTER);
        // 设置大小不会无限下滑
        this.resultScrollPane.setPreferredSize(new Dimension(900, 250));
        this.add(resultScrollPane);

        this.add(jbGetSource);
        this.add(jbResetAll);

        jbGetSource.addActionListener(this);
        jbResetAll.addActionListener(this);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        TestJFrame t = new TestJFrame();
        // 1 x 2 y 3 宽、4 、高
        t.setBounds(x, y, w, h);
        t.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        JButton jb = (JButton) e.getSource();
        String html = "";
        String cssQuery = "";
        if (jb == jbGetSource) {
            dealGetSource(html, cssQuery);
        } else if (jb == jbResetAll) {
            urlField.setText("");
            cssPathField.setText("");
            htmlArea.setText("");
            resultArea.setText("");
        }
    }

    /**
     * @declare:处理得到源码操作
     * @param html
     * @param cssQuery
     * @author cphmvp
     */
    private void dealGetSource(String html, String cssQuery) {
        String txt = htmlArea.getText();
        htmlArea.setText(txt);
        List<String> htmls = RegexUtils.getStringList(txt, "\w+", 0);
        String result = htmls.toString();
        result = result.replace("[", "").replace("]", "");
        resultArea.setText(result);
    }
}
原文地址:https://www.cnblogs.com/cphmvp/p/4095183.html