6

ユーザーが使用したいオプションを簡単に選択できるように、Java プログラムで RadioButtons と Checkboxes を使用する必要があります (これは「ガソリンスタンド」です)。

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

public class New_Gas_Bar extends JFrame
{
    public JPanel panel1,panel2,panel3,panel4,panel5;
    public JLabel main1, main2, main3;
    public JLabel gasBar,total;
    public JButton button1,button2,button3,button4;
    public JRadioButton bronzeG,silverG,goldG,selfS,fullS,fullService,selfService;
    public JCheckBox oilC,windWash,windClean;
    static double fullCost,selfCost;

public New_Gas_Bar()
{
    super ("      New Gas Bar");
    setSize(640,640);
    Container container = getContentPane();

    panel1=new JPanel();
    panel2=new JPanel();

    panel1.setBackground(new Color(107,202,226));
    panel1.setLayout(new GridLayout(7,1));

    main1 = new JLabel ("         Gas Bar Project ");
    main2 = new JLabel ("         ICS4U0 - September 2013");
    main3 = new JLabel ("     ");

    button1 = new JButton ("Gas Station");
    button2 = new JButton ("Car Wash");
    button3 = new JButton ("Total");
    button4 = new JButton ("Exit");

    panel2.setBackground(new Color(144,160,170));
    panel2.setLayout(new GridLayout(4,1));
    panel2.add (button1);
    panel2.add (button2);
    panel2.add (button3);
    panel2.add (button4);
    panel1.add (main1);
    panel1.add (main2);

    //panel2.add(gasBar);
    //panel3.add(total);

    container.setLayout(new BorderLayout());

    container.add(panel1,BorderLayout.NORTH);
    container.add(panel2,BorderLayout.CENTER);

    ButtonHandler handler = new ButtonHandler ();
    button1.addActionListener (handler);
    button2.addActionListener (handler);
    button3.addActionListener (handler);
    button4.addActionListener (handler);

    ButtonGroup serveStyle = new ButtonGroup();
    fullS = new JRadioButton ("Full Serve");
    selfS = new JRadioButton ("Self Serve");
    serveStyle.add (fullS);
    serveStyle.add (selfS);

    ButtonGroup serveGas = new ButtonGroup();
    bronzeG = new JRadioButton("Bronze Service");
    silverG = new JRadioButton("Silver Service");
    goldG = new JRadioButton("Gold Service");
    serveGas.add (bronzeG);
    serveGas.add (silverG);
    serveGas.add (goldG);

    oilC = new JCheckBox("Oil Change");
    windWash = new JCheckBox("Windshield Wash");
    windClean = new JCheckBox("Windshield Cleaning");

    RadioButtonHandler radioHand = new RadioButtonHandler ();
    bronzeG.addItemListener (radioHand);
    CheckBoxHandler checkHand = new CheckBoxHandler();
    oilC.addItemListener (checkHand);
    setVisible(true);
    pack();
}

public static void main (String [] args)
{

    New_Gas_Bar application = new New_Gas_Bar();    
}

public class ButtonHandler implements ActionListener

{
    public void actionPerformed (ActionEvent event)
    {
        if (event.getSource () == button1)
        {           
            panel1.setVisible(true);
            panel2.setVisible(false);
            panel3.setVisible(true);
            panel4.setVisible(false);
            panel5.setVisible(false);


        }
        else if (event.getSource () == button2)
        {

        }
        else if (event.getSource () == button3)
        {

        }
        else if (event.getSource () == button4)
        {
            System.exit(0);
        }
    }
}
public class RadioButtonHandler implements ActionListener

{
    public void actionPerformed (ActionEvent event)
    {
        if (event.getSource () == fullS)
        {           

        }
        else if (event.getSource () == selfS)
        {

        }
        if (event.getSource () == bronzeG)
        {           

        }
        else if (event.getSource () == silverG)
        {

        }
        else if (event.getSource () == goldG)
        {
        }
    }
}
public class CheckBoxHandler implements ActionListener

{
    public void actionPerformed (ActionEvent event)
    {
        if (event.getSource () == checkBox1)
        {           

        }
        else if (event.getSource () == checkBox2)
        {

        }
        else if (event.getSource () == checkBox3)
        {

        }
    }
}
}
}

私が得ているエラーはこの部分にあります

RadioButtonHandler radioHand = new RadioButtonHandler ();
    bronzeG.addItemListener (radioHand);
    CheckBoxHandler checkHand = new CheckBoxHandler();
    oilC.addItemListener (checkHand);

の理由がわかりませんbronzeG.addItemListener、と書かれています

addItemListener(java.awt.event.ItemListener) in javax.swing.AbstractButton cannot be applied to (New_Gas_Bar.RadioButtonHandler)
4

1 に答える 1

6

addItemListenerItemListener引数として an が渡されることを期待します。拡張するクラスを渡していますActionListener

それ以外の

bronzeG.addItemListener(radioHand);

使用する

bronzeG.addActionListener(radioHand);

アクションリスナーの書き方を読む

于 2013-09-11T13:31:38.483 に答える