JToggleButton示例

程序示例:
package MyJava.Base;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class JToggleButtonFrame extends JFrame

 private JToggleButton button1;
 private JToggleButton button2;
 private JToggleButton button3;
 private JToggleButton button4;
 private JToggleButton button5;
 private JToggleButton button6;
 private JToggleButton button7; 
 
 public JToggleButtonFrame(String title)
 { 
  super(title);
  setSize(400,500);
  //获得容器
  Container container=this.getContentPane(); 
  
  //设置布局为6行一列
  container.setLayout(new GridLayout(7,1));
  
  //创建带有Icon的JToggleButton
  Icon pic=new ImageIcon("E://Java//JCreator2.5//picture//search.gif");
  
  button1=new JToggleButton(pic);
  container.add(button1);
  
  //创建带有Icon的JToggleButton,并且设定按钮的初始状态
  Icon pic2=new ImageIcon("E://Java//JCreator2.5//picture//index.gif");
  button2=new JToggleButton(pic2,true);
  container.add(button2);
    
  //创建含有文字的JToggleButton
  button3=new JToggleButton("I Love You");
  container.add(button3);
  
  //创建含有文字的JToggleButton,并且设定按钮的初始状态
  button4=new JToggleButton("I Love You",true);
  container.add(button4);
  
  //创建含有文字和图像的JToggleButton;
  button5=new JToggleButton("I Love You",pic);
  container.add(button5);
  
  
  //创建含有文字和图像的JToggleButton;并设定初始状态
  button6=new JToggleButton("I Love You",pic,true);
  container.add(button6);
  
  //创建含有Rollover的JToggleButton
  button7=new JToggleButton();
  button7.setRolloverEnabled(true);
  button7.setIcon(new ImageIcon("E://Java//JCreator2.5//picture//cat.gif"));
  button7.setRolloverIcon(new ImageIcon("E://Java//JCreator2.5//picture//bird.gif"));
  button7.setSelectedIcon(new ImageIcon("E://Java//JCreator2.5//picture//buff.gif"));
  container.add(button7);
  
  
  
 }
}
public class JToggleButtonDemo

 public static void main(String[] args) throws Exception
 { 
  JToggleButtonFrame frame=new JToggleButtonFrame("JToggleButtonDemo");
 // frame.pack();
  frame.setVisible(true);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
 }
}
 
程序结果:
 
 

 
开始时的状态,注意有几个按钮是处于被按下去的状态
 
 

 
鼠标在按钮范围内时按钮上显示的图像
 
 

按钮被按下去的时候显示的图像,注意JToggleButton与JButton的不同之处: JButton按下去释放的时候不想会马上恢复到原来的图像,JToggleButton按下去释放的时候图像不会变化,只有你再次点击按钮的时候才会变化

请教:判断JToggleButton的select状态设置不同背景色问题

09-13

想实现一个效果:JToggleButton,当没按下去时显示绿色,按下去时显示黄色。rn但不知为什么按下去后显示不出黄色来,而是默认的灰色。rn请教各位高手,这是什么原因?[color=#FF0000]以及要达到这个目的应该如何做![/color]rn非常感谢各位高手,有提示也欢迎,谢谢!rn代码如下:rn[code=Java]package test24Buttons;rnrnimport java.awt.Color;rnimport java.awt.event.ActionEvent;rnimport java.awt.event.ActionListener;rnrnimport javax.swing.JFrame;rnimport javax.swing.JPanel;rnimport javax.swing.JToggleButton;rnrnpublic class Hour rn JPanel timePanel = new JPanel();rn JBusyTimeButton btn1 = new JBusyTimeButton("1");rn JBusyTimeButton btn2 = new JBusyTimeButton("2");rn JBusyTimeButton btn3 = new JBusyTimeButton("3");rn JBusyTimeButton btn4 = new JBusyTimeButton("4");rn JBusyTimeButton btn5 = new JBusyTimeButton("5");rn JBusyTimeButton btn6 = new JBusyTimeButton("6");rn rn public Hour() rn timePanel.add(btn1);rn timePanel.add(btn2);rn timePanel.add(btn3);rn timePanel.add(btn4);rn timePanel.add(btn5);rn timePanel.add(btn6);rn rn rn public JPanel getBusyTimePanel() rn return timePanel;rn rn rn public class JBusyTimeButton extends JToggleButton rn private static final long serialVersionUID = 1L;rn rn public JBusyTimeButton(String text) rn super(text);rn setBackground(Color.green);rn this.addActionListener(new ActionListener() rn public void actionPerformed(ActionEvent e) rn if (!((JToggleButton)e.getSource()).getModel().isSelected()) rn ((JToggleButton)e.getSource()).setBackground(Color.green);rn else rn ((JToggleButton)e.getSource()).setBackground(Color.yellow);rn rn rn );rn rn rn rn public static void main(String[] args) rn JFrame frame = new JFrame();rn Hour bt = new Hour();rn frame.add(bt.getBusyTimePanel());rn frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);rn frame.pack();rn frame.setLocationRelativeTo(null);rn frame.setVisible(true);rn rnrn[/code]rn 论坛

没有更多推荐了,返回首页