2017-09-22 47 views
1

我想弄清楚在这个例子中如何正确使用监听器。我想要它做的是在点击提交按钮后显示一个弹出窗口,显示哪些框被选中,但现在我点击每个框后弹出一个弹出窗口。我已经尝试为单选按钮和复选框的项目监听器实现一个动作监听器,但我不确定这是否正确。我错过了什么?我不知道如何实现ItemListener和ActionListener

import javax.swing.*; 
import java.awt.event.*; 
import java.awt.*; 



public class CarSelector extends JFrame implements ActionListener, ItemListener{ 

    JButton submit = new JButton("Submit"); 
    JLabel label1 = new JLabel("Select Vehicle type and options"); 
    JLabel carLabel = new JLabel("Vehicle Type"); 
    JLabel options = new JLabel("Options"); 
    ButtonGroup group = new ButtonGroup(); 
    JRadioButton carRadio = new JRadioButton("Car", true); 
    JRadioButton vanRadio = new JRadioButton("Minivan"); 
    JRadioButton truckRadio = new JRadioButton("Pickup Truck"); 
    JRadioButton suvRadio = new JRadioButton("SUV"); 

    JCheckBox leather = new JCheckBox("Leather Seats"); 
    JCheckBox ac = new JCheckBox("Air Conditioning"); 
    JCheckBox sat = new JCheckBox("Sattelite Radio"); 
    JCheckBox warmer = new JCheckBox("Seat Warmers"); 
    String optionsSelected; 
    String carSelected; 

    ActionListener listen = new ActionListener(){ 

     @Override 
     public void actionPerformed(ActionEvent ae){ 

      JOptionPane.showMessageDialog(
      CarSelector.this, sb.toString + ssb.toString()); 
     } 
    }; 




    CarSelector(){ 
     super("Vehicle Selector"); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setSize(300, 300); 

     CarGUI(); 
    } 

    public void CarGUI(){ 


     JPanel vehicleTypes = new JPanel(); 
     JPanel carOptions = new JPanel(); 
     JPanel submitButton = new JPanel(); 


     carRadio.addActionListener(listen); 
     vanRadio.addActionListener(listen); 
     truckRadio.addActionListener(listen); 
     suvRadio.addActionListener(listen); 
     leather.addActionListener(listen); 
     ac.addActionListener(listen); 
     sat.addActionListener(listen); 
     warmer.addActionListener(listen); 
     submit.addActionListener(listen); 

     add(submitButton); 
     submitButton.setLayout(new BoxLayout(submitButton, BoxLayout.X_AXIS)); 
     submitButton.setBounds(100, 150, 100, 100); 

     add(vehicleTypes); 
     vehicleTypes.setLayout(new BoxLayout(vehicleTypes, BoxLayout.Y_AXIS)); 
     vehicleTypes.setBounds(150,0,125,125); 

     add(carOptions); 
     carOptions.setLayout(new BoxLayout(carOptions, BoxLayout.Y_AXIS)); 

     vehicleTypes.add(carLabel); 
     vehicleTypes.add(carRadio); 
     vehicleTypes.add(vanRadio); 
     vehicleTypes.add(truckRadio); 
     vehicleTypes.add(suvRadio); 
     group.add(carRadio); 
     group.add(vanRadio); 
     group.add(truckRadio); 
     group.add(suvRadio);   


     carOptions.add(options); 
     carOptions.add(leather); 
     carOptions.add(ac); 
     carOptions.add(sat); 
     carOptions.add(warmer); 

     submitButton.add(submit); 
     setVisible(true); 

    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
    } 

    @Override 
    public void itemStateChanged(ItemEvent e) { 


     throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
    } 




} 

这是运行程序的代码。

public class GUITest { 
    public static void main (String[] args){ 
     CarSelector car = new CarSelector(); 

    } 
} 
+0

所以......你应该只在'submit'中添加'ActionListener'。 – CraigR8806

回答

0

为了推动我的评论上方,为您的使用情况下,你应该只添加ActionListenersubmit按钮。

ActionListener你将需要调用每个JRadioButton S和JCheckBox ES的isSelected

像这样的事情会做的伎俩:

public void CarGUI(){ 


    JPanel vehicleTypes = new JPanel(); 
    JPanel carOptions = new JPanel(); 
    JPanel submitButton = new JPanel(); 


    //The below syntax assumes you are running Java 8. Please let me know if you are not 
    //and I will update the syntax 
    submit.addActionListener((e)=>{ 
     StringBuilder sb = new StringBuilder(); 

     sb.append("Vehicle Type: "); 
     if(carRadio.isSelected()) sb.append("Car"); 
     else if(vanRadio.isSelected()) sb.append("Van"); 

     //Continue with the rest of radiobuttons 
     // and do the same with the Checkboxes just make sure to 
     // not use "else" if and make a new conditional for each Checkbox 

     JOptionPane.showMessageDialog(
     CarSelector.this, sb.toString); 


    }); 

一个更清洁,更可持续的方式来处理这样的事情将是把你的JRadioButton S和JCheckBox ES在Array秒。这会使你的代码更加清洁,添加更多选项会更容易,因为你只需要在数组中插入一个新的复选框或单选按钮,而不必更改任何ActionListener代码。

JRadioButton[] vehicleTypes = new JRadioButton[] { new JRadioButton("Car", true), new JRadioButton("Van") ... }; 

// then in ActionListener 

for(JRadioButton rBtn: vehicleTypes){ 
     if(rBtn.isSelected()){ 
      sb.append(rBtn.getText()); 
     } 
} 
相关问题