事件处理以及如何对多个项目使用itemListener



所以我有两个问题,下面补充了关于我将要问的问题的以下课程:

1( 如果我有多个JCheckBox,我如何使用itemListener来知道何时选择了特定的JCheckBox。(在下面的例子中,我有3个JCheckBox,分别命名为汽油、电动和柴油,如果选择了汽油,我怎么能意识到这一点,我想做一些事情,比如,如果选择汽油,那么从JComboBox中删除一些项目(

2( 单击JButton时,如何使进度条增加或减少。在下面的代码中,我有一个JProgressBar,当用户单击drive时,我希望JProgressBar减小,当他们选择加油时,我想要JProgressBar增大。我想用JProgressBar来表示汽车的燃油油位。我该怎么做?

1级

import javax.swing.*;
import java.awt.*;  
import javax.swing.border.*;
public class CarViewer extends JFrame{

//row1
JPanel row1 = new JPanel();
JButton drv = new JButton("Drive");
JButton park = new JButton("Park");
JButton refuel = new JButton("Refuel");
//row2
JPanel row2 = new JPanel();
JLabel carTypeTag = new JLabel("Car Model:", JLabel.RIGHT);
JComboBox<String> options = new JComboBox<String>();
JCheckBox petrol = new JCheckBox("Petrol");
JCheckBox Electric = new JCheckBox("Electric");
JCheckBox diesel = new JCheckBox("Diesel");
JLabel fuelTypeTag = new JLabel("Fuel Type: ", JLabel.RIGHT);
ButtonGroup groupFuelType = new ButtonGroup();
//row3
JPanel row3 = new JPanel();
JLabel costTag = new JLabel("Cost:", JLabel.RIGHT);
JTextField costField = new JTextField(10);
JLabel engTag = new JLabel("Engine Size: ", JLabel.RIGHT);
JTextField engField = new JTextField(5);
JLabel mileageTag = new JLabel("Mileage: ", JLabel.RIGHT);
JTextField mField = new JTextField(10);
JLabel tankSizeTag = new JLabel("Tank size: ", JLabel.RIGHT);
JTextField tSField = new JTextField(5);
//row4
JPanel row4 = new JPanel();
JProgressBar petTank = new JProgressBar();
//row5
JPanel row5 = new JPanel();
JButton reset = new JButton("Reset");
public CarViewer(){
    super("Analyse A Car - AAC");
    setSize(400,800);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    GridLayout layoutMaster = new GridLayout(6, 1, 10, 20);
    setLayout(layoutMaster);
    ////Initial Errands
    groupFuelType.add(petrol);
    groupFuelType.add(diesel);
    groupFuelType.add(Electric);
    Dimension buttonDimension = new Dimension(80,30);
    Dimension resetButtonX = new Dimension(150,100);
    drv.setPreferredSize(buttonDimension);
    park.setPreferredSize(buttonDimension);
    refuel.setPreferredSize(buttonDimension);   
    petTank.setMinimum(0);
    petTank.setMaximum(100);

            ///Adding Car Models to Dropdown (JComboBox)
            options.addItem("Mercedes C63 AMG");
            options.addItem("BMW i7");
            options.addItem("Jaguar XFR");
            options.addItem("Nissan Skyline R35 GTR 4");
            EmptyBorder empty0 = new EmptyBorder(60, 0, 440, 0); //empty Border;
            EmptyBorder empty2 = new EmptyBorder(50,40,0,120); //empty Border row 3;
            EmptyBorder empty4 = new EmptyBorder(80,0,0,0);
            //Errands Complete
        CarEvent handler = new CarEvent();
        //Adding Listeners
        drv.addActionListener(handler);
        park.addActionListener(handler);
        refuel.addActionListener(handler);
        reset.addActionListener(handler);
        options.addItemListener(handler);
        petrol.addItemListener(handler);
        Electric.addItemListener(handler);
        diesel.addItemListener(handler);
        //Listeners Added.


    FlowLayout layout0 = new FlowLayout();
    row1.setLayout(layout0);
    row1.add(drv);
    row1.add(park);
    row1.add(refuel);
    row1.setBorder(empty0);
    add(row1);
    GridLayout layout1 = new GridLayout(1, 3, 40, 50);
    row2.setLayout(layout1);
    row2.add(carTypeTag);
    row2.add(options);
    row2.add(fuelTypeTag);
    row2.add(petrol);
    row2.add(diesel);
    row2.add(Electric);
    add(row2);
    GridLayout layout2 = new GridLayout(1, 4, 20, 0);
    row3.setLayout(layout2);
    costField.setEditable(false);
    engField.setEditable(false);
    tSField.setEditable(false);
    mField.setEditable(false);
    row3.add(costTag);
    row3.add(costField);
    row3.add(engTag);
    row3.add(engField);
    row3.add(tankSizeTag);
    row3.add(tSField);
    row3.add(mileageTag);
    row3.add(mField);
    row3.setBorder(empty2);
    add(row3);
    FlowLayout layout3 = new FlowLayout(FlowLayout.CENTER);
    row4.setLayout(layout3);
    row4.setBorder(empty4);
    row4.add(petTank);
    add(row4);
    FlowLayout layout4 = new FlowLayout(FlowLayout.CENTER);
    row5.setLayout(layout4);
    reset.setPreferredSize(resetButtonX);
    row5.add(reset);
    add(row5);
    setVisible(true);

}

public static void main(String[] args){
    CarViewer gui = new CarViewer();
}
}

事件处理类别(2(:

    import java.awt.event.*;
public class CarEvent implements ActionListener, ItemListener {
public void actionPerformed(ActionEvent event){
    String cmd = event.getActionCommand();
    if(cmd.equals("Drive"));{
    }
    else if(cmd.equals("Park")){
    }
    else if(cmd.equals("Refuel")){
    }
    else if(cmd.equals("Reset")){
    }
}
public void itemStateChanged(ItemEvent event){
    Object identifier = event.getItem();
    String item = identifier.toString();
    if())
}   
}

对于复选框问题,下面的代码可能会对您有所帮助。

  1. 将CarEvent类用作CarViewer的内部类。

  2. 在CarViewer中,将事件添加到每个控件

    public class CarViewer extends JFrame {
        petrol.addItemListener (new CarEvent());
        Electric.addItemListener (new CarEvent());
        diesel.addItemListener (new CarEvent());
    
    class CarEvent implements ActionListener, ItemListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println(e.toString());
    }
    @Override
    public void itemStateChanged(ItemEvent e) {
        if (e.getItem().equals(petrol)) {
            System.out.println("Petrol");
        }
        else if (e.getItem().equals(Electric)) {
            System.out.println("Electric");
        }
        else if (e.getItem().equals(diesel)) {
            System.out.println("Diesel");
        }
    }
    } //CarEvent class
    

    }//CarViewer类

如果您想将CarEvent用作单独的类,则需要将CarViewer类实例传递到此CarEvent类并访问复选框(当复选框为公共时(

public class CarViewer extends JFrame {
    petrol.addItemListener (new CarEvent(this));
    //and so on ,,,,
}//CarViewer class
class CarEvent implements ActionListener, ItemListener {
      CarViewer cv:
      public CarEvent(CarViewer _object){
            cv=_object;
      }
      @Override
public void actionPerformed(ActionEvent e) {
    System.out.println(e.toString());
}
@Override
public void itemStateChanged(ItemEvent e) {
    if (e.getItem().equals(cv.petrol)) {
        System.out.println("Petrol");
    }
    else if (e.getItem().equals(cv.Electric)) {
        System.out.println("Electric");
    }
    else if (e.getItem().equals(cv.diesel)) {
        System.out.println("Diesel");
    }
}
    }//CarEvent class

相关内容

  • 没有找到相关文章

最新更新