Click here to Skip to main content
15,915,319 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm having an issue where my buttons don't show up at all.

What I have tried:

package journal.pkg7a;
/**
*
* @author stephenwessels
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Journal7A extends JFrame implements ActionListener
{
Container content = new Container();
JButton btnGreen = new JButton("Green");
JButton btnBlue = new JButton("Blue");
JButton btnRed = new JButton("Red");

public Journal7A()
{
content.setLayout(new FlowLayout());
content.add(btnBlue, BorderLayout.SOUTH);
content.add(btnGreen, BorderLayout.SOUTH);
content.add(btnRed, BorderLayout.SOUTH);

this.setVisible(true);
this.setSize(300,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("First GUI App");


btnBlue.addActionListener(this);
btnGreen.addActionListener(this);
btnRed.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e)
{
JButton btn = (JButton) e.getSource();
if(btn == btnBlue)
content.setBackground(Color.BLUE);
else
content.setBackground(Color.GREEN);
content.setBackground(Color.RED);
}

/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
Journal7A gui = new Journal7A();
}

}
Posted
Updated 26-Feb-18 17:49pm

1 solution

There is no need for your Journal7A class to subclass JFrame. Instead, you can extends your class to JPanel, and create frame object in your main.
public class Journal7A extends JPanel implements ActionListener {

    JPanel content = new JPanel();
    JButton btnGreen = new JButton("Green");
    JButton btnBlue = new JButton("Blue");
    JButton btnRed = new JButton("Red");

    public Journal7A() {
        content.setLayout(new FlowLayout());
        content.add(btnBlue, BorderLayout.SOUTH);
        content.add(btnGreen, BorderLayout.SOUTH);
        content.add(btnRed, BorderLayout.SOUTH);
        btnBlue.addActionListener(this);
        btnGreen.addActionListener(this);
        btnRed.addActionListener(this);
        add(content);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton btn = (JButton) e.getSource();
        if (btn == btnBlue) {
            content.setBackground(Color.BLUE);
        } else {
            content.setBackground(Color.GREEN);
        }
        content.setBackground(Color.RED);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Journal7A gui = new Journal7A();
        Frame frame = new JFrame();
        frame.add(gui);
        frame.pack();
        frame.setSize(300, 300);
        frame.setTitle("First GUI App");
        frame.setVisible(true);
    }

}

If you like to use your own code , add
add(content);
after line
btnRed.addActionListener(this);
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900