I have problem with this code
when I click on file and click on new ,new panel comes to screen and when I want to change JRadioBox status to change Label status,Label status changes but also the panel goes away :(
public class MainClass {
public static void main(String[] args) {
new MainFrame();
}
}
class Toolbar extends JPanel {
private JRadioButton Status1;
private JRadioButton Status2;
private ButtonGroup radioButtonGroup;
public Toolbar() {
super();
setLayout(new FlowLayout());
Status1 = new JRadioButton("Status1");
Status2 = new JRadioButton("Status2");
radioButtonGroup = new ButtonGroup();
radioButtonGroup.add(Status2);
radioButtonGroup.add(Status1);
Status1.addItemListener(new ItemListener() {
#Override
public void itemStateChanged(ItemEvent e) {
MainFrame m = new MainFrame();
m.l.setText("Status1");
}
});
Status2.addItemListener(new ItemListener() {
#Override
public void itemStateChanged(ItemEvent e) {
MainFrame m = new MainFrame();
m.l.setText("Status2");
}
});
add(Status1);
add(Status2);
}
}
class Panel extends JPanel {
public Panel() {
super();
setBackground(Color.MAGENTA);
}
}
class MenuBar extends JMenuBar {
private JMenu menu;
private JMenuItem fileItems;
public boolean panel = false;
public MenuBar() {
super();
menu = new JMenu("File");
add(menu);
fileItems = new JMenuItem("New");
menu.add(fileItems);
fileItems.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
MainFrame mf = new MainFrame();
Panel p = new Panel();
mf.addPanel(p);
mf.add(new Toolbar(), BorderLayout.NORTH);
repaint();
}
});
}
}
class MainFrame extends JFrame {
public static JLabel l;
public MainFrame() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400, 400);
l = new JLabel("No Status");
add(l, BorderLayout.SOUTH);
MenuBar mb = new MenuBar();
setJMenuBar(mb);
setVisible(true);
}
public void addPanel(Panel p) {
add(p, BorderLayout.CENTER);
}
}
Stop making new MainFrames all over the place. Create it once and maintain a handle to it for whenever you need it.
Related
I want to have a menu bar in my GUI. The menu is not visible.
public class GUI extends JPanel implements ItemListener{
final static String RUN_TEST = "Test 4G";
final static String SETTINGS = "Settings";
JPanel p;
JPanel cards = new JPanel(new CardLayout());
public GUI(){
JFrame window = new JFrame();
TestRun runTest = new TestRun();
cards.add(runTest , RUN_TEST);
cards.add(runTest , SETTINGS);
CardLayout cl = (CardLayout)(cards.getLayout());
cl.show(cards, RUN_TEST);
window.setContentPane(cards);
window.pack();
window.setVisible(true);
}
#Override
public void itemStateChanged(ItemEvent evt) {
CardLayout cl = (CardLayout)(cards.getLayout());
cl.show(cards, (String)evt.getItem());
}
}
How can I show to the user the menu "Test 4G" and "settings" so that they can change the JPanel?
Thanks for your help
This is an example of using JMenuBar in JFrame and JPopupMenu in JPanel (view).
public class MainFrame extends JFrame {
final static String RUN_TEST = "Test 4G";
final static String SETTINGS = "Settings";
private JPanel viewPanel = new JPanel();
public MainFrame() throws HeadlessException {
super("MainFrame");
cretaeGUI();
}
private void cretaeGUI() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLayout(new BorderLayout());
setJMenuBar(cretaeMenuBar());
setMinimumSize(new Dimension(800, 600));
viewPanel.setLayout(new CardLayout());
viewPanel.add(new Test4GView(this), RUN_TEST);
viewPanel.add(new SettingsView(this), SETTINGS);
add(viewPanel, BorderLayout.CENTER);
pack();
setLocationRelativeTo(null);
}
private JMenuBar cretaeMenuBar() {
JMenuItem testMenuItem = new JMenuItem("Test 4G");
testMenuItem.addActionListener(this::showTest4GView);
JMenuItem settingsMenuItem = new JMenuItem("Settings");
settingsMenuItem.addActionListener(this::showSettingsView);
JMenu viewMenu = new JMenu("View");
viewMenu.add(testMenuItem);
viewMenu.add(settingsMenuItem);
JMenuBar menuBar = new JMenuBar();
menuBar.add(viewMenu);
return menuBar;
}
private void showView(String name) {
((CardLayout)viewPanel.getLayout()).show(viewPanel, name);
}
public void showTest4GView(ActionEvent event) {
showView(RUN_TEST);
}
public void showSettingsView(ActionEvent event) {
showView(SETTINGS);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new MainFrame().setVisible(true));
}
}
аnd these are both views
public class Test4GView extends JPanel {
private MainFrame mainFrame;
public Test4GView(MainFrame mainFrame) {
this.mainFrame = mainFrame;
add(new JLabel("Test 4G"));
addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
showPopupMenu(e);
}
#Override
public void mouseReleased(MouseEvent e) {
showPopupMenu(e);
}
private void showPopupMenu(MouseEvent e) {
if(!e.isPopupTrigger()) {
return;
}
JMenuItem showSettingsView = new JMenuItem("Settings");
showSettingsView.addActionListener(mainFrame::showSettingsView);
JPopupMenu popupMenu = new JPopupMenu();
popupMenu.add(showSettingsView);
popupMenu.show(e.getComponent(), e.getX(), e.getY());
}
});
}
}
public class SettingsView extends JPanel {
private MainFrame mainFrame;
public SettingsView(MainFrame mainFrame) {
this.mainFrame = mainFrame;
add(new JLabel("Settings"));
addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
showPopupMenu(e);
}
#Override
public void mouseReleased(MouseEvent e) {
showPopupMenu(e);
}
private void showPopupMenu(MouseEvent e) {
if(!e.isPopupTrigger()) {
return;
}
JMenuItem showSettingsView = new JMenuItem("Test 4G");
showSettingsView.addActionListener(mainFrame::showTest4GView);
JPopupMenu popupMenu = new JPopupMenu();
popupMenu.add(showSettingsView);
popupMenu.show(e.getComponent(), e.getX(), e.getY());
}
});
}
}
I've created 3 buttons. They are each displayed twice in a JFrame. I'm having trouble changing the background of the frame. I've set ActionListeners but upon clicking, nothing is changing. May I ask for some help.
public class MyButtons extends JPanel {
public static JFrame frame;
private JButton Red = new JButton("Red");
private JButton Green = new JButton("Green");
private JButton Blue = new JButton("Blue");
public void InitializeButton()
{
Blue.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
frame.setBackground(Color.BLUE);
}
});
Green.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
frame.setBackground(Color.GREEN);
}
});
Red.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
frame.setBackground(Color.RED);
}
});
}
public MyButtons() {
InitializeButton();
add(Red);
add(Green);
add(Blue);
}
public static void main(String[] args) {
frame = new JFrame();
JPanel row1 = new MyButtons();
JPanel row2 = new MyButtons();
row1.setPreferredSize(new Dimension(250, 100));
row2.setPreferredSize(new Dimension(250, 100));
frame.setLayout(new GridLayout(3,2));
frame.add(row1);
frame.add(row2);
frame.pack();
frame.setVisible(true);
}
}
This code works, but probably not as you expected:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyButtons extends JPanel {
//public static JFrame frame;
// static is rarely a solution of problems in a GUI. ToDo! Change!
static JPanel ui = new JPanel(new GridLayout(2, 0, 20, 20));
private JButton Red = new JButton("Red");
private JButton Green = new JButton("Green");
private JButton Blue = new JButton("Blue");
public void InitializeButton() {
Blue.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ui.setBackground(Color.BLUE);
}
});
Green.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ui.setBackground(Color.GREEN);
}
});
Red.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ui.setBackground(Color.RED);
}
});
}
public MyButtons() {
InitializeButton();
add(Red);
add(Green);
add(Blue);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(250, 100);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel row1 = new MyButtons();
JPanel row2 = new MyButtons();
//row1.setPreferredSize(new Dimension(250, 100));
//row2.setPreferredSize(new Dimension(250, 100));
//frame.setLayout(new GridLayout(3, 2,10,10));
ui.add(row1);
ui.add(row2);
frame.add(ui);
frame.pack();
frame.setVisible(true);
}
}
N.B. Please learn common Java nomenclature (naming conventions - e.g. EachWordUpperCaseClass, firstWordLowerCaseMethod(), firstWordLowerCaseAttribute unless it is an UPPER_CASE_CONSTANT) and use it consistently.
I need to make GUI which saves the user's input using a linked list and let him/her "browse" through the saved data. I made a simpler version of the code which I included below. My problem is how to "update" the buttons in the browse card after the user entered and saved a text.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class sampleDemo extends JPanel {
NodeSample newNode = new NodeSample();
SampleSave s = new SampleSave();
public static final String CARD_ADD = "Card Add";
public static final String CARD_BROWSE = "Card Browse";
public static CardLayout cardLayout = new CardLayout();
public static JPanel card = new JPanel(cardLayout);
public static sampleDemo sd = new sampleDemo();
public sampleDemo() {
WindowAdd wina = new WindowAdd();
card.add(wina, CARD_ADD);
WindowBrowse winb = new WindowBrowse();
card.add(winb, CARD_BROWSE);
setLayout(new BorderLayout());
add(card, BorderLayout.PAGE_END);
}
public static void createAndShowGUI() {
JPanel buttonPanel = new JPanel();
JButton addButton = new JButton("Add");
JButton browseButton = new JButton("Browse");
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
cardLayout.show(card, "Card Add");
}
});
browseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
cardLayout.show(card, "Card Browse");
}
});
buttonPanel.add(addButton);
buttonPanel.add(browseButton);
JFrame frame = new JFrame("Sample Demo");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(buttonPanel, BorderLayout.PAGE_END);
frame.getContentPane().add(sd);
frame.setSize(300, 200);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
class WindowAdd extends JPanel {
public ActionListener action;
public WindowAdd() {
init();
}
public void init() {
JTextField textField = new JTextField(10);
NodeSample newNode = new NodeSample();
JPanel content = new JPanel(new FlowLayout());
JButton saveButton = new JButton("Save");
saveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
newNode.textContent = textField.getText();
s.insert(newNode);
}
});
content.add(textField);
content.add(saveButton);
textField.setText(null);
add(content);
}
}
class WindowBrowse extends JPanel {
public WindowBrowse() {
init();
}
public void init() {
setLayout(new GridLayout(1, 3));
JButton a = new JButton();
JButton b = new JButton();
JButton c = new JButton();
try {
a = new JButton(s.head.textContent);
b = new JButton(s.head.next.textContent);
c = new JButton(s.head.next.next.textContent);
}catch (NullPointerException e) {
//
}
add(a);
add(b);
add(c);
}
}
class NodeSample {
String textContent;
NodeSample next;
}
class SampleSave {
NodeSample head;
NodeSample tail;
public void insert(NodeSample add) {
if(head == null){
head = add;
tail = add;
}else {
add.next = head;
head = add;
}
}
}
}
In order to change the text on a JButton use something similar to this in your action listener
btn.setText(textfield.getText());
This will change the text of whatever button to the text entered in the textfield
I'm using webcam-capture libraries and AWT to develop a simple interface for taking pictures from a webcam. The buttons and the combobox in my JFrame disappear after minimizing the window or after moving another window on top of it. Moving the pointer over the frame restores the components' visibility. I'm not skilled with Java UI, I can't figure out what's wrong with my code.
#SuppressWarnings("serial")
public class ImageCaptureManager extends JFrame {
private class SkipCapture extends AbstractAction {
public SkipCapture() {
super(“Skip”);
}
#Override
public void actionPerformed(ActionEvent e) {
/*SOME CODE HERE*/
}
}
private class SnapMeAction extends AbstractAction {
public SnapMeAction() {
super(“Snap”);
}
#Override
public void actionPerformed(ActionEvent e) {
/*SOME CODE HERE*/
}
}
private class captureCompleted extends AbstractAction {
public captureCompleted() {
super(“Completed”);
}
#Override
public void actionPerformed(ActionEvent e) {
/*SOME CODE HERE*/
}
}
private class saveImage extends AbstractAction {
public saveImage() {
super(“Save”);
}
#Override
public void actionPerformed(ActionEvent e) {
/*SOME CODE HERE*/
}
}
private class deleteImage extends AbstractAction {
public deleteImage() {
super(“Delete”);
}
#Override
public void actionPerformed(ActionEvent e) {
/*SOME CODE HERE*/
}
}
private class StartAction extends AbstractAction implements Runnable {
public StartAction() {
super(“Start”);
}
#Override
public void actionPerformed(ActionEvent e) {
btStart.setEnabled(false);
btSnapMe.setEnabled(true);
executor.execute(this);
}
#Override
public void run() {
panel.start();
}
}
private Executor executor = Executors.newSingleThreadExecutor();
private Dimension captureSize = new Dimension(640, 480);
private Dimension displaySize = new Dimension(640, 480);
private Webcam webcam = Webcam.getDefault();
private WebcamPanel panel;
private JButton btSnapMe = new JButton(new SnapMeAction());
private JButton btStart = new JButton(new StartAction());
private JButton btComplete = new JButton(new captureCompleted());
private JButton btSave = new JButton(new saveImage());
private JButton btDelete = new JButton(new deleteImage());
private JButton btSkip = new JButton(new SkipCapture());
private JComboBox comboBox = new JComboBox();
public ImageCaptureManager() {
super(“Frame”);
this.addWindowListener( new WindowAdapter()
{
#Override
public void windowDeiconified(WindowEvent arg0) {
}
public void windowClosing(WindowEvent e)
{
}
});
List<Webcam> webcams = Webcam.getWebcams();
for (Webcam webcam : webcams) {
System.out.println(webcam.getName());
if (webcam.getName().startsWith("USB2.0 Camera 1")) {
this.webcam = webcam;
break;
}
}
panel = new WebcamPanel(webcam, displaySize, false);
webcam.setViewSize(captureSize);
panel.setFPSDisplayed(true);
panel.setFillArea(true);
btSnapMe.setEnabled(false);
btSave.setEnabled(false);
btDelete.setEnabled(false);
setLayout(new FlowLayout());
Panel buttonPanel = new Panel();
buttonPanel.setLayout(new GridLayout(10, 1));
buttonPanel.add(Box.createHorizontalStrut(20));
buttonPanel.add(btSnapMe);
buttonPanel.add(Box.createHorizontalStrut(20));
buttonPanel.add(btSave);
buttonPanel.add(Box.createHorizontalStrut(20));
buttonPanel.add(btDelete);
buttonPanel.add(Box.createHorizontalStrut(20));
buttonPanel.add(btComplete);
buttonPanel.add(Box.createHorizontalStrut(20));
buttonPanel.add(btSkip);
JLabel label1 = new JLabel("Test");
label1.setText(“Bla bla bla”);
JLabel label2 = new JLabel("Test");
label2.setText(" ");
Panel captionAndWebcamPanel = new Panel();
captionAndWebcamPanel.add(label1);
captionAndWebcamPanel.add(label2);
captionAndWebcamPanel.add(panel);
captionAndWebcamPanel.add(label2);
captionAndWebcamPanel.add(comboBox);
captionAndWebcamPanel.setLayout(new BoxLayout(captionAndWebcamPanel, BoxLayout.Y_AXIS));
add(captionAndWebcamPanel);
add(buttonPanel);
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
btStart.doClick();
setSize(900,600);
}
}
You are mixing AWT and Swing components.
"Historically, in the Java language, mixing heavyweight and lightweight components in the same container has been problematic."
http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html
I suggest you try using JPanels instead of Panels for captionAndWebcamPanel and buttonPanel, I'd also set layout to captionAndWebcamPanel before adding components.
I know it's something to do with how I've set it up and the actionlistener not being correctly set to the frame or something but I just can't get my hear around it. If someone could point me in the right direction I'd be much obliged. Sorry for noob question.
Here's what I have:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main implements ActionListener {
JPanel cardHolder;
public static final String HOME_CARD = "Home";
public static final String BLUE_PANEL = "Blue Panel";
public static final String RED_PANEL = "Red Panel";
public static final String ORANGE_PANEL = "Orange Panel";
public static JButton home = new JButton("Home");
public static JButton bluePanel = new JButton("Blue Card");
public static JButton redPanel = new JButton("Red Panel");
public static JButton orangePanel = new JButton("Orange Panel");
public static JPanel createCardHolderPanel() {
JPanel cardHolder = new JPanel(new CardLayout());
cardHolder.setBorder(BorderFactory.createTitledBorder("Card Holder Panel"));
cardHolder.add(createHomeCard(), HOME_CARD);
cardHolder.add(createBluePanel(), BLUE_PANEL);
cardHolder.add(createRedPanel(), RED_PANEL);
cardHolder.add(createOrangePanel(), ORANGE_PANEL);
return cardHolder;
}
private static JPanel createOrangePanel() {
JPanel orangePanel = new JPanel();
orangePanel.setBackground(Color.orange);
return orangePanel;
}
private static Component createRedPanel() {
JPanel redPanel = new JPanel();
redPanel.setBackground(Color.red);
return redPanel;
}
private static Component createBluePanel() {
JPanel bluePanel = new JPanel();
bluePanel.setBackground(Color.blue);
return bluePanel;
}
private static Component createHomeCard() {
JPanel homePanel = new JPanel();
homePanel.setBackground(Color.GRAY);
return homePanel;
}
public static JPanel createButtonPanel() {
JPanel buttonPanel = new JPanel(new GridLayout(4, 0, 5, 5));
buttonPanel.setBorder(BorderFactory.createTitledBorder("Button Panel"));
buttonPanel.add(home);
buttonPanel.add(bluePanel);
buttonPanel.add(redPanel);
buttonPanel.add(orangePanel);
return buttonPanel;
}
public static JPanel createContentPane() {
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.setBorder(BorderFactory.createTitledBorder("Main Content Pane"));
contentPane.setBackground(Color.WHITE);
contentPane.setPreferredSize(new Dimension(499, 288));
contentPane.add(createButtonPanel(), BorderLayout.WEST);
contentPane.add(createCardHolderPanel(),BorderLayout.CENTER);
return contentPane;
}
public static JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu file = new JMenu("File");
JMenu users = new JMenu("Users");
JMenu options = new JMenu("Options");
JMenu help = new JMenu("Help");
menuBar.add(file);
menuBar.add(users);
menuBar.add(options);
menuBar.add(help);
return menuBar;
}
public static void createAndShowGUI() {
JFrame frame = new JFrame("Simple CardLayout Program");
frame.setContentPane(createContentPane());
frame.setJMenuBar(createMenuBar());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == home) {
CardLayout cardLayout = (CardLayout) (cardHolder.getLayout());
cardLayout.show(cardHolder, HOME_CARD);
}
if (e.getSource() == bluePanel) {
CardLayout cardLayout = (CardLayout) (cardHolder.getLayout());
cardLayout.show(cardHolder, BLUE_PANEL);
}
if (e.getSource() == redPanel) {
CardLayout cardLayout = (CardLayout) (cardHolder.getLayout());
cardLayout.show(cardHolder, RED_PANEL);
}
if (e.getSource() == orangePanel) {
CardLayout cardLayout = (CardLayout) (cardHolder.getLayout());
cardLayout.show(cardHolder, ORANGE_PANEL);
}
}
}
Others have suggested listening to the buttons; in addition:
Prefer the lowest accessibility consistent with use, e.g. private rather than public.
Don't make everything static.
Use static for immutable constants used throughout the class.
Use class variables rather than static members for content.
Don't repeat your self, e.g. initialize cardLayout just once in your actionPerformed)().
Use parameters rather than separate methods for each color, e.g.
private JPanel createColorPanel(Color color) {...}
Revised code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main implements ActionListener {
private static final String HOME_CARD = "Home";
private static final String BLUE_PANEL = "Blue Panel";
private static final String RED_PANEL = "Red Panel";
private static final String ORANGE_PANEL = "Orange Panel";
private JPanel cardHolder;
private JButton homeButton = new JButton("Home");
private JButton blueButton = new JButton("Blue Card");
private JButton redButton = new JButton("Red Panel");
private JButton orangeButton = new JButton("Orange Panel");
public JPanel createCardHolderPanel() {
cardHolder = new JPanel(new CardLayout());
cardHolder.setBorder(BorderFactory.createTitledBorder("Card Holder Panel"));
cardHolder.add(createColorPanel(Color.gray), HOME_CARD);
cardHolder.add(createColorPanel(Color.blue), BLUE_PANEL);
cardHolder.add(createColorPanel(Color.red), RED_PANEL);
cardHolder.add(createColorPanel(Color.orange), ORANGE_PANEL);
return cardHolder;
}
private JPanel createColorPanel(Color color) {
JPanel panel = new JPanel();
panel.setBackground(color);
return panel;
}
public JPanel createButtonPanel() {
JPanel buttonPanel = new JPanel(new GridLayout(4, 0, 5, 5));
buttonPanel.setBorder(BorderFactory.createTitledBorder("Button Panel"));
buttonPanel.add(homeButton);
buttonPanel.add(blueButton);
buttonPanel.add(redButton);
buttonPanel.add(orangeButton);
homeButton.addActionListener(this);
blueButton.addActionListener(this);
redButton.addActionListener(this);
orangeButton.addActionListener(this);
return buttonPanel;
}
public JPanel createContentPane() {
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createTitledBorder("Main Content Pane"));
panel.setBackground(Color.WHITE);
panel.setPreferredSize(new Dimension(499, 288));
panel.add(createButtonPanel(), BorderLayout.WEST);
panel.add(createCardHolderPanel(), BorderLayout.CENTER);
return panel;
}
public JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu file = new JMenu("File");
JMenu users = new JMenu("Users");
JMenu options = new JMenu("Options");
JMenu help = new JMenu("Help");
menuBar.add(file);
menuBar.add(users);
menuBar.add(options);
menuBar.add(help);
return menuBar;
}
#Override
public void actionPerformed(ActionEvent e) {
CardLayout cardLayout = (CardLayout) (cardHolder.getLayout());
if (e.getSource() == homeButton) {
cardLayout.show(cardHolder, HOME_CARD);
}
if (e.getSource() == blueButton) {
cardLayout.show(cardHolder, BLUE_PANEL);
}
if (e.getSource() == redButton) {
cardLayout.show(cardHolder, RED_PANEL);
}
if (e.getSource() == orangeButton) {
cardLayout.show(cardHolder, ORANGE_PANEL);
}
}
public static void createAndShowGUI() {
JFrame frame = new JFrame("Simple CardLayout Program");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Main main = new Main();
frame.setJMenuBar(main.createMenuBar());
frame.add(main.createContentPane());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
createAndShowGUI();
}
});
}
}
You have not set an action listener for any of the buttons. Implementing the actionPerformed method of the interface does not automatically set an action listener for the buttons. You have to call the addActionListener method which in your case would look like the following since your class implements the ActionListener Interface.
public static JButton home = new JButton("Home").addActionListener(this);
public static JButton bluePanel = new JButton("Blue Card").addActionListener(this);
public static JButton redPanel = new JButton("Red Panel").addActionListener(this);
public static JButton orangePanel = new JButton("Orange Panel").addActionListener(this);