I would like to know of how do I make a link in a button of the main class inside of a JPanel
that close the JFrame
and opens a JOptionPane
from another class, any help will be welcome
This is my main class:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class StartingScreen extends JFrame{
JFrame StartingScreen = new JFrame();
JPanel SidePanel = new JPanel();
JPanel CenterPanel = new JPanel();
JPanel BottomPanel = new JPanel();
JButton ButtonNewGame = new JButton("New Game");
JButton ButtonLoadGame = new JButton("Load Game");
JButton ButtonDeleteGame = new JButton("Delete Game");
public static void main(String[] args) {
new StartingScreen();
FileLoader FL = new FileLoader();
}
StartingScreen(){
//Configuration of the JPanel
super("StartingScreen");
StartingScreen.setSize(880,520);
StartingScreen.setResizable(false);
StartingScreen.setDefaultCloseOperation(EXIT_ON_CLOSE);
//Configuration of the panels
SidePanel.setBackground(Color.YELLOW);
SidePanel.setPreferredSize(new Dimension(300,520));
BottomPanel.setPreferredSize(new Dimension(880,80));
BottomPanel.setBackground(Color.RED);
CenterPanel.setBackground(Color.BLUE);
//Configuration of the buttons
ButtonNewGame.setBackground(Color.GREEN);
ButtonNewGame.setPreferredSize(new Dimension(280,130));
ButtonNewGame.setActionCommand("Add Credits");
ButtonNewGame.addActionListener(this);
ButtonLoadGame.setBackground(Color.GREEN);
ButtonLoadGame.setPreferredSize(new Dimension(280,130));
ButtonDeleteGame.setBackground(Color.GREEN);
ButtonDeleteGame.setPreferredSize(new Dimension(280,130));
//Adding components
StartingScreen.add(SidePanel,BorderLayout.WEST);
StartingScreen.add(CenterPanel,BorderLayout.CENTER);
StartingScreen.add(BottomPanel,BorderLayout.PAGE_END);
SidePanel.add(ButtonNewGame);
SidePanel.add(ButtonLoadGame);
SidePanel.add(ButtonDeleteGame);
//Making it visible(Important to be at the end)
StartingScreen.setVisible(true);
}
}
This is my secondary class where I store the option pane I want to open:
import javax.swing.JOptionPane;
public class NewGame {
String[] classes ={
"Paladin",
"Mage",
"Warlock",
};
int classes_index=JOptionPane.showOptionDialog(null, "Choose a class", "Classes", 0,
JOptionPane.INFORMATION_MESSAGE, null, classes, 0);
}
The button I want to link is ButtomNewGme
in the panel SidePanel
.