Results 1 to 3 of 3
  1. #1
    Eric is offline Senior Member
    Join Date
    Jun 2007
    Rep Power

    Default Help with GUI Java

    Hi, I am trying to make a simple java program which acts as a shop register.
    The user is able to click on a cd and select to buy it.
    I am having major problems learning java and cannot find anyway for forms to navigate from one to another. Is it even possible?
    Are there any programs which work as a GUI creation as a tool which may help me?


  2. #2
    Albert is offline Senior Member
    Join Date
    Jun 2007
    Rep Power


    Have you written any code?

    Also, is this a Java application or JSP (Java Server Pages)?
    You said forms, so I just want to clarify.

    I think Netbeans (the IDE) has a feature where you can do a sort of "GUI Creation" but I'm unsure, as I'm typically an Eclipse only user.


  3. #3
    Daniel is offline Member
    Join Date
    Jun 2007
    Rep Power


    Hello, I have created a quick example that demonstrates how you could create more than one JFrame in the same class.

    Depending on your particular might be better to create a class for each frame that would make it easier when performing maintenance etc.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class MyApp {
        private JFrame first,         // first Jframe instance
                       second,        // second Jframe instance 
                       third;        // third Jframe instance
        private JButton openFirst,    // opens second jframe
                        openSecond;    // open third jframe 
        private final int WIDTH = 400;    // first frame's width
        private final int HEIGHT = 200;    // second frame's height
        public MyApp() {
            // create and initilize the first frame...first being the main one
            this.first = new JFrame( "First Frame" );
            this.first.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            // initialize components 
            // place the first frame in the center of the screen
                ( Toolkit.getDefaultToolkit().getScreenSize().width - WIDTH ) / 2, 
                ( Toolkit.getDefaultToolkit().getScreenSize().height - HEIGHT ) / 2
            // set frame size
            this.first.setSize( WIDTH, HEIGHT );
            this.first.setVisible( true ); // make it visible
        } //-- ends class constructor
        private void initComponents() {
            // this method takes care of initializing and
            // adding comonents to the first frame
            Container pane = this.first.getContentPane();
            FlowLayout layout = new FlowLayout();
            pane.setLayout( layout );
            // initialize the first button to open the first frame
            this.openFirst = new JButton( "Open First Dialog" );
                new ActionListener() {
                    public void actionPerformed( ActionEvent event ){
                        openFirst_clicked( event );
            pane.add( this.openFirst );        
            this.openSecond = new JButton( "Open Second Dialog" );
                new ActionListener() {
                    public void actionPerformed( ActionEvent event ) {
                        openSecond_clicked( event );
            pane.add( this.openSecond );    
        } //-- ends instance method initComponents
        private void openFirst_clicked( ActionEvent event ) {
            this.second = new JFrame( "Second Frame" );
            this.second.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
            this.second.getContentPane().setLayout( new FlowLayout() );
            this.second.getContentPane().add( new JLabel("Hello from second frame" ) );
            this.second.setSize( 250, 100 );
            this.second.setVisible( true );
        } //-- ends instance method openFirst_clicked
        private void openSecond_clicked( ActionEvent event ) {
            this.third = new JFrame( "Third Frame" );
            this.third.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
            this.third.getContentPane().setLayout( new FlowLayout() );
            this.third.getContentPane().add( new JLabel("Hello from third frame" ) );
            this.third.setSize( 250, 100 );
            this.third.setVisible( true );
        } //-- ends instance method openFirst_clicked
        public static void main( String [] args ) {
                new Runnable() {
                    public void run() {
                        new MyApp();
        } //-- ends class method main
    } //-- ends class definition

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts