1

当我按下 JCheckBox 时,我试图让 JDialog “弹出”(如果它不是真的)。我想用作按钮。我希望我发布这个是正确的,我在这里的第一篇文章:)

我收到一条错误消息,提示我无法将 JCheckBox 转换为 JButton。你能帮我做对吗?

封装模型;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class FirstJDialog extends JDialog 
{
    private Controller controller;

    private HotelJDialog hotelJDialog;
    private PartnerJDialog partnerJDialog;

    private JLabel lblName, lblArrival, lblDeparture, lblLekture, lblHotel, lblPartner;
    private JTextField txfName, txfArrival, txfDeparture;
    private JCheckBox ckbLekture, ckbHotel, ckbPartner;

    public FirstJDialog()
    {
        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        this.setTitle("Person informationer");
        this.setLayout(null);
        this.setSize(900, 650);
        this.setLocation(500, 200);
        this.setResizable(false);

        // Labels for TextField ######################################################################
        lblName = new JLabel("Navn:");
        this.add(lblName);
        this.lblName.setSize(100, 40);
        this.lblName.setLocation(25, 25);

        lblArrival = new JLabel("Ankomst:");
        this.add(lblArrival);
        this.lblArrival.setSize(100, 40);
        this.lblArrival.setLocation(25, 70);

        lblDeparture = new JLabel("Afgang:");
        this.add(lblDeparture);
        this.lblDeparture.setSize(100, 40);
        this.lblDeparture.setLocation(25, 115);

        // TextField ######################################################################
        txfName = new JTextField();
        this.add(txfName);
        this.txfName.setSize(200, 40);
        this.txfName.setLocation(180, 25);

        txfArrival = new JTextField();
        this.add(txfArrival);
        this.txfArrival.setSize(200, 40);
        this.txfArrival.setLocation(180, 70);

        txfDeparture = new JTextField();
        this.add(txfDeparture);
        this.txfDeparture.setSize(200, 40);
        this.txfDeparture.setLocation(180, 115);

        // CheckBox ######################################################################
        ckbLekture = new JCheckBox();
        this.add(ckbLekture);
        this.ckbLekture.setSize(25, 25);
        this.ckbLekture.setLocation(25, 200);

        ckbHotel = new JCheckBox();
        this.add(ckbHotel);
        this.ckbHotel.setSize(25, 25);
        this.ckbHotel.setLocation(25, 250);

        ckbPartner = new JCheckBox();
        this.add(ckbPartner);
        this.ckbPartner.setSize(25, 25);
        this.ckbPartner.setLocation(25, 300);

        // Labels for CheckBox ######################################################################
        lblLekture = new JLabel("Foredrag");
        this.add(lblLekture);
        this.lblLekture.setSize(100, 40);
        this.lblLekture.setLocation(125, 190);

        lblHotel = new JLabel("Hotel");
        this.add(lblHotel);
        this.lblHotel.setSize(100, 40);
        this.lblHotel.setLocation(125, 240);

        lblPartner = new JLabel("Ledsager");
        this.add(lblPartner);
        this.lblPartner.setSize(100, 40);
        this.lblPartner.setLocation(125, 290);

        // JDialogs ######################################################################
        hotelJDialog = new HotelJDialog();
        partnerJDialog = new PartnerJDialog();

        controller = new Controller();
        ckbHotel.addActionListener(controller);
    }

    private class Controller implements ActionListener
    {                   

        public void actionPerformed(ActionEvent e) 
        {
            JButton source = (JButton) e.getSource();
            JCheckBox sourxe = (JCheckBox) e.getSource();
            if(sourxe.equals(ckbHotel))
                hotelJDialog.setVisible(true);
        }   
    }
4

2 回答 2

2

该错误是由于尝试强制JCheckBox转换为JButton. 这个演员表是不必要的,因为你永远不会用 any 注册这个监听器JButton,只有ckbHotelis JCheckBox

在给定的示例中,不需要强制转换,您可以简单地检查源:

if(ckbHotel.equals(e.getSource()))
    hotelJDialog.setVisible(true);

您还可以为此复选框注册不同的操作侦听器。无需使用单个动作侦听器来为容器中的所有控件提供服务,例如:

ckbHotel.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        hotelJDialog.setVisible(true);
    }
});
于 2013-11-14T16:52:37.970 回答
0
 JButton source = (JButton) e.getSource(); 

这一行给出了这个错误。删除这个 ans 添加这个

JCheckBox sourxe = (JCheckBox) e.getSource();
if(sourxe.isSelected())
      hotelJDialog.setVisible(true);

如果选择了源,这仅显示您的对话框..

于 2013-11-14T16:54:23.697 回答