常 用 组 件
(1)JLabel(标签)组件
JLabel组件用来显示文本和图像,可以只显示其中的一者,也可以二者同时显示。
import java.awt.Font;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class JLabel_Example extends JFrame {
public static void main(String args[]) {
JLabel_Example frame = new JLabel_Example();
frame.setVisible(true);
}
public JLabel_Example() {
super("标签组件示例");
setBounds(100, 100, 500, 360);
getContentPane().setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JLabel label = new JLabel(); // 创建标签对象
label.setBounds(0, 0, 492, 341); // 设置标签的显示位置及大小
label.setText("欢迎进入Flora世界!"); // 设置标签显示文字
label.setFont(new Font("", Font.BOLD, 22)); // 设置文字的字体及大小
label.setHorizontalAlignment(JLabel.CENTER); // 设置标签内容居中显示
label.setIcon(new ImageIcon("C:\\Users\\acer\\Pictures\\Camera Roll\\5bafa40f4bfbfbed29dd64c572f0f736aec31f87.jpg")); // 设置标签显示图片
label.setHorizontalTextPosition(JLabel.CENTER); // 设置文字相对图片在水平方向的显示位置
label.setVerticalTextPosition(JLabel.BOTTOM); // 设置文字相对图片在垂直方向的显示位置
getContentPane().add(label); // 将标签添加到窗体中
}
}
显示效果:
(2)JButton(按钮)组件
import java.awt.Insets;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
public class JButton_Example extends JFrame {
public static void main(String args[]) {
JButton_Example frame = new JButton_Example();
frame.setVisible(true);
}
public JButton_Example() {
super("按钮组件示例");
setBounds(100, 100, 180, 200);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JButton button = new JButton(); // 创建按钮对象
button.setMargin(new Insets(0, 0, 0, 0)); // 设置按钮边框和标签之间的间隔
button.setContentAreaFilled(false); // 设置不绘制按钮的内容区域,即设置按钮的背景为透明
button.setBorderPainted(false); // 设置不绘制按钮的边框
button.setIcon(new ImageIcon("C:\\\\Users\\\\acer\\\\Pictures\\\\Camera Roll\\\\5bafa40f4bfbfbed29dd64c572f0f736aec31f87.jpg")); // 设置默认情况下按钮显示的图片
button.setRolloverIcon(new ImageIcon("C:\\Users\\acer\\Pictures\\Camera Roll\\11.jpg")); // 设置光标经过时显示的图片
button.setPressedIcon(new ImageIcon("C:\\Users\\acer\\Pictures\\Saved Pictures\\12.jpg"));// 设置按钮被按下时显示的图片
button.setBounds(30, 30, 100, 100); // 设置标签的显示位置及大小
getContentPane().add(button); // 将按钮添加到窗体中
}
}
显示效果:
(3)JRadioButton(单选按钮)组件
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
public class JRadioButton_Example extends JFrame {
public static void main(String args[]) {
JRadioButton_Example frame = new JRadioButton_Example();
frame.setVisible(true);
}
public JRadioButton_Example() {
super();
setTitle("单选按钮组件示例");
setBounds(100, 100, 300, 100);
getContentPane().setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建标签对象
final JLabel label = new JLabel();
label.setText("性别:"); // 设置标签文本
label.setBounds(10, 10, 46, 15); // 设置标签的显示位置及大小
add(label); // 将标签添加到窗体中
// 创建按钮组对象
ButtonGroup buttonGroup = new ButtonGroup();
// 创建单选按钮对象
final JRadioButton manRadioButton = new JRadioButton();
buttonGroup.add(manRadioButton); // 将单选按钮添加到按钮组中
manRadioButton.setSelected(true); // 设置单选按钮默认为被选中
manRadioButton.setText("男"); // 设置单选按钮的文本
manRadioButton.setBounds(62, 6, 46, 23); // 设置单选按钮的显示位置及大小
add(manRadioButton); // 将单选按钮添加到窗体中
final JRadioButton womanRadioButton = new JRadioButton();
buttonGroup.add(womanRadioButton);
womanRadioButton.setText("女");
womanRadioButton.setBounds(114, 6, 46, 23);
add(womanRadioButton);
}
}
显示效果:
(4)JCheckBox(复选框)组件
核心代码:
final JCheckBox readingCheckBox = new JCheckBox(); // 创建复选框对象
readingCheckBox.setText("读书"); // 设置复选框的标签文本
readingCheckBox.setBounds(62, 6, 55, 23); // 设置复选框的显示位置及大小
add(readingCheckBox); // 将复选框添加到窗体中
(5)JComboBox(选择框)组件
JComboBox组件实现一个选择框,用户可以从下拉列表中选择相应的值,该选择框还可以设置为可编辑的,当设置为可编辑状态时,用户可以在选择框中输入相应的值。JComboBox类提供的常用方法如下表所示。
核心代码:
String[] schoolAges = { "本科", "硕士", "博士" }; // 创建选项数组
JComboBox comboBox = new JComboBox(schoolAges); // 创建选择框对象
comboBox.setEditable(true); // 设置选择框为可编辑
comboBox.setMaximumRowCount(3); // 设置选择框弹出时显示选项的最多行数
comboBox.insertItemAt("大专", 0); // 在索引为0的位置插入一个选项
comboBox.setSelectedItem("本科"); // 设置"本科"选项被选中
comboBox.setBounds(62, 7, 104, 21); // 设置选择框的显示位置及大小
add(comboBox); // 将选择框添加到窗体中
显示效果:
(6)JList(列表框)组件
JList组件实现一个列表框,列表框与选择框的主要区别是列表框可以多选,而选择框只能单选。常用方法如图所示:
- setSelectionMode(int selectionMode)方法设置列表框的选取模式,该方法的参数可以从ListSelectionModel类中的静态常量中选择。这三种选取模式包括一种单选模式和两种多选模式,具体参数信息如下表所示。
- 如果按住Ctrl键,可以选中所有点击的元素;如果选中一个元素,按住Shift键并单击另一个元素,那么这两个元素之间的所有元素都被选中;要从选中的元素中去掉一个,按住Ctrl键单击该元素即可。
核心代码:
final JLabel label = new JLabel(); // 创建标签对象
label.setText("爱好:"); // 设置标签文本
label.setBounds(10, 10, 46, 15); // 设置标签的显示位置及大小
getContentPane().add(label); // 将标签添加到窗体中
String[] likes = { "读书", "听音乐", "跑步", "乒乓球", "篮球", "游泳", "滑雪" };
JList list = new JList(likes); // 创建列表对象
list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); //只允许连续选取多个
list.setFixedCellHeight(20); // 设置选项高度
list.setVisibleRowCount(4); // 设置选项可见个数
JScrollPane scrollPane = new JScrollPane(); // 创建滚动面板对象
scrollPane.setViewportView(list); // 将列表添加到滚动面板中
scrollPane.setBounds(62, 5, 100, 100); // 设置滚动面板的显示位置及大小
getContentPane().add(scrollPane); // 将滚动面板添加到窗体中
显示效果:
(7)JTextField(文本框)组件
JTextField组件实现一个文本框,用来接受用户输入的单行文本信息。常用方法如下图:
核心代码:
JTextField textField = new JTextField(); // 创建文本框对象
textField.setHorizontalAlignment(JTextField.CENTER); // 设置文本框内容的水平对齐方式
textField.setFont(new Font("", Font.BOLD, 12)); // 设置文本框内容的字体样式
textField.setBounds(62, 7, 120, 21); // 设置文本框的显示位置及大小
getContentPane().add(textField); // 将文本框添加到窗体中
(8)JPasswordField(密码框)组件
JPasswordField组件实现一个密码框,用来接受用户输入的单行文本信息,在密码框中并不显示用户输入的真实信息,而是通过显示一个指定的回显字符作为占位符。新创建密码框的默认回显字符为“*”。常用方法如下图所示:
(9)JTextArea(文本域)组件
JTextArea组件实现一个文本域,文本域可以接受用户输入的多行文本。
核心代码:
final JLabel label = new JLabel();
label.setText("备注:");
label.setBounds(10, 10, 46, 15);
getContentPane().add(label);
JTextArea textArea = new JTextArea(); // 创建文本域对象
textArea.setColumns(15); // 设置文本域显示文字的列数
textArea.setRows(3); // 设置文本域显示文字的行数
textArea.setLineWrap(true); // 设置文本域自动换行
final JScrollPane scrollPane = new JScrollPane(); // 创建滚动面板对象
scrollPane.setViewportView(textArea); // 将文本域添加到滚动面板中
Dimension dime = textArea.getPreferredSize(); // 获得文本域的首选大小
scrollPane.setBounds(62, 5, dime.width, dime.height); // 设置滚动面板的位置及大小
getContentPane().add(scrollPane); // 将滚动面板添加到窗体中