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

    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?

    Thanks.
    Eric

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

    Default

    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.

    Greetings
    Albert:rolleyes:

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

    Default

    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 requirements...it might be better to create a class for each frame though...as 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 
            this.initComponents();
            
            // place the first frame in the center of the screen
            this.first.setLocation(
                ( 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" );
            this.openFirst.addActionListener(
                new ActionListener() {
                    public void actionPerformed( ActionEvent event ){
                        openFirst_clicked( event );
            }});
            pane.add( this.openFirst );        
            
            this.openSecond = new JButton( "Open Second Dialog" );
            this.openSecond.addActionListener(
                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 ) {
            SwingUtilities.invokeLater(
                new Runnable() {
                    public void run() {
                        new MyApp();
            }});
        } //-- ends class method main
        
    } //-- ends class definition
    Greetings
    Daniel:o

Posting Permissions

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