请教java中setUI的用法

 我来答
育知同创教育
2016-03-26 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
展开全部
java中setUI的用法:
语法:public void setUI(ComboBoxUI ui)

举例:

import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.plaf.ComboBoxUI;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicArrowButton;
import javax.swing.plaf.basic.BasicComboBoxUI;

public class Main {

public static void main(final String args[]) {
final String labels[] = { "A", "B", "C", "D", "E" };
JFrame frame = new JFrame("Popup JComboBox");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JComboBox comboBox = new JComboBox(labels);
comboBox.setUI((ComboBoxUI) MyComboBoxUI.createUI(comboBox));
frame.add(comboBox, BorderLayout.NORTH);

frame.setSize(300, 200);
frame.setVisible(true);

}

static class MyComboBoxUI extends BasicComboBoxUI {
public static ComponentUI createUI(JComponent c) {
return new MyComboBoxUI();
}

protected JButton createArrowButton() {
JButton button = new BasicArrowButton(BasicArrowButton.EAST);
return button;
}
}
}
匿名用户
推荐于2016-04-25
展开全部
两个类
运行的类UI1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

package com.monubia.ui;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Random;
public class UI1 extends JFrame {
private JPanel contentPane;
private JButton btnNewButton;
private JButton btnNewButton_1;
private JButton btnNewButton_2;
private JButton btnStart;
public static UI1 uiGet;
/**
* Launch the application.
*/
public static void main(String[] args) {

UI1 frame = new UI1();
frame.setTitle("");
frame.setVisible(true);
uiGet=frame;
}
/**
* Create the frame.
*/
public UI1() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

btnNewButton = new JButton("New button");
btnNewButton.setBounds(44, 28, 93, 23);
contentPane.add(btnNewButton);

btnNewButton_1 = new JButton("New button");
btnNewButton_1.setBounds(157, 28, 93, 23);
contentPane.add(btnNewButton_1);

btnNewButton_2 = new JButton("New button");
btnNewButton_2.setBounds(274, 28, 93, 23);
contentPane.add(btnNewButton_2);

btnStart = new JButton("Start");
btnStart.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
System.out.println("点击了鼠标");
new ThreadText().ThreadControlText();
}
});
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
btnStart.setBounds(100, 131, 93, 23);
contentPane.add(btnStart);

JButton btnNewButton_3 = new JButton("New button");
btnNewButton_3.setBounds(214, 131, 93, 23);
contentPane.add(btnNewButton_3);
}

public void setButtonText()
{
btnNewButton.setText(new Random().nextInt(100)+1+"");
btnNewButton_1.setText(new Random().nextInt(100)+1+"");
btnNewButton_2.setText(new Random().nextInt(100)+1+"");
}
}

线程类ThreadText

1
2
3

package com.monubia.ui;
public class ThreadText {

public void ThreadControlText()
{
Thread tct = new Thread()
{
public void run()
{
while(true)
{
UI1.uiGet.setButtonText();
}
}
};
tct.start();
}
}
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式