JToggleButto示例

package test;

import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JToggleButton;

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);
 }

你可能感兴趣的:(java,swing)