Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Aug 2007
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Multiple window Java app

    Hi,

    Newbie (to Java) here. Looking to Java-tize an old board game I dug out. It basically will be a database driven game (i.e., all game results came off of charts).

    I would like to start with a Main Window with various buttons to open other windows. From there I would like to be accessing various databases etc.

    So...my first step was to find a way to create that Main Window and have it capable of using buttons to open other windows. Checking the internet, tutorials, etc., here is what I've come up with so far:


    Main class:
    Code:
    package b17;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import java.awt.Dimension;
    import java.awt.Toolkit;
    import java.awt.Color;
    
    
    public class Main extends JPanel implements ActionListener {
    
        JFrame parent;
        JButton missionPlanningButton, browseAircraftButton, browseTargetsButton,
                statisticsButton;
                   
       
        // This is the Main Application Window
        Main(JFrame parent) {
            this.parent = parent;
            this.parent.setBackground(Color.BLACK);
            this.parent.setSize(200,200);
            Dimension frameSize = this.parent.getSize();
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            this.parent.setLocation((screenSize.width - frameSize.width)/2,
            (screenSize.height - frameSize.height)/2);
            missionPlanningButton = new JButton("Plan Mission");
            browseAircraftButton = new JButton("Browse available aircraft");
            browseTargetsButton = new JButton("Browse Target List");
            statisticsButton = new JButton("View Squadron Statistics");
            missionPlanningButton.addActionListener(this);
            browseAircraftButton.addActionListener(this);
            browseTargetsButton.addActionListener(this);
            statisticsButton.addActionListener(this);
            add(missionPlanningButton);
            add(browseAircraftButton);
            add(browseTargetsButton);
            add(statisticsButton);
    
        }
    
        // Controller
        public void actionPerformed(ActionEvent e) {
            String command = e.getActionCommand();
            if (command.equals("Plan Mission")) {
                JOptionPane.showMessageDialog(parent,
                        new MissionPlanning(parent));
            } else if (command.equals("Browse available aircraft")) {
                JOptionPane.showMessageDialog(parent,
                        new Aircraft(parent));        }
              else if (command.equals("Browse Target List")) {
                JOptionPane.showMessageDialog(parent,
                        new Targets(parent));         }
              else if (command.equals("View Squadron Statistics")) {
                JOptionPane.showMessageDialog(parent,
                        new Statistics (parent));
            }
        }
    }
    Application class:
    Code:
    package b17;
    
    
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    
    
    public class Application {
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    JFrame f = new JFrame("B17: Queen of the Skies");                
                    f.add(new Main(f));
                    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    f.pack();
                    f.setVisible(true);
                }
            });
        }
    }
    Mission Planning class (only posting one...the rest are the same)
    Code:
    package b17;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    
    public class MissionPlanning extends JPanel {
        MissionPlanning(JFrame parent) {
            add(new JLabel("This window will open and provide buttons to step the " +
                    "user through total mission planning.  Aircraft, crew members, " +
                    "target and other assorted information will be presented"));
        }
    }

    Right now...the main problem I'm having is being unable to set the size, color, location, look/feel, etc. of the Main Window.

    Also...looking forward...guidance on moving forward to making the called windows able to view/update databases would be great.

    Thanks,

  • #2
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts

    Macintosh

    Right now...the main problem I'm having is being unable to set the size, color, location, look/feel, etc. of the Main Window.
    Well, you don't set those properties. You're busy setting the properties of the JFrame that is opened in your 'Application' class from within your 'Main' constructor. I'd recommend letting each class or each frame set its own properties.

    Also...looking forward...guidance on moving forward to making the called windows able to view/update databases would be great.
    Each 'window' or frame will have it's own buttons / triggers to perform different tasks. Simply use a listener to trigger that task.

    Otherwise, my personal preference is to not allow the frame to implement the ActionListener interface, but to use an inner (probably private) class that either extends a component that will use it (like JButton), or just plain implements the event listener itself for a specific task.

  • #3
    New Coder
    Join Date
    Aug 2007
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OK...thanks for the response. I have been able to get done what I was looking for.

    Of course...with further investigation...now I'm actually looking forward to trying something else. Subject for another thread...


  •  

    Posting Permissions

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