![](https://video.ask-data.xyz/img.php?b=https://iknow-pic.cdn.bcebos.com/d000baa1cd11728b793f3e9dc9fcc3cec3fd2c7a?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto)
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();
}
}
追问大哥,这是两道题,能分开说吗
追答楼主,已经分开了,还加了些功能。
![](https://video.ask-data.xyz/img.php?b=https://iknow-pic.cdn.bcebos.com/908fa0ec08fa513df2da057e3c6d55fbb2fbd91a?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto)