Program doesn't recognize inner classes in java - java

I want to use 2 buttons on the same JFrame to do different task each. One to change the label on the right and one to change the color of the circle in the middle. (The random changing color is on another class.)
For some unknown reason the program doesn't seem to recognize the inner classes which exists inside the main class (class TwoButtons). I am pretty new to java and I cannot find what I am doing wrong.... Could you please help me to resolve my problem?
package twoButtonsPackage;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TwoButtons {
JFrame frame;
JLabel label;
public static void main(String[] args) {
TwoButtons gui = new TwoButtons();
gui.go();
}
public void go(){
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton labelButton = new JButton("Change label");
labelButton.addActionListener(new LabelListener());
JButton colorButton = new JButton("Change cirle");
colorButton.addActionListener(new ColorListener());
label = new JLabel("I'm a labele");
MyDrawPanel drawPanel = new MyDrawPanel();
frame.getContentPane().add(BorderLayout.SOUTH, colorButton);
frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
frame.getContentPane().add(BorderLayout.WEST, labelButton);
frame.getContentPane().add(BorderLayout.EAST, label);
frame.setSize(300, 300);
frame.setVisible(true);
class LabelListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
label.setText("Ouch!");
}
}
class ColorListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
frame.repaint();
}
}
}
}
I get an error on
labelButton.addActionListener(new LabelListener());
and on
colorButton.addActionListener(new ColorListener());
It says on both occasions that both LabelListener and ColorListener cannot resolved as a type.
Thank you very much in advance..!!

You need to move the classes LabelListener and ColorListener out of the public void go() Method
class LabelListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
label.setText("Ouch!");
}
}
and
class ColorListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
frame.repaint();
}
}

In java, you cannot define classes (just like variables, in methods) in methods after you use them, so, instead try defining the classes ColorListener and LabelListener within the class TwoButtons, instead of defining them in the go method, like so: (This is generally better practice)
package twoButtonsPackage;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TwoButtons {
JFrame frame;
JLabel label;
public static void main(String[] args) {
TwoButtons gui = new TwoButtons();
gui.go();
}
public void go(){
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton labelButton = new JButton("Change label");
labelButton.addActionListener(new LabelListener());
JButton colorButton = new JButton("Change cirle");
colorButton.addActionListener(new ColorListener());
label = new JLabel("I'm a labele");
MyDrawPanel drawPanel = new MyDrawPanel();
frame.getContentPane().add(BorderLayout.SOUTH, colorButton);
frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
frame.getContentPane().add(BorderLayout.WEST, labelButton);
frame.getContentPane().add(BorderLayout.EAST, label);
frame.setSize(300, 300);
frame.setVisible(true);
}
class LabelListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
label.setText("Ouch!");
}
}
class ColorListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
frame.repaint();
}
}
}

Related

java GUI Form open other form onclick button

I am trying to do when clicked button from form1 open form2. Its sounds very simple but i coudnt find any way to do this.I am using java intellij.
When i use netbeans and swing i was doing this with :
"Form2 form2=new Form2();
form2.setVisible(true);
dispose(); "
Form1(Main):
public class Main {
private JButton b_show;
private JButton b_Add;
private JPanel jp_main;
public Main() {
b_show.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent actionEvent) {
}
});
}
public static void main(String[]args){
JFrame frame=new JFrame();
frame.setContentPane(new Main().jp_main);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setVisible(true);
}
}
form2(Show):
public class Show {
private JButton b_back;
public JPanel jpanelmain;
public Show() {
Show show=new Show();
geriButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent actionEvent) {
}
});
}
public static void main(String[]args){
JFrame frame=new JFrame();
frame.setContentPane(new Show().jpanelmain);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setVisible(true);
}
}
is any one can help me ?
when click b_show open form2(Show).
Here is an mcve demonstrating it
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main {
private final JButton b_show;
private final JPanel jp_main;
public Main() {
jp_main = new JPanel();
b_show = new JButton("Show");
b_show.addActionListener(actionEvent -> {
new Show();
});
jp_main.add(b_show);
}
public static void main(String[]args){
JFrame frame=new JFrame();
frame.setContentPane(new Main().jp_main);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setVisible(true);
}
}
class Show {
private JButton b_back;
public JPanel jpanelmain;
public Show() {
createAndShowGui();
}
void createAndShowGui(){
JFrame frame=new JFrame();
frame.setLocationRelativeTo(null);
jpanelmain = new JPanel();
b_back = new JButton("Back");
jpanelmain.add(b_back);
frame.setContentPane(jpanelmain);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setVisible(true);
}
}
However, please read The Use of Multiple JFrames: Good or Bad Practice?
The best way to do this would be using JDialogs. When actionPerformed() at 'Form1' is called, you would instantiate a new JDialog and set him visible. Here is an example:
public class Show extends JDialog {
private JButton b_back;
public JPanel jpanelmain;
public Show(Frame owner, boolean modal) {
super(owner, modal);
}
//method that creates the GUI
}
b_show.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent actionEvent) {
Show show = new Show(JOptionPane.getFrameForComponent(this), true);
show.setVisible(true);
}
});
Finally, when you want to close the dialog, implement an actionPerformed() in it, and call the dispose() method

I can't change the color of a JButton when pressed

I have all the imports needed and there are no errors but it won't work.
final JButton button_32 = new JButton("2");
button_32.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
button_32.setBackground(Color.red);
}
});
button_32.setBounds(0, 57, 33, 29);
contentPane.add(button_32);
You can create your own Button, which extends ButtonModel or just do it, as suggested here.
public class Main {
static JFrame frame;
public static void main(String[] args)
{
// schedule this for the event dispatch thread (edt)
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
displayJFrame();
}
});
}
static void displayJFrame()
{
frame = new JFrame("Our JButton listener example");
// create our jbutton
final JButton showDialogButton = new JButton("Click Me");
// add the listener to the jbutton to handle the "pressed" event
showDialogButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// when the button is pressed
showDialogButton.setBackground(Color.RED);
}
});
// put the button on the frame
frame.getContentPane().setLayout(new FlowLayout());
frame.add(showDialogButton);
// set up the jframe, then display it
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(300, 200));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
I think it can be related to "implementation of the abstract class".
Try this:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class ExamButton extends JFrame {
JButton button_32 = new JButton("ssf");
JFrame frame = new JFrame();
public ExamButton() {
final JButton button_32 = new JButton("2");
button_32.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
button_32.setBackground(Color.red);
}
});
button_32.setBounds(0, 57, 33, 29);
add(button_32, BorderLayout.CENTER);
setSize(300, 300);
setVisible(true);
}
public static void main(String[] args) {
new ExamButton();
}
}

How do i add an action to a JFrame JButton?

I have a JFrame gui im making and I need help.
Im trying to find out how to add a action to my button.
as in using it in a "if" statement or make i print something out when you push it.
thanks!
code:
package agui;
import javax.swing.*;
public class Agui extends JFrame {
public Agui() {
setTitle("My Gui");
setSize(400, 400);
JButton button = new JButton("click me");
JPanel panel = new JPanel();
panel.add(button);
this.getContentPane().add(panel);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Agui a = new Agui();
}
}
You want the "addActionListener" method, something like:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("You clicked the button");
}
});

check event of panel in the main java class

I want to check the event of panel class which is being added on the JFrame class. In this sample program there is a button on a panel.
I want to monitor the click event of the button from the source frame.
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
class test extends JFrame implements ActionListener {
test() {
Container cp = this.getContentPane();
JButton b1 = new JButton("add");
cp.add(b1);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand().equals("add")) {
panel1 frm = new panel1();
cp.add(frm);
}
}
public static void main(String args[]) {
test t1 = new test();
t1.show(true);
}
}
class panel1 extends JPanel {
panel1() {
JButton b1 = new JButton("ok");
add(b1);
}
}
You need to make the JButton available to the "out side" world some how.
I, personally, would be reluctant to make the button itself available, instead, I would allow the outside world the ability to to attach a ActionListener to it...
public class Test extends JFrame implements ActionListener {
public Test() {
Container cp = this.getContentPane();
JButton b1 = new JButton("add");
cp.add(b1);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand().equals("add")) {
TestPane frm = new TestPane();
frm.addActionListener(...); // Add your new action listener here
cp.add(frm);
}
}
public static void main(String args[]) {
test t1 = new test();
t1.show(true);
}
}
public class TestPane extends JPanel {
private JButton b1;
public TestPane() {
b1 = new JButton("ok");
add(b1);
}
public void addActionListener(ActionListener listener) {
b1.addActionListener(listener);
}
public void removeActionListener(ActionListener listener) {
b1.removeActionListener(listener);
}
}
whatever you put in frame it just put into the center of the frame. So use BorderLayout for this to be visible as below
public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand().equals("add")) {
System.out.println("in actionPerformed");
panel1 frm = new panel1();
// this.removeAll();
add(frm,BorderLayout.NORTH);
this.validate();
}
}

how to modify current JPanel and JFrame in java

I am using the following code:
class ButtonPanel extends JPanel implements ActionListener
{
public ButtonPanel()
{
yellowButton=new JButton("Yellow");
blueButton=new JButton("Blue");
redButton=new JButton("Red");
add(yellowButton);
add(blueButton);
add(redButton);
yellowButton.addActionListener(this);
blueButton.addActionListener(this);
redButton.addActionListener(this);
}
public void actionPerformed(ActionEvent evt)
{
Object source=evt.getSource();
Color color=getBackground();
if(source==yellowButton) color=Color.yellow;
else if (source==blueButton) color=Color.blue;
else if(source==redButton) color=Color.red;
setBackground(color);
repaint();
}
private JButton yellowButton;
private JButton blueButton;
private JButton redButton;
}
class ButtonFrame extends JFrame
{
public ButtonFrame()
{
setTitle("ButtonTest");
setSize(400,400);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
Container contentPane=getContentPane();
contentPane.add(new ButtonPanel());
}
}
public class ButtonTest
{
public static void main(String args[])
{
JFrame frame=new ButtonFrame();
frame.show();
}
}
In actionperformed() I want to modify my panel and add some more component, is there any way to do this?
Yes.
Just call add() for the panel and then revalidate() and repaint();
but in actionperformed() i want to modify my panel and want to add some more component... Is there any way to do this....
Yes, you can add the components to the JPanel or "this" via the add(...) method, and you'll need to call revalidate() and then sometimes repaint() (especially if you also remove components) on the JPanel (this). But before you do this, if you haven't already done so, I think that you'll want to read the tutorials on using the layout managers so you can add components that are well situated.
hmmm I hard to comment anything, there are lots of mistakes, please start with this code
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class ButtonPanel extends JPanel implements ActionListener {
private static final long serialVersionUID = 1L;
private JButton yellowButton;
private JButton blueButton;
private JButton redButton;
public ButtonPanel() {
yellowButton = new JButton("Yellow");
yellowButton.addActionListener(this);
blueButton = new JButton("Blue");
blueButton.addActionListener(this);
redButton = new JButton("Red");
redButton.addActionListener(this);
add(yellowButton);
add(blueButton);
add(redButton);
setPreferredSize(new Dimension(400, 400));
}
#Override
public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
Color color = getBackground();
if (source == yellowButton) {
color = Color.yellow;
} else if (source == blueButton) {
color = Color.blue;
} else if (source == redButton) {
color = Color.red;
}
setBackground(color);
revalidate();
repaint();
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("ButtonTest");
frame.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.add(new ButtonPanel());
frame.pack();
frame.setVisible(true);
}
});
}
}

Categories

Resources