Java Swing 组件

本文最后更新于:2024年3月18日 凌晨

Java Swing 组件

窗体

JFrame

  • 与Frame类似,都是窗体容器。
1
2
3
4
public JFrame() throws HeadlessException {
super();
frameInit();
}

实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class JFrameTest {
public JFrameTest() {
JFrame jFrame = new JFrame("窗体名");
jFrame.setVisible(true);
jFrame.setBounds(100, 100, 200, 200);
// 关闭窗口的事件。
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// 设置背景颜色,需要先获得ContentPane
jFrame.getContentPane().setBackground(Color.BLACK);
}

public static void main(String[] args) {
JFrameTest jFrameTest = new JFrameTest();
}
}

JDialog

  • Swing的弹窗。
  • JDialog默认包含窗口关闭事件。
1
2
3
public JDialog() {
this((Frame)null, false);
}

实例

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
class Test {
public Test() {
JFrame jFrame = new JFrame();
jFrame.setVisible(true);
jFrame.setBounds(100, 100, 200, 200);
JButton jButton = new JButton("点击弹出对话框");
jFrame.add(jButton);
// 点击按钮,创建弹窗。
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new DialogTest();
}
});
}

// 自定义弹窗类。
class DialogTest extends JDialog {
public DialogTest() {
this.setVisible(true);
this.setBounds(200, 200, 500, 500);
}
}

public static void main(String[] args) {
Test Test = new Test();
}
}

JPanel

1
2
3
4
5
6
public JPanel(LayoutManager layout, boolean isDoubleBuffered) {
setLayout(layout);
setDoubleBuffered(isDoubleBuffered);
setUIProperty("opaque", Boolean.TRUE);
updateUI();
}

实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Test extends JFrame {

public Test() {
JPanel jPanel = new JPanel();
jPanel.add(new JButton("1"));
jPanel.add(new JButton("2"));
jPanel.add(new JButton("3"));
jPanel.setLayout(new GridLayout(1, 2, 10, 10));
this.setVisible(true);
this.setSize(400, 400);

getContentPane().add(jPanel);
}


public static void main(String[] args) {
new Test();
}
}

JScrollPane

  • 拥有滚动条的Panel
  • 当窗体大小小于内容时才会出现滚动条。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public JScrollPane(Component view, int vsbPolicy, int hsbPolicy)
{
setLayout(new ScrollPaneLayout.UIResource());
setVerticalScrollBarPolicy(vsbPolicy);
setHorizontalScrollBarPolicy(hsbPolicy);
setViewport(createViewport());
setVerticalScrollBar(createVerticalScrollBar());
setHorizontalScrollBar(createHorizontalScrollBar());
if (view != null) {
setViewportView(view);
}
setUIProperty("opaque",true);
updateUI();

if (!this.getComponentOrientation().isLeftToRight()) {
viewport.setViewPosition(new Point(Integer.MAX_VALUE, 0));
}
}

实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Test extends JFrame {

public Test() {
// 设置文本域并制定长宽。
JTextArea jTextArea = new JTextArea(20, 50);
jTextArea.setText("Content");
// 将文本域加入到JScrollPane中。
JScrollPane jScrollPane = new JScrollPane(jTextArea);

this.setVisible(true);
this.setSize(400, 400);
getContentPane().add(jScrollPane);
}


public static void main(String[] args) {
new Test();
}
}

按钮

JButton

1
2
3
4
5
6
7
public JButton(String text, Icon icon) {
// Create the model
setModel(new DefaultButtonModel());

// initialize
init(text, icon);
}

实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Test extends JFrame {

public Test() {
URL url = Test.class.getResource("1.png");
ImageIcon imageIcon = new ImageIcon(url);

// 初始化按钮。
JButton button = new JButton();
// 设置按钮图标。
button.setIcon(imageIcon);
// 设置提示文本。
button.setToolTipText("Tips");

getContentPane().add(button);
setVisible(true);
setBounds(100, 100, 200, 200);
}

public static void main(String[] args) {
new Test();
}
}

JRadioButton

  • 单选按钮,即同一个按钮组中只能有一个按钮被选择。
1
2
3
4
5
public JRadioButton (String text, Icon icon, boolean selected) {
super(text, icon, selected);
setBorderPainted(false);
setHorizontalAlignment(LEADING);
}

实例

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
class Test extends JFrame {

public Test() {
// 初始化单选按钮。
JRadioButton jButton1 = new JRadioButton("JButton1");
JRadioButton jButton2 = new JRadioButton("JButton2");
JRadioButton jButton3 = new JRadioButton("JButton3");
// 设置按钮组。
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(jButton1);
buttonGroup.add(jButton2);
buttonGroup.add(jButton3);
// 加入容器中。
getContentPane().add(jButton1, BorderLayout.CENTER);
getContentPane().add(jButton2, BorderLayout.NORTH);
getContentPane().add(jButton3, BorderLayout.SOUTH);
setVisible(true);
setBounds(100, 100, 200, 200);
}

public static void main(String[] args) {
new Test();
}
}

JCheckBox

  • 复选按钮即多选框。
1
2
3
4
5
public JCheckBox (String text, Icon icon, boolean selected) {
super(text, icon, selected);
setUIProperty("borderPainted", Boolean.FALSE);
setHorizontalAlignment(LEADING);
}

实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Test extends JFrame {

public Test() {
// 初始化复选按钮。
JCheckBox JCheckBox1 = new JCheckBox("JCheckBox1");
JCheckBox JCheckBox2 = new JCheckBox("JCheckBox2");
JCheckBox JCheckBox3 = new JCheckBox("JCheckBox3");
// 加入容器中。
getContentPane().add(JCheckBox1, BorderLayout.CENTER);
getContentPane().add(JCheckBox2, BorderLayout.NORTH);
getContentPane().add(JCheckBox3, BorderLayout.SOUTH);
setVisible(true);
setBounds(100, 100, 200, 200);
}

public static void main(String[] args) {
new Test();
}
}

图标

Icon

  • 图标可以放在label或者button上。
  • 初始化图标需要实现3个方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public interface Icon
{
/**
* Draw the icon at the specified location. Icon implementations
* may use the Component argument to get properties useful for
* painting, e.g. the foreground or background color.
*/
void paintIcon(Component c, Graphics g, int x, int y);

/**
* Returns the icon's width.
*
* @return an int specifying the fixed width of the icon.
*/
int getIconWidth();

/**
* Returns the icon's height.
*
* @return an int specifying the fixed height of the icon.
*/
int getIconHeight();
}

实例

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
class Test extends JFrame implements Icon {
private int height;
private int width;

public Test() {

}

public Test(int height, int width) {
this.height = height;
this.width = width;
}

public void init() {
Test iconTest = new Test(15, 15);
JLabel label = new JLabel("label", iconTest, SwingConstants.CENTER);
setBounds(100, 100, 200, 200);
getContentPane().add(label);
this.setVisible(true);
}

@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x, y, width, height);
}

@Override
public int getIconWidth() {
return this.width;
}

@Override
public int getIconHeight() {
return this.height;
}

public static void main(String[] args) {
new Test().init();
}
}

IconImage

1
2
3
4
5
6
7
8
9
public ImageIcon(URL location, String description) {
image = Toolkit.getDefaultToolkit().getImage(location);
if (image == null) {
return;
}
this.location = location;
this.description = description;
loadImage(image);
}

实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Test extends JFrame {
private int height;
private int width;

public Test() {
JLabel jLabel = new JLabel();
// 读取当前类目录下图片。
URL url = Test.class.getResource("1.png");
ImageIcon imageIcon = new ImageIcon(url,"pic");
// 设置标签的IconImage
jLabel.setIcon(imageIcon);
getContentPane().add(jLabel);
setVisible(true);
setBounds(100, 100, 200, 200);
}

public static void main(String[] args) {
new Test();
}
}

列表

JComboBox

  • 下拉框。
1
2
3
4
5
public JComboBox() {
super();
setModel(new DefaultComboBoxModel<E>());
init();
}

实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Test extends JFrame {

public Test() {
// 初始化下拉框。
JComboBox jComboBox = new JComboBox();
// 添加列表项。
jComboBox.addItem(null);
jComboBox.addItem("list1");
jComboBox.addItem("list2");
jComboBox.addItem("list3");
// 添加下拉框到容器中。
getContentPane().add(jComboBox);
setVisible(true);
setBounds(100, 100, 200, 200);
}

public static void main(String[] args) {
new Test();
}
}

JList

  • 列表。
1
2
3
4
5
6
7
8
public JList(final Vector<? extends E> listData) {
this (
new AbstractListModel<E>() {
public int getSize() { return listData.size(); }
public E getElementAt(int i) { return listData.elementAt(i); }
}
);
}

实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Test extends JFrame {

public Test() {
// 生成列表的内容。
String[] contents = {"1", "2", "3"};
Vector vector = new Vector();
vector.add("vector1");
vector.add("vector2");
vector.add("vector3");
// 初始化列表。
JList jList1 = new JList(contents);
JList jList2 = new JList(vector);
// 添加列表到容器中。
getContentPane().add(jList1, BorderLayout.SOUTH);
getContentPane().add(jList2, BorderLayout.NORTH);
setVisible(true);
setBounds(100, 100, 200, 200);
}

public static void main(String[] args) {
new Test();
}
}

文本

JLabel

  • Swing的标签,与Label类似,用于显示文本信息。
  • 可以在初始化JLabel的时候,同时指定horizontalAlignment
1
2
3
4
5
6
7
public JLabel(String text, Icon icon, int horizontalAlignment) {
setText(text);
setIcon(icon);
setHorizontalAlignment(horizontalAlignment);
updateUI();
setAlignmentX(LEFT_ALIGNMENT);
}

实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Test {
public Test() {
JFrame jFrame = new JFrame();
jFrame.setVisible(true);
jFrame.setBounds(100, 100, 200, 200);
// 新建标签。
JLabel jLabel = new JLabel("标签名");
// 将标签添加到窗体中。
jFrame.add(jLabel);
// 设置水平居中。
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
// 设置垂直居中。
jLabel.setVerticalAlignment(SwingConstants.CENTER);
}

public static void main(String[] args) {
JFrameTest jFrameTest = new JFrameTest();
}
}

JTextField

  • 文本框,用于单行输入文本。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public JTextField(Document doc, String text, int columns) {
if (columns < 0) {
throw new IllegalArgumentException("columns less than zero.");
}
visibility = new DefaultBoundedRangeModel();
visibility.addChangeListener(new ScrollRepainter());
this.columns = columns;
if (doc == null) {
doc = createDefaultModel();
}
setDocument(doc);
if (text != null) {
setText(text);
}
}

实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Test extends JFrame {

public Test() {
// 初始化文本框。
JTextField jTextField1 = new JTextField("Test1");
JTextField jTextField2 = new JTextField("Test2", 20);
// 添加文本框添加到容器中。
getContentPane().add(jTextField1, BorderLayout.SOUTH);
getContentPane().add(jTextField2, BorderLayout.NORTH);

setVisible(true);
setBounds(100, 100, 200, 200);
}

public static void main(String[] args) {
new Test();
}
}

JPasswordField

  • 密码框,用于输入密码,非明文显示。
1
2
3
4
5
6
7
public JPasswordField(Document doc, String txt, int columns) {
super(doc, txt, columns);
// We could either leave this on, which wouldn't be secure,
// or obscure the composted text, which essentially makes displaying
// it useless. Therefore, we turn off input methods.
enableInputMethods(false);
}

实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Test extends JFrame {

public Test() {
// 初始化密码框。
JPasswordField jPasswordField = new JPasswordField();
// 默认隐藏EchoChar为实心圆点,也可以指定为其他的字符。
jPasswordField.setEchoChar('*');
// 添加密码框添加到容器中。
getContentPane().add(jPasswordField);
setVisible(true);
setBounds(100, 100, 200, 200);
}

public static void main(String[] args) {
new Test();
}
}

JTextArea

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
public JTextArea(Document doc, String text, int rows, int columns) {
super();
this.rows = rows;
this.columns = columns;
if (doc == null) {
doc = createDefaultModel();
}
setDocument(doc);
if (text != null) {
setText(text);
select(0, 0);
}
if (rows < 0) {
throw new IllegalArgumentException("rows: " + rows);
}
if (columns < 0) {
throw new IllegalArgumentException("columns: " + columns);
}
LookAndFeel.installProperty(this,
"focusTraversalKeysForward",
JComponent.
getManagingFocusForwardTraversalKeys());
LookAndFeel.installProperty(this,
"focusTraversalKeysBackward",
JComponent.
getManagingFocusBackwardTraversalKeys());
}

实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Test extends JFrame {

public Test() {
// 设置文本域并制定长宽。
JTextArea jTextArea = new JTextArea(20, 50);
jTextArea.setText("Content");

getContentPane().add(jTextArea);
this.setVisible(true);
this.setSize(400, 400);
}


public static void main(String[] args) {
new Test();
}
}

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!