0

Why doesn't it work? I have four classes, the main class, a "MyFrame" class, a "MyButton" class and a MySecondFrame class. If the user clicks the Button, the first frame should disappear and the second Frame should appear " (its a cannot resolve symbol error in the mehod on.Click). Also theres the error "'com.company.Main.this' cannot be referenced from a static context (in the second line of the main method)". How can I solve this problem? Heres my code:

package com.company;

interface ButtonListener{
void onClick();
}

public class Main implements ButtonListener {

public static void main(String[] args) {


    MyFrame firstFrame = new MyFrame();
    MyButton firstButton = new MyButton(this);
    //MyTextField firstTextField = new MyTextField();
    MySecondFrame secondFrame = new MySecondFrame();
    MyLabel firstLabel = new MyLabel();
    firstFrame.add(firstButton);
    //secondFrame.add(firstTextField);
    //secondFrame.add(firstLabel);
}

@Override
void onClick() {
    firstFrame.setVisible(false);
    secondFrame.setVisible(true);
}

}

package com.company;
import javax.swing.*;
import java.awt.*;

public class MyFrame extends JFrame{

MyFrame(){
    this.getContentPane().setBackground(new Color(0xD3D3D3));
    this.setSize(500, 700);
    this.setLocation(870,15);
    this.setResizable(false);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
    this.setTitle("Converter");
    this.setLayout(null);
}

}

package com.company;

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

public class MyButton extends JButton implements ActionListener{

private ButtonListener buttonListener;



MyButton(ButtonListener buttonListener){
    this.buttonListener = buttonListener;
    this.setBounds(150, 100, 200, 50);
    this.setText("Convert Currencies");
    this.setFocusable(false);
    this.setVisible(true);
    }
    @Override
    public void actionPerformed(ActionEvent e){
    if(e.getSource()==this){
        buttonListener.onClick();
    }
    }

}

package com.company;

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

public class MySecondFrame extends JFrame {
MySecondFrame(){
    this.getContentPane().setBackground(new Color(0xD3D3D3));
    this.setSize(500, 700);
    this.setLocation(870,15);
    this.setResizable(false);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
    this.setTitle("Converter");
    this.setLayout(null);
}

}

jakob
  • 33
  • 6
  • `main` is `static`, so you don't have access to `this` – QBrute Sep 04 '21 at 14:50
  • 1
    `this` refers to "the instance of the class containing the method which is executing" - but only for *instance* methods. For static methods (like your `main` method) there *is* no instance. I suspect you want to create an instance of `Main` and pass that in... – Jon Skeet Sep 04 '21 at 14:50
  • How can I get aceess to "this"? – jakob Sep 04 '21 at 14:54
  • You don't. There isn't a `this`. That's the reality of the situation. You're not in a class. That's what `static` means. If you want a `this`, you need to be in a non-static method. – Silvio Mayolo Sep 04 '21 at 15:20
  • @jakob you can't access `this` in the `main()` method. If you still need it then create an instance of `Main` class and assign it to the variable. Then you can us this variable instead of `this`. – Roman C Sep 04 '21 at 15:31

1 Answers1

0

Solution 1: declare an instance method instanceMain in the Main class, and change your main method to create a Main object and call its instanceMain:

public void instanceMain() {
    // Move your code from your existing main() method to this method
}
public static void main(String[] args) {
    // Move your existing code out of main() and replace it with this:
    Main inst = new Main();
    inst.instanceMain();
}

Solution 2: In your main method, change

MyButton firstButton = new MyButton(this);

to

MyButton firstButton = new MyButton(new Main());
jason44107
  • 399
  • 2
  • 13