[英]Trying to use a JCheckBox as a JButton (Java Swing)
i'm trying to get a JDialog to "pop up" when i press a JCheckBox (if its not true). 当我按下JCheckBox时,我正试图让JDialog“弹出”(如果它不是真的)。 I want to use is as a button. 我想用的是按钮。 I hope i am posting this right, my first post here :) 我希望我发布这个权利,我的第一篇文章:)
I am getting a error that says i can't cast my JCheckBox to a JButton. 我收到一个错误,说我无法将JCheckBox强制转换为JButton。 Can you help me do this right? 你能帮我做到这一点吗?
package model; 包装模型;
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);
}
}
The error is a result of an attempt to cast JCheckBox
to a JButton
. 该错误是尝试将JCheckBox
转换为JButton
。 This cast is unnecessary, as you never register this listener with any JButton
, only with ckbHotel
which is JCheckBox
. 这种ckbHotel
是不必要的,因为你从来没有用任何JButton
注册这个监听器,只有ckbHotel
是JCheckBox
。
In the given example, there is no need to cast, you can simply check the source: 在给定的示例中,不需要强制转换,您只需检查源:
if(ckbHotel.equals(e.getSource()))
hotelJDialog.setVisible(true);
You can also register a different action listener for this checkbox. 您还可以为此复选框注册其他操作侦听器。 There is no need to have a single action listener to serve all the controls in the container, for example: 没有必要使用单个动作侦听器来提供容器中的所有控件,例如:
ckbHotel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
hotelJDialog.setVisible(true);
}
});
JButton source = (JButton) e.getSource();
this line give this error. 这一行给出了这个错误。 remove this ans add this 删除此ans添加此项
JCheckBox sourxe = (JCheckBox) e.getSource();
if(sourxe.isSelected())
hotelJDialog.setVisible(true);
this is only show your dialog if sourxe is selected.. 如果选择了sourxe,这只显示你的对话框..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.