哪位大神能帮忙解答一下,感激不尽,要求用java编写

如题所述

import java.awt.Color;
import java.awt.Container;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextPane;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

public class TestFrame extends JFrame implements Runnable {

    private static final long serialVersionUID = 1L;
    private JMenuBar bar = new JMenuBar();
    private JMenu file = new JMenu("文件");
    private JMenu edit = new JMenu("编辑");
    private JMenu help = new JMenu("帮助");
    private JMenuItem it1 = new JMenuItem("打开");
    private JLabel select = new JLabel("请选择");
    private JComboBox box = new JComboBox();
    private JButton search = new JButton("查找");
    private JButton replace = new JButton("替换");
    private JTextPane pane = new JTextPane();

    public TestFrame() {
        super("A Test");
        try {
            init();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void init() {

        /*组合框设置*/
        box.addItem("小学");
        box.addItem("中学");
        box.addItem("大学");

        /*大小位置设置*/
        select.setBounds(180, 50, 50, 22);
        box.setBounds(225, 50, 60, 22);
        search.setBounds(130, 250, 60, 22);
        replace.setBounds(195, 250, 60, 22);
        pane.setBounds(20, 80, 350, 150);

        /*添加组件*/
        Container c = getContentPane();
        c.setLayout(null);
        c.add(select);
        c.add(box);
        c.add(search);
        c.add(replace);
        c.add(pane);

        /*菜单栏设置*/
        bar.add(file);
        bar.add(edit);
        bar.add(help);
        file.add(it1);
        this.setJMenuBar(bar);

        /*基本设置*/
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(400, 350);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    public void run() {
        try {
            while (true) {
                Thread.sleep(500);
                System.out.println("come on!!!");
                Random random = new Random(System.currentTimeMillis());
                SimpleAttributeSet attrSet = new SimpleAttributeSet();
                StyleConstants
                        .setForeground(attrSet, new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)));
                StyleConstants.setBold(attrSet, random.nextBoolean());
                StyleConstants.setFontSize(attrSet, random.nextInt(50) + 10);
                Document doc = pane.getDocument();
                String str = "Hello World";
                doc.remove(0, doc.getLength());
                doc.insertString(doc.getLength(), str, attrSet);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new Thread(new TestFrame()).start();
    }

}

追问

大哥,这是两道题,能分开说吗

追答

楼主,已经分开了,还加了些功能。


温馨提示:答案为网友推荐,仅供参考
相似回答