当我按下 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);
}
}