单选按钮事件处理程序

问题描述:

我在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); 
    } 
} 
+0

您不慎代替主框架类的给定'radioButtonPanel',你能解决这个问题? –

+0

没有意识到我做到了。得到它修复。 – Wildfire44601

下面的代码应该给你简要介绍如何使用事件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 
      } 
     } 
    }); 

根据所被选择时,该 不需要应隐藏某些框的单选按钮。例如,如果选择了矩形,则只应显示长度和宽度方框 。

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

例子:

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. 
      } 
     } 
}); 
+0

我想我正在关注这个,但让我确定。在if语句中,e.getSource()检测选中哪个单选按钮,并为我想要隐藏的字段调用setVisible属性。现在,action是否执行了捕获更改的泛型函数,或者是否应该使用像itemStateChanged这样更具体的东西替换它? – Wildfire44601

+0

我应该为每个按钮制作一个ActionListener,还是可以用一系列if语句来制作单个按钮来处理不同的按钮?从你的示例看起来,我会猜测每个按钮都有自己的ActionListener? – Wildfire44601

+0

是的,每个都有自己的,看看oracle java参考页面的进一步帮助。如果答案对你有帮助,如果你将答案标记为已接受,我将不胜感激。谢谢。 –