Java/Swing JFC/ToggleButton

Материал из Java эксперт
Перейти к: навигация, поиск

JToggleButton is a button that has two states. Pressed and not pressed.

  
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
public class ToggleButton extends JDialog implements ActionListener {
  private JToggleButton red = new JToggleButton("red");
  private JToggleButton green = new JToggleButton("green");
  private JToggleButton blue = new JToggleButton("blue");
  private JPanel display = new JPanel();
  public ToggleButton() {
    JPanel bottom = new JPanel();
    JPanel leftPanel = new JPanel();
    red.addActionListener(this);
    green.addActionListener(this);
    blue.addActionListener(this);
    leftPanel.add(red);
    leftPanel.add(green);
    leftPanel.add(blue);
    bottom.add(leftPanel);
    display.setBackground(Color.black);
    bottom.add(display);
    add(bottom);
    pack();
    setResizable(false);
    setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    setVisible(true);
  }
  public static void main(String[] args) {
    new ToggleButton();
  }
  public void actionPerformed(ActionEvent e) {
    Color color = display.getBackground();
    int red = color.getRed();
    int green = color.getGreen();
    int blue = color.getBlue();
    if (e.getActionCommand() == "red") {
      if (red == 0) {
        red = 255;
      } else {
        red = 0;
      }
    }
    if (e.getActionCommand() == "green") {
      if (green == 0) {
        green = 255;
      } else {
        green = 0;
      }
    }
    if (e.getActionCommand() == "blue") {
      if (blue == 0) {
        blue = 255;
      } else {
        blue = 0;
      }
    }
    Color setCol = new Color(red, green, blue);
    display.setBackground(setCol);
  }
}





This class represents the toggle buttons used in toolbars.

  
/*
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at
       http://www.apache.org/licenses/LICENSE-2.0
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JToggleButton;
import javax.swing.UIManager;
/**
 * This class represents the toggle buttons used in toolbars.
 *
 * @version $Id: JToolbarButton.java 498555 2007-01-22 08:09:33Z cam $
 */
public class JToolbarToggleButton extends JToggleButton {
    /**
     * Creates a new toolbar button.
     */
    public JToolbarToggleButton() {
        initialize();
    }
    /**
     * Creates a new toolbar button.
     * @param txt The button text.
     */
    public JToolbarToggleButton(String txt) {
        super(txt);
        initialize();
    }
    /**
     * Initializes the button.
     */
    protected void initialize() {
        if (!System.getProperty("java.version").startsWith("1.3")) {
            setOpaque(false);
            setBackground(new java.awt.Color(0, 0, 0, 0));
        }
        setBorderPainted(false);
        setMargin(new Insets(2, 2, 2, 2));
        // Windows XP look and feel seems to have a bug due to which the
        // size of the parent container changes when the border painted
        // property is set. Temporary fix: disable mouseover behavior if
        // installed lnf is Windows XP
        if (!UIManager.getLookAndFeel().getName().equals("Windows")) {
            addMouseListener(new MouseListener());
        }
    }
    /**
     * To manage the mouse interactions.
     */
    protected class MouseListener extends MouseAdapter {
        public void mouseEntered(MouseEvent ev) {
            setBorderPainted(true);
        }
        public void mouseExited(MouseEvent ev) {
            setBorderPainted(false);
        }
    }
}





Working with Toggle Buttons

  
import java.awt.Color;
import java.awt.ruponent;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Polygon;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
public class ToggleTest {
  static class TriangleIcon implements Icon {
    String name;
    static class State {
      public static final int NORMAL = 0;
      public static final int PRESSED = 1;
      public static final int ROLLOVER = 2;
      public static final int SELECTED = 3;
      private State() {
      }
    }
    int state;
    Color color;
    public TriangleIcon(Color c, int state) {
      color = c;
      this.state = state;
    }
    public int getIconWidth() {
      return 20;
    }
    public int getIconHeight() {
      return 20;
    }
    public void paintIcon(Component c, Graphics g, int x, int y) {
      g.setColor(color);
      Polygon p = new Polygon();
      if (state == State.NORMAL) {
        p.addPoint(x + (getIconWidth() / 2), y);
        p.addPoint(x, y + getIconHeight() - 1);
        p.addPoint(x + getIconWidth() - 1, y + getIconHeight() - 1);
      } else if (state == State.PRESSED) {
        p.addPoint(x, y);
        p.addPoint(x + getIconWidth() - 1, y);
        p.addPoint(x + (getIconWidth() / 2), y + getIconHeight() - 1);
      } else if (state == State.SELECTED) {
        p.addPoint(x + getIconWidth() - 1, y);
        p.addPoint(x + getIconWidth() - 1, y + getIconHeight() - 1);
        p.addPoint(x, y + (getIconHeight() / 2));
      } else {
        p.addPoint(x, y);
        p.addPoint(x, y + getIconHeight() - 1);
        p.addPoint(x + getIconWidth() - 1, y + (getIconHeight() / 2));
      }
      g.fillPolygon(p);
    }
  }
  public static void main(String args[]) {
    JFrame frame = new JFrame();
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new GridLayout(0, 1));
    Icon normalIcon = new TriangleIcon(Color.red, TriangleIcon.State.NORMAL);
    Icon pressedIcon = new TriangleIcon(Color.red,
        TriangleIcon.State.PRESSED);
    Icon selectedIcon = new TriangleIcon(Color.red,
        TriangleIcon.State.SELECTED);
    Icon rolloverIcon = new TriangleIcon(Color.red,
        TriangleIcon.State.ROLLOVER);
    JToggleButton b = new JToggleButton("Initially UnSelected");
    contentPane.add(b);
    b = new JToggleButton("Initially Selected", true);
    contentPane.add(b);
    b = new JToggleButton(normalIcon);
    b.setPressedIcon(pressedIcon);
    b.setSelectedIcon(selectedIcon);
    b.setRolloverIcon(rolloverIcon);
    b.setRolloverEnabled(true);
    contentPane.add(b);
    frame.setSize(300, 150);
    frame.show();
  }
}