I am having trouble calling a java class from another class when a button is pressed.
When I press Play the other frame comes up with the screen is white and I can't even exit it. other than shutting the whole eclipse program off. The new frame that pop's up is just a white screen nothing else from Main Class is coming up like the character.
This is my Menu class code which contains the button Play which the user will press.
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Menu implements ActionListener {
JButton Play, Scoreboard;
public static void main(String[] args) throws InterruptedException
{
Menu myWindow = new Menu();
}
public Menu() {
JFrame frame = new JFrame("Fruit Catcher");
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
// Filler panel to fill in the empty space to get button panel centered.
JPanel filler = new JPanel();
filler.setPreferredSize(new Dimension(180,180));
ImageIcon junglebackground = new ImageIcon("junglebackground.jpg");
JLabel backgroundimage = new JLabel(junglebackground);
frame.add(backgroundimage);
frame.setSize(700,470);
frame.setResizable(false);
frame.setVisible(true);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout((LayoutManager) new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
Play = new JButton("Play");
Scoreboard = new JButton("Scoreboard");
Play.setAlignmentX(Component.CENTER_ALIGNMENT);
Scoreboard.setAlignmentX(Component.CENTER_ALIGNMENT);
JLabel gap = new JLabel("\n");
Play.addActionListener(this);
Scoreboard.addActionListener(this);
buttonPanel.add(Play);
buttonPanel.add(gap);
buttonPanel.add(Scoreboard);
panel.add(buttonPanel, BorderLayout.CENTER);
panel.add(filler, BorderLayout.NORTH);
frame.add(panel);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(Play))
{
String[] args = {};
try {
Main.main(args);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
if(e.getSource().equals(Scoreboard))
{
System.out.println("test");
}
}
}
This is Main java class which is the Level . it contains character and fruit. the character can move.
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.*;
import javax.swing.*;
public class Main extends JPanel
{
Character player = new Character(this);
movement fruit = new movement(this);
public Main()
{
addKeyListener(new KeyListener() //KeyListener for the main
{
public void keyTyped(KeyEvent e)
{
}
public void keyReleased(KeyEvent e)
{
player.keyReleased(e);
}
public void keyPressed(KeyEvent e)
{
player.keyPressed(e);
}
});
setFocusable(true); //Allows the focus to be on the main and enables movement
}
private void move()
{
player.move();
}
public void paint(Graphics g)
{
super.paint(g);
Graphics2D Player = (Graphics2D) g;
player.paint(Player);
Graphics2D Fruit = (Graphics2D) g;
fruit.paintComponent(Fruit);
}
public static void main(String[] args) throws InterruptedException
{
JFrame frame = new JFrame("Fruit Catcher");
Main game = new Main();
frame.add(game);
frame.setSize(700, 450);
frame.setVisible(true);
while (true)
{
game.move();
game.repaint();
Thread.sleep(5); //Control speed of player
}
}
}