多状态按钮该如何实现 点击的按钮有多个状态,类似 JToggleButton有两个状态一样,例如我点击一个按钮第一次点击后按钮的状态是a,第二次点击按钮的状态是b,第三次点击按钮状态是c 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 加个flag就行了,每点一下,flag按一定规律变换,然后根据flag调用不同的方法 写一个简单的代码,应该是楼主要的吧import java.awt.*import java.awt.event.*;import javax.swing.*;public class ButtonChange extends JFrame { private int flag; //设置状态标志 private JButton button; //变化的按钮 private Icon[] images; //用于按钮表面的图片显示 public ButtonChange() { setFrame(); flag = 0; button = new JButton("Click Me"); images = new Icon[] { new ImageIcon(getClass().getResource("1.gif")), new ImageIcon(getClass().getResource("2.gif")), new ImageIcon(getClass().getResource("3.gif")) } button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(mad == 0)) { button.setIcon(images[0]); mad++; } if(mad == 1) { button.setIcon(images[1]); mad++; } if(mad == 2) { button.setIcon(images[2]); mad = 0; } } }); } public void setFrame() { this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLayout(new FlowLayout()); this.setSize(600, 480); this.setVisible(true); } public static void main(Strin[] args) { new ButtonChange(); }} 如果楼主要求的多态是换Icon和文字,可以用楼上的方法。我没用过button表现多态,如果多种状态一般用JRadioButton或者JComboBox这样的(只是说我自己这样用)。只用过JToggleButton这样两种状态的。 不单是图标和文字例如一个jtextfiled&一个buttonjtextfiled中输入的是字符串,button显示状态a输入int,显示状态b输入double,显示状态czhanglongnihao同学给出的代码能满足上述需求,不过我想要的情况是:单jtextfiled输入int后,button显示状态b,然后我点击button,显示状态c时输入的int转换成double,显示状态a时int转换成string 实现功能不难,重要的是用JButton表现多种状态好不好。如果类型不是太多的话,用JRadioButton是不是直观一点?类似下面的布局| text ||*int| |*str| |*double|如果类型多的话,JButton变来变去的,用户很晕的。 看看JAVA设计模式里面的状态模式或许能有些帮助,我之前看过但是现在忘记了..... 拓维集团的面试题谁有? newbie 怎么获得一个对象数组的某属性 有了解规则引擎 rete的吗 JAI怎么压缩tiff啊,急救 java 这两句代码有什么区别吗 我是刚学习JAVA的新手,一些问题希望得到帮助 关于JAVA代码的合并问题 汉字拆分问题 applet的问题~我没搞过,谁知道的。 大家来说说 空格转换问题
import java.awt.event.*;
import javax.swing.*;public class ButtonChange extends JFrame {
private int flag; //设置状态标志
private JButton button; //变化的按钮
private Icon[] images; //用于按钮表面的图片显示
public ButtonChange() {
setFrame();
flag = 0;
button = new JButton("Click Me");
images = new Icon[] {
new ImageIcon(getClass().getResource("1.gif")),
new ImageIcon(getClass().getResource("2.gif")),
new ImageIcon(getClass().getResource("3.gif"))
}
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(mad == 0)) {
button.setIcon(images[0]);
mad++;
}
if(mad == 1) {
button.setIcon(images[1]);
mad++;
}
if(mad == 2) {
button.setIcon(images[2]);
mad = 0;
}
}
});
}
public void setFrame() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
this.setSize(600, 480);
this.setVisible(true);
}
public static void main(Strin[] args) {
new ButtonChange();
}
}
如果多种状态一般用JRadioButton或者JComboBox这样的(只是说我自己这样用)。
只用过JToggleButton这样两种状态的。
例如一个jtextfiled&一个button
jtextfiled中输入的是字符串,button显示状态a
输入int,显示状态b
输入double,显示状态c
zhanglongnihao同学给出的代码能满足上述需求,不过我想要的情况是:
单jtextfiled输入int后,button显示状态b,然后我点击button,显示状态c时输入的int转换成double,显示状态a时int转换成string
如果类型不是太多的话,用JRadioButton是不是直观一点?
类似下面的布局
| text |
|*int| |*str| |*double|如果类型多的话,JButton变来变去的,用户很晕的。