一、常用类
JFrame:窗口
JLabel: 标签
JPanel:面板
JDialog:弹窗
JButton:按钮
Icon:图标
ImageIcon:图片JScrollPane:滚动面板
JTextArea:文本域
JRadioButton:单选框(使用ButtonGroup分组)
JCheckBox:多选框
JComboBox:下拉框
JList:列表框
JTextField:文本框
JPasswordField:密码框(使用setEchoChar替换字符)
二、使用示例
package com.SwingTest;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class JFrameTest {
public static void main(String[] args) {
new MYJFrame().init();
}
}
class MYJFrame extends JFrame{
MYJFrame frame = this;
void init(){
//窗口设置
setBounds(100,100,700,500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container contentPane = getContentPane();
contentPane.setBackground(Color.GRAY);
contentPane.setLayout(new GridLayout(3,4));
//创建组件:文本、ICON、图片、按钮
MyLabel label1 = new MyLabel("文本", null, SwingConstants.CENTER);
MyLabel label2 = new MyLabel("paint绘制Icon", new MyIcon(),SwingConstants.CENTER);
ImageIcon imageIcon = new ImageIcon(JFrameTest.class.getResource("龙眼.jpg"));
MyLabel label3 = new MyLabel("图片Icon",imageIcon,SwingConstants.CENTER);
Btn_Dialog btn1 = new Btn_Dialog("点击弹窗按钮");
JScrollPane panel1 = new JScrollPane(new JTextArea("滚动窗口",30,80));
//单选框
ButtonGroup group = new ButtonGroup();
JRadioButton btn2 = new JRadioButton("男");
JRadioButton btn3 = new JRadioButton("女");
group.add(btn2);
group.add(btn3);
JPanel panel2 = new JPanel();
panel2.add(new JLabel("单选框"));
panel2.add(btn2);
panel2.add(btn3);
//多选框
JPanel panel3 = new JPanel(new GridLayout(3,1));
for (int i = 1; i <= 3; i++) {
panel3.add(new JCheckBox("多选框" + i));
}
//下拉框
JComboBox comboBox = new JComboBox();
comboBox.addItem("(下拉框)");
comboBox.addItem("选项1");
comboBox.addItem("选项2");
comboBox.addItem("选项3");
//列表框
Vector<String> contents = new Vector<>();
JList list = new JList(contents);
contents.add("列表框1");
contents.add("列表框2");
contents.add("列表框3");
//密码框
JPanel panel4 = new JPanel(new GridLayout(2,1));
JPasswordField password = new JPasswordField();
password.setEchoChar('*');
panel4.add(new Label("密码框"));
panel4.add(password);
//插入组件
contentPane.add(label1);
contentPane.add(label2);
contentPane.add(label3);
contentPane.add(btn1);
contentPane.add(panel1);
contentPane.add(panel2);
contentPane.add(panel3);
contentPane.add(comboBox);
contentPane.add(list);
contentPane.add(panel4);
setVisible(true);
}
class MyLabel extends JLabel{
public MyLabel(String text, Icon icon, int horizontalAlignment) {
super(text, icon, horizontalAlignment);
}
}
class Btn_Dialog extends Button{
public Btn_Dialog(String label) throws HeadlessException {
super(label);
this.addActionListener((e)->{
new MyDialog();
});
}
}
class MyDialog extends JDialog{
public MyDialog() {
init();
}
void init(){
this.setVisible(true);
this.setBounds(100,100,800,600);
}
}
class MyIcon extends Container implements Icon{
private int width = 30;
private int height = 30;
public MyIcon() {
init();
}
void init(){
this.setBounds(60,60,width,height);
}
public MyIcon(int width, int height) {
this.width = width;
this.height = height;
init();
}
@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;
}
}
}