单选按钮事件处理程序

时间:2017-03-06 18:55:51

标签: java eventhandler

我在使用java项目时遇到了一些麻烦。我已经建立了一个空的GUI界面,现在我需要为它添加一些功能。然而,我对如何解决这个问题感到困惑。基本布局有4个单选按钮,矩形,框,圆和圆柱。我有一个组面板,有4个单独的面板,每个面板都有文本框,标签用于输入高度,长度,宽度和半径。以下是它的外观:GUI layout。根据所选的单选按钮,应隐藏某些不需要的框。例如,如果选择“矩形”,则只应显示长度和宽度的框。

将显示所有内容的主框架在这里:

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import java.awt.Font;

public class GUIFrame extends JFrame
{
    //private final BorderLayout layout;
    private final FlowLayout layout;
    private final JLabel lblTitle;
    private final JButton btnProc;

    public GUIFrame()
    {

        super("GUI Layout");
        Font titleFont = new Font("Verdana", Font.BOLD, 26);

        btnProc = new JButton("Click to Process");
        lblTitle = new JLabel("Figure Center");
        lblTitle.setFont(titleFont);
        widthPanel myWidth = new widthPanel();
        myWidth.setLocation(0, 400);
        lengthPanel myLength = new lengthPanel();
        heightPanel myHeight = new heightPanel();
        radiusPanel myRadius = new radiusPanel();
        radioButtonPanel myButtons = new radioButtonPanel();

        //layout = new BorderLayout(3, 2);
        layout = new FlowLayout();

        JPanel txtGroup = new JPanel();
        txtGroup.setLayout(new GridLayout(2, 2));
        txtGroup.add(myWidth);
        txtGroup.add(myLength);
        txtGroup.add(myRadius);
        txtGroup.add(myHeight);
        setLayout(layout);

        add(lblTitle);
        add(myButtons);
        add(txtGroup);
        add(btnProc);


        if(myButtons.btnRectangle.isSelected())
        {
            myHeight.setVisible(false);
            myRadius.setVisible(false);
        }

    }

    private class RadioButtonHandler implements ItemListener
    {
        @Override
        public void itemStateChanged(ItemEvent event)
        {

        }
    }

}

我可以使用if语句来解决这个问题,但是我应该使用事件处理程序,而我却失去了如何对其进行编码以使其正常工作。

如果有帮助,请点击按钮面板的代码:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import java.awt.GridLayout;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;


public class radioButtonPanel extends JPanel
{
    private final JRadioButton btnRectangle;
    private final JRadioButton btnBox;
    private final JRadioButton btnCircle;
    private final JRadioButton btnCylinder;
    private final ButtonGroup radioButtonGroup;
    private final JLabel label;
    private final JPanel radioPanel;


    public radioButtonPanel()
    {
        radioPanel = new JPanel();
        radioPanel.setLayout(new GridLayout(5,1));

        btnRectangle = new JRadioButton("Rectangle", true);
        btnBox = new JRadioButton("Box", false);
        btnCircle = new JRadioButton("Circle", false);
        btnCylinder = new JRadioButton("Cylinder", false);
        label = new JLabel("Select A Figure:");
        radioPanel.add(label);
        radioPanel.add(btnRectangle);
        radioPanel.add(btnBox);
        radioPanel.add(btnCircle);
        radioPanel.add(btnCylinder);

        radioButtonGroup = new ButtonGroup();
        radioButtonGroup.add(btnRectangle);
        radioButtonGroup.add(btnBox);
        radioButtonGroup.add(btnCircle);
        radioButtonGroup.add(btnCylinder);


        add(radioPanel);


    }

}

其中一个面板的样本。它们都遵循相同的设置,只是不同的变量名称。

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPanel;
import java.awt.GridLayout;

public class heightPanel extends JPanel
{
    private final JLabel lblHeight;
    private final JTextField txtHeight;
    private final JPanel myHeight;

    public heightPanel()
    {
        myHeight = new JPanel();
        myHeight.setLayout(new GridLayout(2,1));

        lblHeight = new JLabel("Enter Height:");
        txtHeight = new JTextField(10);
        myHeight.add(lblHeight);
        myHeight.add(txtHeight);


        add(myHeight);
    }
}

1 个答案:

答案 0 :(得分:0)

以下代码可让您快速了解如何将事件listener/handler用于您的按钮组(JRadioButtons)。

  

但我应该使用事件处理程序,而我却失去了如何使用   代码,以使其正常工作。

    //add listener to the rectangle button and should be the same for the other `JRadioButtons` but with different `identifiers`.

    btnRectangle.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
           if(e.getSource() == btnRectangle){
          //TODO
           }
        }
    });
  

取决于所选的单选按钮,某些框表示   不需要隐藏。例如,如果选择了Rectangle,   只有长度和宽度的框应该是可见的。

//隐藏JTextFields使用   void setVisible(boolean visible)方法。如果要隐藏false,则应将JTextField作为参数传递给方法。

示例:

 btnRectangle.addActionListener(new ActionListener() {
       @Override
        public void actionPerformed(ActionEvent e) {
           if(e.getSource() == btnRectangle){
              TextFieldName.setVisible(false); // set the textfields that you want to be hidden once the Rectangle button is chosen.
           }
        }
 });