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
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:
- How do I make a window in Java?Best solution by Stack Overflow
- How can I make a Spinner only by java?Best solution by stackoverflow.com
- How do I make a python web program that is on a ubuntu server allow access to the server?Best solution by Yahoo! Answers
- How do i make a my yahoo but not a home page?Best solution by Yahoo! Answers
- How do you make a simple origami person?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.