按钮是java中图形界面中最基本的组件之一,经常用到的按钮有四种形式,我们就一一来介绍; 一、普通按钮; JButton,JButton是最普通的按钮,在前两篇文章中也总是用到了它; 之后我们用例子来看看;
public class Buttonexample extends JFrame{ private JButton b1=new JButton(); private JButton b2=new JButton("按钮"); public Buttonexample() { this.setLayout(new FlowLayout()); this.add(b1); this.add(b2); this.setSize(300,100); this.setVisible(true); } }这里就不多说了,因为很简单,看不懂代码的可以看看前面的文章
二、切换按钮 JToggleButton,关于他的使用我也不多说; 直接看代码,和上一个用法基本一样;
public class Buttonexample extends JFrame{ private JToggleButton b1=new JToggleButton("按钮"); private JToggleButton b2=new JToggleButton("按钮"); private JToggleButton b3=new JToggleButton("按钮"); public Buttonexample() { this.setLayout(new FlowLayout()); this.add(b1); this.add(b2); this.add(b3); this.setSize(300,100); this.setVisible(true); }为什么叫切换按钮呢,因为按钮本身有两种状态; 选中状态和非选中状态; 看一下他是什么样子; 能看得出第二个按钮点击一下就被选中了吧;
三、复选按钮
JCheckBox;依旧还是看代码;
public class Buttonexample extends JFrame{ private JCheckBox b1=new JCheckBox("按钮1"); private JCheckBox b2=new JCheckBox("按钮2"); private JCheckBox b3=new JCheckBox("按钮3"); public Buttonexample() { this.setLayout(new FlowLayout()); this.add(b1); this.add(b2); this.add(b3); this.setSize(300,100); this.setVisible(true); } }然后我们来看一下运行效果; 这个按钮也是有选中状态和非选中状态的; 因为一般用于多选所以叫复选按钮;
四、单选按钮
JRadioButton,来看代码;
public class Buttonexample extends JFrame{ private JRadioButton b1=new JRadioButton("按钮1"); private JRadioButton b2=new JRadioButton("按钮2"); private JRadioButton b3=new JRadioButton("按钮3"); public Buttonexample() { this.setLayout(new FlowLayout()); this.add(b1); this.add(b2); this.add(b3); this.setSize(300,100); this.setVisible(true); } }看一下运行效果;
同样有着选中和非选中两种状态; 因为一般用于单选所以叫单选按钮,常和按钮组一起用(一会会说);
总结了这四个常用的按钮之后; 我们可以看一下这些按钮可用的常用方法;
一、addActionLListener()
这个方法四个按钮都可以用,是按钮的点击事件; 在上一篇也说过; 例如我给上一个例子中添加一下;
public class Buttonexample extends JFrame{ private JRadioButton b1=new JRadioButton("按钮1"); private JRadioButton b2=new JRadioButton("按钮2"); private JRadioButton b3=new JRadioButton("按钮3"); public Buttonexample() { b1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub System.out.println("点击了按钮"); } }); this.setLayout(new FlowLayout()); this.add(b1); this.add(b2); this.add(b3); this.setSize(300,100); this.setVisible(true); } }由于上一篇已经说过就不再看运行结果了;
二、isSelected()
后三种按钮都有两种状态,所以我们有个函数可以来判断按钮的状态; 这个函数返回值为true时按钮为选中状态; 反之为非选中状态; 例如,我把上一个例子中的一行代码改一下;
public class Buttonexample extends JFrame{ private JRadioButton b1=new JRadioButton("按钮1"); private JRadioButton b2=new JRadioButton("按钮2"); private JRadioButton b3=new JRadioButton("按钮3"); public Buttonexample() { b1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub System.out.println(b1.isSelected());//修改的地方 } }); this.setLayout(new FlowLayout()); this.add(b1); this.add(b2); this.add(b3); this.setSize(300,100); this.setVisible(true); } }这回我们运行; 我一直点击按钮1,就会有不同的控制台输出;
二、addItemListener()
这个方法和只有后三个按钮可以使用,用来表示按钮的状态是否发生变化; 我们用代码来看一下;
public class Buttonexample extends JFrame{ private JRadioButton b1=new JRadioButton("按钮1"); private JRadioButton b2=new JRadioButton("按钮2"); private JRadioButton b3=new JRadioButton("按钮3"); public Buttonexample() { b1.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { // TODO Auto-generated method stub System.out.println("按钮状态改变了"); } }); this.setLayout(new FlowLayout()); this.add(b1); this.add(b2); this.add(b3); this.setSize(300,100); this.setVisible(true); } }运行后的结果:
我每次点击按钮1让他的状态发生改变的时候; 就会在控制台输出字符串;
三、按钮组,ButtonGroup
这个按钮组的类也是用来管理后三种按钮的; 也就是把若干个按钮放在一个按钮组中后; 我们只能使一个按钮为选中状态,当点击其他按钮后,上一个按钮就会从选种状态变为非选中状态; 看一下代码咋用吧;
public class Buttonexample extends JFrame{ private JRadioButton b1=new JRadioButton("按钮1"); private JRadioButton b2=new JRadioButton("按钮2"); private JRadioButton b3=new JRadioButton("按钮3"); private ButtonGroup group=new ButtonGroup(); public Buttonexample() { this.setLayout(new FlowLayout()); group.add(b1); group.add(b2); group.add(b3); this.add(b1); this.add(b2); this.add(b3); this.setSize(300,100); this.setVisible(true); } }运行后得结果看一下;
因为展示不出来,所以要自己尝试写出来看效果了;
OK,关于按钮这个控件的使用基本都在这里了; end,结束;