📜  Java摇摆 | JToggleButton 类

📅  最后修改于: 2022-05-13 01:55:12.095000             🧑  作者: Mango

Java摇摆 | JToggleButton 类

JToggleButton 是一个有两种状态的按钮。两种状态被选中和未选中。 JRadioButtonJCheckBox类是此类的子类。当用户按下切换按钮时,它会在按下或未按下之间切换。 JToggleButton 用于从可能的选项列表中选择一个选项。按钮可以通过操作进行配置,并在一定程度上进行控制。除了直接配置按钮之外,将操作与按钮一起使用还有很多好处。

JToggleButton 中的构造函数:

  1. JToggleButton():创建一个最初未选择的切换按钮,而不设置文本或图像。
  2. JToggleButton(Action a):创建一个切换按钮,其中的属性取自提供的 Action。
  3. JToggleButton(Icon icon):使用指定的图像创建一个最初未选择的切换按钮,但没有文本。
  4. JToggleButton(Icon icon, boolean selected):创建一个具有指定图像和选择状态的切换按钮,但没有文本。
  5. JToggleButton(String text):使用指定的文本创建一个未选中的切换按钮。
  6. JToggleButton(String text, boolean selected):创建具有指定文本和选择状态的切换按钮。
  7. JToggleButton(String text, Icon icon):创建一个具有指定文本和图像的切换按钮,并且最初未被选中。
  8. JToggleButton(String text, Icon icon, boolean selected):创建具有指定文本、图像和选择状态的切换按钮。

常用方法:

MethodDescription
getAccessibleContext()Gets the AccessibleContext associated with this JToggleButton.
getUIClassID()Returns a string that specifies the name of the l&f class that renders this component.
paramString()Returns a string representation of this JToggleButton.
updateUI()Resets the UI property to a value from the current look and feel.

下面的程序说明了 JToggleButton 类:

1. 使用 ItemListener 实现 JToggleButton 事件的Java程序:在这个程序中,我们使用JFrame()创建框架。这里, setDefaultCloseOperation()用于设置框架的关闭选项。使用JToggleButton()创建一个按钮。实例化仅包含在单击按钮时自动调用的itemStateChanged()方法的ItemListener 。在按钮上生成事件,因此将输出打印到控制台。附加所有侦听器并将ItemListener添加到按钮。将按钮添加到框架并设置框架的大小。

Java
import java.awt.BorderLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
 
public class JToggleButtonExamp {
 
    // Main Method
    public static void main(String args[])
    {
 
        // create a frame and set title
        JFrame frame = new JFrame("Selecting Toggle");
 
        // set the default close operation of the frame
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        // create a ToggleButton
        JToggleButton toggleButton = new JToggleButton("Toggle Button");
 
        // ItemListener is notified whenever you click on the Button
        ItemListener itemListener = new ItemListener() {
 
            // itemStateChanged() method is nvoked automatically
            // whenever you click or unlick on the Button.
            public void itemStateChanged(ItemEvent itemEvent)
            {
 
                // event is generated in button
                int state = itemEvent.getStateChange();
 
                // if selected print selected in console
                if (state == ItemEvent.SELECTED) {
                    System.out.println("Selected");
                }
                else {
 
                    // else print deselected in console
                    System.out.println("Deselected");
                }
            }
        };
 
        // Attach Listeners
        toggleButton.addItemListener(itemListener);
        frame.add(toggleButton, BorderLayout.NORTH);
        frame.setSize(300, 125);
        frame.setVisible(true);
    }
}


Java
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractButton;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
 
public class JToggleButtonExamp {
 
    // Main Method
    public static void main(String args[])
    {
 
        // create the JFrame
        JFrame frame = new JFrame("Selecting Toggle");
 
        // set default close operation for frame
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        // create a ToggleButton
        JToggleButton toggleButton = new JToggleButton("Toggle Button");
 
        // Define ActionListener
 
        ActionListener actionListener = new ActionListener()
        {
 
            // actionPerformed() method is invoked
            // automatically whenever you click on
            // registered component
            public void actionPerformed(ActionEvent actionEvent)
            {
     
                AbstractButton abstractButton =
                (AbstractButton)actionEvent.getSource();
     
                // return true or false according
                // to the selection or deselection
                // of the button
                boolean selected = abstractButton.getModel().isSelected();
     
                System.out.println("Action - selected=" + selected + "\n");
            }
          };
        // Attach Listeners
        toggleButton.addActionListener(actionListener);
 
        // add ToggleButton to the frame
        frame.add(toggleButton, BorderLayout.NORTH);
 
        // set size of the frame
        frame.setSize(300, 125);
 
        frame.setVisible(true);
    }
}


输出:

2. Java程序使用ActionListener实现JToggleButton事件:这里,在JFrame上创建了一个JToggleButton。然后,我们定义 ActionListener。 actionPerformed()ActionListener()中唯一的方法,只要单击已注册的组件就会调用该方法。 abstractButton.getModel().isSelected()如果按钮被选中则返回 true,否则返回 false。将侦听器附加到切换按钮。

Java

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractButton;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
 
public class JToggleButtonExamp {
 
    // Main Method
    public static void main(String args[])
    {
 
        // create the JFrame
        JFrame frame = new JFrame("Selecting Toggle");
 
        // set default close operation for frame
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        // create a ToggleButton
        JToggleButton toggleButton = new JToggleButton("Toggle Button");
 
        // Define ActionListener
 
        ActionListener actionListener = new ActionListener()
        {
 
            // actionPerformed() method is invoked
            // automatically whenever you click on
            // registered component
            public void actionPerformed(ActionEvent actionEvent)
            {
     
                AbstractButton abstractButton =
                (AbstractButton)actionEvent.getSource();
     
                // return true or false according
                // to the selection or deselection
                // of the button
                boolean selected = abstractButton.getModel().isSelected();
     
                System.out.println("Action - selected=" + selected + "\n");
            }
          };
        // Attach Listeners
        toggleButton.addActionListener(actionListener);
 
        // add ToggleButton to the frame
        frame.add(toggleButton, BorderLayout.NORTH);
 
        // set size of the frame
        frame.setSize(300, 125);
 
        frame.setVisible(true);
    }
}

输出 :

注意:上述程序可能无法在在线 IDE 中运行。请使用离线编译器。

参考: https://docs.oracle.com/javase/7/docs/api/javax/swing/JToggleButton.html