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);
}
}