图形用户界面
GUI组件
-
框架
-
容器
-
按钮
-
标签
-
文本框和密码框及文本区域
-
下拉式列表
-
复选框和单选按钮
案例1:框架、容器、按钮
import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; //继承顶级框架JFrame public class SimpleFrame extends JFrame { //初始化面板组件 JPanel panel=new JPanel(); //初始化图标 ImageIcon addIcon=new ImageIcon("src/add.gif"); ImageIcon deleteIcon=new ImageIcon("src/delete.gif"); //初始化按钮 JButton button=new JButton("button"); JButton addButton=new JButton("add",addIcon); JButton deleteButton=new JButton("delete", deleteIcon); public SimpleFrame(){ //带参数的JFrame构造方法 super("Frame title"); //把组件添加到面板容器panel中 panel.add(button); panel.add(addButton); panel.add(deleteButton); //把面板容器panel添加到框架中 this.add(panel); //设置框架大小 setSize(300,300); //关闭框架 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //依据组件大小自动调整框架大小 this.pack(); //设置框架显示 setVisible(true); } public static void main(String[] args) { new SimpleFrame(); } }
案例2:标签、文本框、文本区域
import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextArea; import javax.swing.JTextField; public class LabelText extends JFrame { JPanel panel=new JPanel(); //初始化标签 JLabel labelName=new JLabel("用户名:"); JLabel labelPassword=new JLabel("密 码:"); JLabel labelDescription=new JLabel("个人说明:"); //初始化文本框 JTextField textName=new JTextField(10); //初始化密码框 JPasswordField psk=new JPasswordField(10); //初始化文本区域 JTextArea areaDesc=new JTextArea(4, 15); public LabelText(){ //添加组件到面板容器panel中 panel.add(labelName); panel.add(textName); panel.add(labelPassword); panel.add(psk); panel.add(labelDescription); panel.add(areaDesc); this.setSize(200,200); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.add(panel); this.setVisible(true); } public static void main(String[] args) { new LabelText(); } }
案例3:滚动条、文本区域
import java.awt.Panel; import java.awt.TextArea; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; public class JScrollPaneDemo extends JFrame{ JScrollPane jsp=null; JPanel panel=null; JTextArea textArea=null; public JScrollPaneDemo(){ //初始化文本区域 textArea=new JTextArea(4, 10); //设置文本区域自动换行 textArea.setLineWrap(true); //为文本区域设置滚动条 jsp=new JScrollPane(textArea); //初始化面板及添加组件 panel=new JPanel(); panel.add(jsp); this.add(panel); this.setSize(200, 300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public static void main(String[] args) { new JScrollPaneDemo(); } }
案例4:复选框和单选框
import javax.swing.ButtonGroup; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JRadioButton; public class CheckRadioDemo extends JFrame{ JRadioButton[] teams=new JRadioButton[3]; ButtonGroup group=null; JPanel panel=null; public CheckRadioDemo(){ panel=new JPanel(); ImageIcon icon=new ImageIcon("add.gif"); JCheckBox checkBox1=new JCheckBox("增加"); JCheckBox checkBox2=new JCheckBox("删除"); ImageIcon icon1=new ImageIcon("view.gif"); JCheckBox checkBox3=new JCheckBox("查看"); panel.add(checkBox1); panel.add(checkBox2); panel.add(checkBox3); teams[0]=new JRadioButton("猪"); teams[1]=new JRadioButton("马"); teams[2]=new JRadioButton("牛"); group=new ButtonGroup(); for(JRadioButton jrb:teams){ group.add(jrb); panel.add(jrb); } this.add(panel); this.setSize(300, 300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public static void main(String[] args) { new CheckRadioDemo(); } }
案例5:下拉式列表
import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JScrollPane; /** * 组合框JcomboBox和列表JList的用法 * @author Administrator * */ public class ComDemo extends JFrame{ //组合框,下拉式菜单 Object[] items={"北京","上海","广州","深圳","重庆","成都"}; JComboBox jcb=new JComboBox(items); JPanel panel=new JPanel(); //列表 Object[] listData={"beijing","shanghai","guangzhou","shenzhen","chongqin","chengdu","hefei","zhenzhou","liaocheng","jinan","xizhang","lasa"}; JList list=new JList(listData); public ComDemo() { list.setVisibleRowCount(4); JScrollPane jsp=new JScrollPane(list); panel.add(jcb); panel.add(jsp); this.setSize(300,300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); this.add(panel); } public static void main(String[] args) { new ComDemo(); } }