9.JRadioButton(单选按钮_实现多选)
基本思路: 1.删除分组的操作。
package KuangStudy; import javax.swing.*; import java.awt.*; import java.net.URL; public class first extends JFrame { public first(){ //框体的容器 Container container=this.getContentPane(); //单选框 JRadioButton jRadioButton_one=new JRadioButton("A 你是大傻瓜"); JRadioButton jRadioButton_two=new JRadioButton("B 我是大傻瓜"); JRadioButton jRadioButton_three=new JRadioButton("C 我俩都是大傻瓜"); /* //由于单选框只能选择一个,所以我们要对其分组 ButtonGroup buttonGroup_one=new ButtonGroup(); buttonGroup_one.add(jRadioButton_one); buttonGroup_one.add(jRadioButton_two); buttonGroup_one.add(jRadioButton_three); */ //添加到窗体中 container.add(jRadioButton_one,BorderLayout.NORTH); container.add(jRadioButton_two,BorderLayout.CENTER); container.add(jRadioButton_three,BorderLayout.SOUTH); setSize(500,500); setVisible(true); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setLocationRelativeTo(null); } }
package KuangStudy; public class Test { public static void main(String[] args) { first first_one=new first(); } }
10.JcheckBox (多选框)
基本思路: 1.记住多选框的类: JcheckBox()
package KuangStudy; import javax.swing.*; import java.awt.*; import java.net.URL; public class first extends JFrame { public first(){ //框体的容器 Container container=this.getContentPane(); //多选框 JCheckBox jCheckBox_one=new JCheckBox("A 谁是傻子?"); JCheckBox jCheckBox_two=new JCheckBox("B 吉士先生"); JCheckBox jCheckBox_three=new JCheckBox("C 李明"); //添加到窗体中去 container.add(jCheckBox_one,BorderLayout.NORTH); container.add(jCheckBox_two,BorderLayout.CENTER); container.add(jCheckBox_three,BorderLayout.SOUTH); //窗体显示 setSize(500,500); setVisible(true); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setLocationRelativeTo(null); } }
package KuangStudy; public class Test { public static void main(String[] args) { first first_one=new first(); } }
11.JComBox (下拉框)
基本思路: 1.理解 JComBox 类
package KuangStudy; import javax.swing.*; import java.awt.*; import java.net.URL; public class first extends JFrame { public first(){ //框体的容器 Container container=this.getContentPane(); //下拉框 JComboBox jComboBox_one=new JComboBox(); //下拉框添加 jComboBox_one.addItem(null); jComboBox_one.addItem("正字热映..."); jComboBox_one.addItem("已下架"); jComboBox_one.addItem("即将上映"); //添加到面板优化位置 JPanel jPanel=new JPanel(new GridLayout(10,1)); jPanel.add(jComboBox_one); pack(); //添加到窗体 container.add(jPanel); //窗体显示 setSize(500,500); setVisible(true); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setLocationRelativeTo(null); } }
package KuangStudy; public class Test { public static void main(String[] args) { first first_one=new first(); } }
12.JList (列表框)
基本思路: 1.了解 JList 2.了解 Vector 这个集合 3.JList 里面要添加集合/数组
package KuangStudy; import javax.swing.*; import java.awt.*; import java.net.URL; import java.util.Vector; public class first extends JFrame { public first(){ //框体的容器 Container container=this.getContentPane(); //列表框 Vector vector=new Vector(); JList jList_one=new JList(vector); vector.add(1); vector.add(1); vector.add(1); vector.add(1); //添加到窗体 container.add(jList_one); //窗体显示 setSize(500,500); setVisible(true); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setLocationRelativeTo(null); } }
package KuangStudy; public class Test { public static void main(String[] args) { first first_one=new first(); } }
13.JTextField (文本框)
基本思路: 1.JTextField (文本框) 2.
package KuangStudy; import javax.swing.*; import java.awt.*; import java.net.URL; import java.util.Vector; public class first extends JFrame { public first(){ //框体的容器 Container container=this.getContentPane(); JTextField jTextField_one=new JTextField("hello"); JTextField jTextField_two=new JTextField("world"); jTextField_two.setBounds(50,50,100,100); container.add(jTextField_two); container.add(jTextField_one,BorderLayout.CENTER); //窗体显示 setSize(500,500); setVisible(true); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setLocationRelativeTo(null); } }
package KuangStudy; public class Test { public static void main(String[] args) { first first_one=new first(); } }
14.JPasswordText (密码框)
基本思路: 1.JPasswordText 密码框加密
package KuangStudy; import javax.swing.*; import java.awt.*; import java.net.URL; import java.util.Vector; public class first extends JFrame { public first(){ //框体的容器 Container container=this.getContentPane(); JPasswordField jPasswordField_one=new JPasswordField("请输入密码"); JPasswordField jPasswordField_two=new JPasswordField("请输入您的账号"); container.add(jPasswordField_one,BorderLayout.NORTH); container.add(jPasswordField_two,BorderLayout.CENTER); //窗体显示 setSize(500,500); setVisible(true); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setLocationRelativeTo(null); } }
package KuangStudy; public class Test { public static void main(String[] args) { first first_one=new first(); } }
(三)、监听
ActionLitener、WindowListener=======> 监听按钮
KeyListener、MouserListener========>键盘 和鼠标()
WindowListener、KeyListener、MouserListene===》窗体
获取信息的时候是: e.getActionComent(); 尽量不用e.getSource();