0

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. 3) See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) .. – Andrew Thompson Jan 05 '18 at 23:45
  • .. 4) `public class StartingScreen extends JFrame{ JFrame StartingScreen = new JFrame(); ..` That code both extends frame & keeps a reference to a standard frame. Do the latter, not the former. 5) `StartingScreen.add(..,BorderLayout.WEST); .. StartingScreen.add(..,BorderLayout.PAGE_END);` This uses a combination of compass & logical constraints. It's better to consistently use the logical constraints (which adapt to the default direction of the text of a language, for e.g.) consistently, so `StartingScreen.add(..,BorderLayout.LINE_START); .. StartingScreen.add(..,BorderLayout.PAGE_END);` – Andrew Thompson Jan 05 '18 at 23:46
  • Thanks, but how do I set the size of the jpanel without preferredsize?, setsize doesn't work if I have multiple jpanels in one jframe –  Jan 06 '18 at 11:47
  • *"how do I set the size of the jpanel without preferredsize?"* Are they custom painted panels, or do they contain components? Did you read the top two voted answers in the linked Q&A? I think they answered at least part of your question. What don't you understand after reading those answers? BTW - where is the MCVE / SSCCE? Voting to close for lack of one. – Andrew Thompson Jan 06 '18 at 13:13

2 Answers2

0

You'd better do your showOptionDialog in constructor (or some other method for best practice) of class NewGame. And to open this JOptionPane, you have to :

ButtonNewGame.addActionListener((ActionEvent e) -> {
     new NewGame();
});
Ladence
  • 244
  • 2
  • 9
0

I don't think this code will compile because you have ButtonNewGame.addActionListener(this); but your class doesn't implement ActionListener.

To fix that, one of the things you'll need to do is write an actionPerformed(...) method. That will be called when the button is pressed, and I think that's where you want to make an instance of NewGame and at that point your chooser dialog will appear.

Jonathan
  • 349
  • 1
  • 9