组合框和按钮在 java 中不能一起使用
combobox and button not working together in java
我正在为我的计算机科学做一个日历项目 class。目前我正在开发一个主菜单,但我似乎 运行 陷入了一个我无法摆脱的错误!当我 运行 程序时,下拉菜单工作正常,但是当我单击按钮时,终端上会出现一堆错误:
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();
}
}
我正在为我的计算机科学做一个日历项目 class。目前我正在开发一个主菜单,但我似乎 运行 陷入了一个我无法摆脱的错误!当我 运行 程序时,下拉菜单工作正常,但是当我单击按钮时,终端上会出现一堆错误:
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();
}
}