0

http://pastebin.com/HNtg0Emv

I need some assistance using Eclipse on implementing a Jbutton task/code that opens up another applet when I click it, like how there are game launchers have the "Play Game" option to launch a game. Is this possible to do so?

My source code:

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;


public class NiceGui {

        private JFrame frmSomegame;

        /**
         * Launch the application.
         */
        public static void main(String[] args) {
                EventQueue.invokeLater(new Runnable() {
                        public void run() {
                                try {
                                        NiceGui window = new NiceGui();
                                        window.frmSomegame.setVisible(true);
                                } catch (Exception e) {
                                        e.printStackTrace();
                                }
                        }
                });
        }

        /**
         * Create the application.
         */
        public NiceGui() {
                initialize();
        }

        /**
         * Initialize the contents of the frame.
         */
        private void initialize() {
                frmSomegame = new JFrame();
                frmSomegame.setTitle("SomeGame");
                frmSomegame.getContentPane().setBackground(Color.BLUE);
                frmSomegame.setBounds(100, 100, 450, 300);
                frmSomegame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frmSomegame.getContentPane().setLayout(null);

                JButton btnNewButton = new JButton("Play");
                btnNewButton.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent arg0) {
                                JOptionPane.showMessageDialog(null,"Hello!");
                        }
                });
                btnNewButton.setBounds(28, 41, 133, 50);
                frmSomegame.getContentPane().add(btnNewButton);

                JButton btnNewButton_1 = new JButton("Credit");
                btnNewButton_1.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent arg0) {
                                JOptionPane.showMessageDialog(null,"Created by Calvin");
                        }
                });
                btnNewButton_1.setBounds(25, 142, 136, 50);
                frmSomegame.getContentPane().add(btnNewButton_1);

                JButton btnNewButton_2 = new JButton("Quit");
                btnNewButton_2.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent arg0) {
                                int result = JOptionPane.showConfirmDialog(null,"Do you want to quit?","Are you sure?",JOptionPane.YES_NO_OPTION) ;
                                if(result == JOptionPane.YES_OPTION) {
                                        System.exit(0) ;
                                }
                        }
                });
                btnNewButton_2.setBounds(249, 142, 156, 50);
                frmSomegame.getContentPane().add(btnNewButton_2);
        }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Calvin
  • 1
  • 1
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). .. – Andrew Thompson Nov 24 '14 at 08:22
  • .. 3) *"Is this possible to do so?"* Yes it is. Where is the `ActionListener`? – Andrew Thompson Nov 24 '14 at 08:23

0 Answers0