gpt4 book ai didi

java - 尝试使用 JCheckBox 作为 JButton (Java Swing)

转载 作者:行者123 更新时间:2023-12-02 06:32:02 25 4
gpt4 key购买 nike

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

我收到一条错误消息,指出我无法将 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);
}
}

最佳答案

该错误是由于尝试将 JCheckBox 转换为 JButton 造成的。这种强制转换是不必要的,因为您永远不会使用任何 JButton 注册此监听器,而只能使用 ckbHotel (即 JCheckBox)注册此监听器。

在给定的示例中,不需要强制转换,您只需检查源即可:

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

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

ckbHotel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
hotelJDialog.setVisible(true);
}
});

关于java - 尝试使用 JCheckBox 作为 JButton (Java Swing),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19983422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com