这篇文章给大家演示一下Swing中的普通控件的用法,为了方面大家入门,我们的示例将会尽可能的简化。Swing中的普通控件包括JLabel(文本标签),JTextField(单行文本输入框), JPasswordField(密码输入框),JRadioButton(单选框),JCheckBox(复选框),JComboBox(下拉列表框),JList(普通列表框),JTextArea(多行文本输入框),JButton(普通按钮)。为了增加趣味性,也把JFileChooser和JColorChooser加上了。运行效果如下:
代码如下:
package org.leno.swing.demo1;
import java.awt.*;
import javax.swing.*;
/**
* @author leno
* @version 1.0
* 一个例子程序,让大家系统地了解Swing中一些普通控件的基本用法
* (http://blog.csdn.net/lenotang 转载请保留相关出处信息!)
*/
public class DemoPlainComponent {
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
//创建一个窗体
JFrame jFrame = new JFrame("示例普通Swing控件");
//设置窗体的位置和大小
jFrame.setLocation(100, 100);
jFrame.setSize(700, 600);
//设置手动布局,这样意味着放在jFrame内容面板上的组件都要指定自身的位置和大小,否则将不会显示。JFrame默认布局方式为BorderLayout
jFrame.setLayout(null);
//设置窗体的关闭行为
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//获得窗体的内容窗格
Container contentPane = jFrame.getContentPane();
//创建一个文本标签并指定自身在容器中的相对位置和大小
JLabel jLabel = new JLabel("姓名:");
jLabel.setBounds(20, 20, 30, 20);
JTextField jTextField = new JTextField("无名氏");
jTextField.setBounds(60, 20, 120, 20);
JLabel jLabel2 = new JLabel("密码:");
jLabel2.setBounds(190, 20, 30, 20);
JPasswordField jPasswordField = new JPasswordField("看不见");
jPasswordField.setBounds(230, 20, 120, 20);
JLabel jLabel3 = new JLabel("偶像:");
jLabel3.setBounds(360, 20, 30, 20);
Object[] stars = new Object[]{"刘德华","张学友","郭富城","黎明"};
JComboBox jComboBox = new JComboBox(stars);
jComboBox.setBounds(400, 20, 120, 20);
JLabel jLabel4 = new JLabel("性别:");
jLabel4.setBounds(20, 50, 30, 20);
ButtonGroup buttonGroup = new ButtonGroup();
JRadioButton jRadioButton1 = new JRadioButton("男",true);
jRadioButton1.setBounds(60, 50, 40, 20);
JRadioButton jRadioButton2 = new JRadioButton("女",false);
jRadioButton2.setBounds(60, 80, 40, 20);
buttonGroup.add(jRadioButton1);
buttonGroup.add(jRadioButton2);
JLabel jLabel5 = new JLabel("格言:");
jLabel5.setBounds(190, 50, 30, 20);
JTextArea jTextArea = new JTextArea(5,10);
jTextArea.setBorder(BorderFactory.createMatteBorder (2,2,2,2, Color.blue));
jTextArea.setLineWrap (true);//设置为禁止自动换行,初始值为false.
jTextArea.setWrapStyleWord (true);
jTextArea.setBackground (Color.white);//文本区背景
jTextArea.setForeground (Color.red);//字体颜色
jTextArea.setFont (new Font ("SansSerif", Font.PLAIN, 14));
jTextArea.setText("天道酬勤");
jTextArea.setBounds(230, 50, 120, 100);
JLabel jLabel6 = new JLabel("权限:");
jLabel6.setBounds(360, 50, 30, 20);
Object[] privs = new Object[]{"增加用户","删除用户","修改用户","预览用户"};
JList jList = new JList(privs);
jList.setBounds(400, 50, 120, 100);
JLabel jLabel7 = new JLabel("爱好:");
jLabel7.setBounds(20, 160, 30, 20);
JCheckBox jCheckBox = new JCheckBox("篮球");
jCheckBox.setBounds(60, 160, 60, 20);
JCheckBox jCheckBox2 = new JCheckBox("电影",true);
jCheckBox2.setBounds(120, 160, 60, 20);
JCheckBox jCheckBox3 = new JCheckBox("旅游");
jCheckBox3.setBounds(180, 160, 60, 20);
JLabel jLabel8 = new JLabel("颜色:");
jLabel8.setBounds(350, 160, 30, 20);
JColorChooser jColorChooser = new JColorChooser(Color.red);
jColorChooser.setBounds(390, 160, 280, 300);
JFileChooser jFileChooser = new JFileChooser();
jFileChooser.setBounds(10, 180, 360, 280);
ImageIcon icon = new ImageIcon(DemoPlainComponent.class.getResource("matthew.gif"));
JLabel jLabel9 = new JLabel(icon);
jLabel9.setBounds(10, 440, icon.getIconWidth(), icon.getIconHeight());
jLabel9.setToolTipText("好有趣的孩子啊");
JButton jButton = new JButton("登陆",icon);
jButton.setBounds(200, 460, 200, 80);
//将上面产生的控件放置在容器中
contentPane.add(jLabel);
contentPane.add(jTextField);
contentPane.add(jLabel2);
contentPane.add(jPasswordField);
contentPane.add(jLabel3);
contentPane.add(jComboBox);
contentPane.add(jLabel4);
contentPane.add(jRadioButton1);
contentPane.add(jRadioButton2);
contentPane.add(jLabel5);
contentPane.add(jTextArea);
contentPane.add(jLabel6);
contentPane.add(jList);
contentPane.add(jLabel7);
contentPane.add(jCheckBox);
contentPane.add(jCheckBox2);
contentPane.add(jCheckBox3);
contentPane.add(jLabel8);
contentPane.add(jColorChooser);
contentPane.add(jFileChooser);
contentPane.add(jLabel9);
contentPane.add(jButton);
//设置窗体可见
jFrame.setVisible(true);
}
}