How to open new JFrame by clicking on button in other JFrame?

How do I make a simple Yes No Button Panel in JAVA?

  • This is the code I have. I simply want the JFrame to come up saying "Play Again?" and peeps press yes or no This is for an assignment due soon so the more help the better, cheers import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Buttonpanel extends JPanel { private JButton yes; private JButton no; public Buttonpanel() { JFrame frame = new JFrame ("Play Again?"); yes = new JButton ("Yes"); no = new JButton ("No"); add (yes); add (no); setPreferredSize (new Dimension(300, 40)); } }

  • Answer:

    Better to extend JFrame. I added a main method to make it pop up. When you run this class as a main class it seems to do what you are looking for: import javax.swing.*; import java.awt.*; import java.awt.event.*; public class YesNoFrame extends JFrame { private JButton yes; private JButton no; public YesNoFrame() { super("Play again?"); JPanel yesNoPanel = new JPanel(); yesNoPanel.setLayout(new FlowLayout()); yes = new JButton("Yes"); no = new JButton("No"); yesNoPanel.add(yes); yesNoPanel.add(no); yesNoPanel.setPreferredSize(new Dimension(300, 40)); add(yesNoPanel); setDefaultCloseOperation(JFrame.EXIT… pack(); } public static void main(String[] args){ YesNoFrame frame = new YesNoFrame(); frame.setVisible(true); } }

Robbie A at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

your code is fine but lacks a main method: public static void main(String [] args){ JFrame frame = new JFrame("Play Again?"); frame.setContentPane(new Buttonpanel()); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } the JFrame should not be declared/initialised inside the constructor.

solidus

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.