gpt4 book ai didi

java - 组合框和按钮在 java 中不能一起工作

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

我正在为我的计算机科学课做一个日历项目。目前我正在开发一个主菜单,但我似乎遇到了一个我无法摆脱的错误!当我运行程序时,下拉菜单工作正常,但是当我单击按钮时,终端上会显示一堆错误:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: 
javax.swing.JButton cannot be cast to javax.swing.JComboBox

看来是主要的。任何帮助,将不胜感激!您可以在下面看到我的代码。

/*pictures used in this code were edited by Martha Ibarra. Original PNG image sources are listed below
http://imagespng.com/Detail/1093/White-cat-PNG-Transparent-image.html
http://www.freeiconspng.com/img/40367
http://www.susancalman.com/wp-content/uploads/2015/11/corner-cat.png*/


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

public class MainMenu implements ActionListener
{

private JPanel panel;
private JFrame frame;
private JButton addEvent;
private JButton delEvent;
private JLabel label;
private ImageIcon image;
TextField text = new TextField(20);
JLabel displayMessage = new JLabel("Select a CATlendar view or you can ADD or DELETE an event!");

public MainMenu()
{
gui();
}
///draws the main menu
public void gui()
{
frame = new JFrame("CATlendar");
frame.setVisible(true);
frame.setSize(600,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

FlowLayout lay = new FlowLayout();
panel= new JPanel();
panel.setBackground(Color.PINK);
panel.setLayout(lay);

addEvent = new JButton("ADD EVENT");
addEvent.setPreferredSize(new Dimension(200, 200));
addEvent.addActionListener(this);
ImageIcon catAdd = new ImageIcon(getClass().getResource("im-hungry.png"));
addEvent.setIcon(catAdd);


delEvent = new JButton("DELETE EVENT");
delEvent.setPreferredSize(new Dimension(200, 200));
delEvent.addActionListener(this);
ImageIcon catDel = new ImageIcon(getClass().getResource("tres-quatro.png"));
delEvent.setIcon(catDel);

image= new ImageIcon(getClass().getResource("uno-dos.png"));
label=new JLabel(image);

String[] viewList = { "Weekly", "Daily", "Monthly" };
JComboBox viewTypes = new JComboBox(viewList);
viewTypes.setSelectedIndex(0);
viewTypes.addActionListener(this);


panel.add(viewTypes);
panel.add(label);
panel.add(addEvent);
panel.add(delEvent);
frame.add(panel);
panel.add(displayMessage);
panel.setVisible(true);
}
//corresponding to what the user choses to do this method will change the event hashmap or display a calendar view.
public void actionPerformed(ActionEvent e)
{ JComboBox cb = (JComboBox)e.getSource();
String viewSelect = (String)cb.getSelectedItem();

if(e.getActionCommand().equals("ADD EVENT"))
{
panel.add(text);
displayMessage.setText("Enter the date of the event you would like to delete ");
String eventDate = text.getText();
}else if(e.getActionCommand().equals("DELETE EVENT"))
{
panel.add(text);
displayMessage.setText("Enter the date of the event you would like to add");
String eventDelDate = text.getText();
}else if(viewSelect.equals("Weekly"))
{
displayMessage.setText("You selected a weekly view");
}else if(viewSelect.equals("Daily"))
{
displayMessage.setText("You selected a daily view");
}else if(viewSelect.equals("Monthly"))
{
displayMessage.setText("You selected a monthly view");
}else
{
displayMessage.setText("pick a view");
}
}
public static void main(String [] args)
{
new MainMenu();
}

}

最佳答案

您的问题:如果事件源是按钮,您尝试访问组合框事件。如果您想在这种情况下访问组合框,您应该提供对此对象的引用。例如:

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

public class MainMenu implements ActionListener
{

private JPanel panel;
private JFrame frame;
private JButton addEvent;
private JButton delEvent;
private JLabel label;
private ImageIcon image;
private JComboBox viewTypes;
TextField text = new TextField(20);
JLabel displayMessage = new JLabel("Select a CATlendar view or you can ADD or DELETE an event!");

public MainMenu()
{
gui();
}
///draws the main menu
public void gui()
{

FlowLayout lay = new FlowLayout();
panel= new JPanel();
panel.setBackground(Color.PINK);
panel.setLayout(lay);

addEvent = new JButton("ADD EVENT");
addEvent.setPreferredSize(new Dimension(200, 200));
addEvent.addActionListener(this);
ImageIcon catAdd = new ImageIcon(getClass().getResource("im-hungry.png"));
addEvent.setIcon(catAdd);


delEvent = new JButton("DELETE EVENT");
delEvent.setPreferredSize(new Dimension(200, 200));
delEvent.addActionListener(this);
ImageIcon catDel = new ImageIcon(getClass().getResource("tres-quatro.png"));
delEvent.setIcon(catDel);

image= new ImageIcon(getClass().getResource("uno-dos.png"));
label=new JLabel("Test");

String[] viewList = { "Weekly", "Daily", "Monthly" };
viewTypes = new JComboBox(viewList);
viewTypes.setSelectedIndex(0);
viewTypes.addActionListener(this);


panel.add(viewTypes);
panel.add(label);
panel.add(addEvent);
panel.add(delEvent);
panel.add(displayMessage);
panel.setVisible(true);
frame = new JFrame("CATlendar");
frame.setVisible(true);
frame.setSize(600,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.pack();
}
//corresponding to what the user choses to do this method will change the event hashmap or display a calendar view.
public void actionPerformed(ActionEvent e) {
String viewSelect = (String)viewTypes.getSelectedItem();

if(e.getActionCommand().equals("ADD EVENT"))
{
panel.add(text);
displayMessage.setText("Enter the date of the event you would like to delete ");
String eventDate = text.getText();
}else if(e.getActionCommand().equals("DELETE EVENT"))
{
panel.add(text);
displayMessage.setText("Enter the date of the event you would like to add");
String eventDelDate = text.getText();
}else if(viewSelect.equals("Weekly"))
{
displayMessage.setText("You selected a weekly view");
}else if(viewSelect.equals("Daily"))
{
displayMessage.setText("You selected a daily view");
}else if(viewSelect.equals("Monthly"))
{
displayMessage.setText("You selected a monthly view");
}else
{
displayMessage.setText("pick a view");
}
panel.revalidate();
panel.repaint();
SwingUtilities.windowForComponent(panel).pack();
}
public static void main(String [] args)
{
new MainMenu();
}

}

关于java - 组合框和按钮在 java 中不能一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44637542/

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