Need help in my code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • crystal2005
    New Member
    • Apr 2007
    • 44

    #1

    Need help in my code

    Hi there, I need some help to correct my code. I would like to create a menu bar with the implementation of OO. So, i think i can edit and change it easyly if i use some classes.

    [HTML]package gui_layout_BM;

    import javax.swing.JFr ame;

    public class layoutDriver {

    public static void main(String[] args) {
    JFrame frame = new JFrame("Bovinet ine Maker Simulator");
    frame.setDefaul tCloseOperation (JFrame.EXIT_ON _CLOSE);
    frame.setJMenuB ar(new MenuBarPanel(). MainMenuBar()); //This line doesn't seem to call the function
    frame.setVisibl e(true);
    frame.setSize(4 80,320);

    }

    }[/HTML]

    And another function
    [HTML]
    package gui_layout_BM;

    import javax.swing.JMe nu;
    import javax.swing.JMe nuBar;
    import javax.swing.JMe nuItem;
    import javax.swing.JSe parator;

    class MenuBarPanel extends JMenuBar{

    public JMenuBar MainMenuBar() {
    JMenuBar menu = new JMenuBar();

    menu.add(Create FileMenu());
    menu.add(new JSeparator());
    menu.add(Create ActionMenu());

    return menu;
    }

    public JMenu CreateFileMenu( ) {
    JMenu filemenu = new JMenu("File");

    JMenuItem fileItem1 = new JMenuItem("Rese t");
    JMenuItem fileItem2 = new JMenuItem("Exit ");

    filemenu.add(fi leItem1);
    filemenu.add(fi leItem2);

    return filemenu;

    }

    public JMenu CreateActionMen u() {
    JMenu actionmenu = new JMenu("Action") ;
    //menu.setMnemoni c("A");

    JMenuItem actionItem1 = new JMenuItem("Add Pot");
    JMenuItem actionItem2 = new JMenuItem("Add Ground");
    JMenuItem actionItem3 = new JMenuItem("Add Water");
    actionItem3.add (new JSeparator());
    JMenuItem actionItem4 = new JMenuItem("Remo ve Pot");
    JMenuItem actionItem5 = new JMenuItem("Remo ve Ground");
    JMenuItem actionItem6 = new JMenuItem("Remo ve Water");
    actionItem6.add (new JSeparator());
    JMenuItem actionItem7 = new JMenuItem("Brew ");

    actionmenu.add( actionItem1);
    actionmenu.add( actionItem2);
    actionmenu.add( actionItem3);
    actionmenu.add( actionItem4);
    actionmenu.add( actionItem5);
    actionmenu.add( actionItem6);
    actionmenu.add( actionItem7);

    return actionmenu;

    }

    }
    [/HTML]

    I hope there is no fatal error in my code.

    Thank you for any kind of help.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Look at your menu bar class:

    [code=java]
    class MenuBarPanel extends JMenuBar{

    public JMenuBar MainMenuBar() {
    JMenuBar menu = new JMenuBar();

    menu.add(Create FileMenu());
    menu.add(new JSeparator());
    menu.add(Create ActionMenu());

    return menu;
    }
    ...
    [/code]

    Your class *is a* JMenuBar because you extend from it but it also *has a*
    JMenuBar because it makes one in the MainMenuBar method. You probably
    want to populate a MenuBarPanel from its constructor. btw, is that a correct
    name for your menu bar, i.e. is it really a panel? Also: only names of classes,
    interfaces and enums start with a capital letter by convention. All the others
    start with a lowercase letter and most of the time the name of a method has
    a verb in it. It really improves readability.

    kind regards,

    Jos

    Comment

    • crystal2005
      New Member
      • Apr 2007
      • 44

      #3
      Originally posted by JosAH
      Look at your menu bar class:

      [code=java]
      class MenuBarPanel extends JMenuBar{

      public JMenuBar MainMenuBar() {
      JMenuBar menu = new JMenuBar();

      menu.add(Create FileMenu());
      menu.add(new JSeparator());
      menu.add(Create ActionMenu());

      return menu;
      }
      ...
      [/code]

      Your class *is a* JMenuBar because you extend from it but it also *has a*
      JMenuBar because it makes one in the MainMenuBar method. You probably
      want to populate a MenuBarPanel from its constructor. btw, is that a correct
      name for your menu bar, i.e. is it really a panel? Also: only names of classes,
      interfaces and enums start with a capital letter by convention. All the others
      start with a lowercase letter and most of the time the name of a method has
      a verb in it. It really improves readability.

      kind regards,

      Jos

      You're right, I want to populate it to make it easier to be called from main class. MenuBarPanel is just a name of class. Thanks to remind me about the naming, i almost forget about it.

      I'll figure out again, if i get something, i'll keep posting in this thread. Thanks

      Comment

      • crystal2005
        New Member
        • Apr 2007
        • 44

        #4
        Hi,

        I have changed my code almost completely recently, And now I have problem in Layout Interface. It looks like a nice interface, but it doesn't. When i resize the frame, some buttons aren't fixed. Could someone tell me what is the most appropriate layout for my interface??

        This layout has no action listener yet.

        Thank you.



        [code=java]
        import java.awt.*;
        import java.awt.event. *;
        import javax.swing.*;
        import javax.swing.bor der.*;

        public class MenuLook {

        public JMenuBar createMenuBar() {
        JMenuBar menuBar = new JMenuBar(); //Create the menu bar.

        menuBar.add(cre ateFileMenu());
        menuBar.add(cre ateActionMenu() );
        menuBar.add(cre ateHelpMenu());

        return menuBar;
        }

        public JMenu createFileMenu( ) {
        JMenu file = new JMenu("File");
        file.setMnemoni c(KeyEvent.VK_F );

        //Group of 'File' JMenuItems
        JMenuItem fileItem1 = new JMenuItem("Rese t", KeyEvent.VK_R);
        JMenuItem fileItem2 = new JMenuItem("Exit ", KeyEvent.VK_R);

        file.add(fileIt em1);
        file.addSeparat or();
        file.add(fileIt em2);

        return file;
        }

        public JMenu createActionMen u() {
        JMenu action = new JMenu("Action") ;
        action.setMnemo nic(KeyEvent.VK _A);

        //Group of 'Action' JMenuItems
        JMenuItem actionItem1 = new JMenuItem("Add Pot");
        JMenuItem actionItem2 = new JMenuItem("Add Ground");
        JMenuItem actionItem3 = new JMenuItem("Add Water");
        JMenuItem actionItem4 = new JMenuItem("Remo ve Pot");
        JMenuItem actionItem5 = new JMenuItem("Remo ve Ground");
        JMenuItem actionItem6 = new JMenuItem("Remo ve Water");
        JMenuItem actionItem7 = new JMenuItem("Brew ");

        action.add(acti onItem1);
        action.add(acti onItem2);
        action.add(acti onItem3);
        action.addSepar ator();
        action.add(acti onItem4);
        action.add(acti onItem5);
        action.add(acti onItem6);
        action.addSepar ator();
        action.add(acti onItem7);

        return action;
        }

        public JMenu createHelpMenu( ) {
        JMenu help = new JMenu("Help");
        help.setMnemoni c(KeyEvent.VK_H );

        //Group of 'help' JMenuItems
        JMenuItem helpItem1 = new JMenuItem("Abou t BM Simulator");

        help.add(helpIt em1);

        return help;
        }

        public Container createContentPa ne() {
        //Create the content-pane-to-be.
        JPanel contentPane = new JPanel(new BorderLayout()) ;
        //contentPane.set Opaque(true);

        //Create a scrolled text area.
        JTextArea output = new JTextArea(5, 30);
        output.setEdita ble(true);
        JScrollPane scrollPane = new JScrollPane(out put);

        //Add the text area to the content pane.
        contentPane.add (scrollPane, BorderLayout.CE NTER);

        contentPane.add (createWestPane l(), BorderLayout.WE ST);
        contentPane.add (createSouthPan el(), BorderLayout.SO UTH);

        return contentPane;
        }

        public JPanel createWestPanel () {
        JPanel westPanel = new JPanel(new BorderLayout()) ; //Probably not appropriate Layout

        //Panels inside 'westPanel'
        JPanel addActionPanel = new JPanel(new GridLayout(3,1, 10,10));
        JPanel removeActionPan el = new JPanel(new GridLayout(3,1, 10,10));
        JPanel brewActionPanel = new JPanel(new GridLayout(1,1, 10,10));

        TitledBorder addActionBorder = new TitledBorder("A dd Action");
        TitledBorder removeActionBor der = new TitledBorder("R emove Action");
        TitledBorder brewActionBorde r = new TitledBorder("B rew Action");

        JButton b1 = new JButton("Add Pot");
        JButton b2 = new JButton("Add Ground");
        JButton b3 = new JButton("Add Water");
        JButton b4 = new JButton("Remove Pot");
        JButton b5 = new JButton("Remove Ground");
        JButton b6 = new JButton("Remove Water");
        JButton b7 = new JButton("Brew") ;

        addActionPanel. add(b1);
        addActionPanel. add(b2);
        addActionPanel. add(b3);
        addActionPanel. setBorder(addAc tionBorder);
        removeActionPan el.add(b4);
        removeActionPan el.add(b5);
        removeActionPan el.add(b6);
        removeActionPan el.setBorder(re moveActionBorde r);
        brewActionPanel .add(b7);
        brewActionPanel .setBorder(brew ActionBorder);

        //Assign Panels to 'westPanel'
        westPanel.add(a ddActionPanel, BorderLayout.PA GE_START);
        westPanel.add(r emoveActionPane l, BorderLayout.CE NTER); //Problem With this line
        westPanel.add(b rewActionPanel, BorderLayout.PA GE_END);

        return westPanel;
        }

        public JPanel createSouthPane l() {
        JPanel southPanel = new JPanel();

        JLabel statusBar = new JLabel("This test");
        statusBar.setTe xt("Testing");

        southPanel.add( statusBar);

        return southPanel;

        }

        private static void createAndShowGU I() {
        //Create and set up the window.
        JFrame frame = new JFrame("Bovinet ine Maker Simulator");
        frame.setDefaul tCloseOperation (JFrame.EXIT_ON _CLOSE);

        //Create and set up the content pane.
        MenuLook demo = new MenuLook();
        frame.setJMenuB ar(demo.createM enuBar());
        frame.setConten tPane(demo.crea teContentPane() );

        //Display the window.
        frame.setSize(6 40,380);
        frame.setVisibl e(true);
        }

        public static void main(String[] args) {
        createAndShowGU I();
        }

        }

        [/code]

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by crystal2005
          Hi,

          I have changed my code almost completely recently, And now I have problem in Layout Interface. It looks like a nice interface, but it doesn't. When i resize the frame, some buttons aren't fixed. Could someone tell me what is the most appropriate layout for my interface??
          Can you tell in plain English what you want it to look and what it doesn't do now?
          I browsed a bit through all that code and it looks sensible overall.

          kind regards,

          Jos

          Comment

          • crystal2005
            New Member
            • Apr 2007
            • 44

            #6
            This is what basically my interface looks like



            But when i try to resize it, the center panel of my west-panel seems to resize itself and become like this



            What i expect is, when i resize the window i want all the button to be fixed. So that it would be a nicer look.

            Thanks for any advice.

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              For that left/west button panel you can use a SpringLayout; the API documentation
              of that class points to a nice tutorial with examples.

              kind regards,

              Jos

              Comment

              Working...