簡體   English   中英

我不知道如何實現ItemListener和ActionListener

[英]I can't figure out how to implement ItemListener and 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();

    }
}

為了進一步說明我的觀點,對於您的用例,您應該只將ActionListener添加到submit按鈕中。

ActionListener您需要在每個JRadioButtonJCheckBox上調用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);


    });

處理此類問題的一種更清潔,更可持續的方法是將JRadioButtonJCheckBox放入Array 這將使您的ActionListener代碼更加整潔,並且添加更多選項將更加容易,因為您只需要在數組中插入新的復選框或單選按鈕,而不必更改任何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());
      }
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM