473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Need help in my code

crystal2005
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.JFrame;

public class layoutDriver {

public static void main(String[] args) {
JFrame frame = new JFrame("Bovinetine Maker Simulator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setJMenuBar(new MenuBarPanel().MainMenuBar()); //This line doesn't seem to call the function
frame.setVisible(true);
frame.setSize(480,320);

}

}[/HTML]

And another function
[HTML]
package gui_layout_BM;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;

class MenuBarPanel extends JMenuBar{

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

menu.add(CreateFileMenu());
menu.add(new JSeparator());
menu.add(CreateActionMenu());

return menu;
}

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

JMenuItem fileItem1 = new JMenuItem("Reset");
JMenuItem fileItem2 = new JMenuItem("Exit");

filemenu.add(fileItem1);
filemenu.add(fileItem2);

return filemenu;

}

public JMenu CreateActionMenu() {
JMenu actionmenu = new JMenu("Action");
//menu.setMnemonic("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("Remove Pot");
JMenuItem actionItem5 = new JMenuItem("Remove Ground");
JMenuItem actionItem6 = new JMenuItem("Remove 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.
May 12 '08 #1
6 1647
JosAH
11,448 Expert 8TB
Look at your menu bar class:

Expand|Select|Wrap|Line Numbers
  1. class MenuBarPanel extends JMenuBar{ 
  2.  
  3.     public JMenuBar MainMenuBar() {
  4.         JMenuBar menu = new JMenuBar();
  5.  
  6.         menu.add(CreateFileMenu());
  7.         menu.add(new JSeparator());
  8.         menu.add(CreateActionMenu());
  9.  
  10.         return menu;
  11.     }
  12.     ...
  13.  
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
May 12 '08 #2
Look at your menu bar class:

Expand|Select|Wrap|Line Numbers
  1. class MenuBarPanel extends JMenuBar{ 
  2.  
  3.     public JMenuBar MainMenuBar() {
  4.         JMenuBar menu = new JMenuBar();
  5.  
  6.         menu.add(CreateFileMenu());
  7.         menu.add(new JSeparator());
  8.         menu.add(CreateActionMenu());
  9.  
  10.         return menu;
  11.     }
  12.     ...
  13.  
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
May 13 '08 #3
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.



Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import javax.swing.border.*;
  5.  
  6. public class MenuLook {
  7.  
  8.     public JMenuBar createMenuBar() {
  9.         JMenuBar menuBar = new JMenuBar(); //Create the menu bar.
  10.  
  11.         menuBar.add(createFileMenu());
  12.         menuBar.add(createActionMenu());
  13.         menuBar.add(createHelpMenu());
  14.  
  15.         return menuBar;
  16.     }
  17.  
  18.     public JMenu createFileMenu() {
  19.         JMenu file = new JMenu("File");
  20.         file.setMnemonic(KeyEvent.VK_F);
  21.  
  22.         //Group of 'File' JMenuItems
  23.         JMenuItem fileItem1 = new JMenuItem("Reset", KeyEvent.VK_R);
  24.         JMenuItem fileItem2 = new JMenuItem("Exit", KeyEvent.VK_R);
  25.  
  26.         file.add(fileItem1);
  27.         file.addSeparator();
  28.         file.add(fileItem2);
  29.  
  30.         return file;        
  31.     }
  32.  
  33.     public JMenu createActionMenu() {
  34.         JMenu action = new JMenu("Action");
  35.         action.setMnemonic(KeyEvent.VK_A);
  36.  
  37.         //Group of 'Action' JMenuItems
  38.         JMenuItem actionItem1 = new JMenuItem("Add Pot");
  39.         JMenuItem actionItem2 = new JMenuItem("Add Ground");
  40.         JMenuItem actionItem3 = new JMenuItem("Add Water");
  41.         JMenuItem actionItem4 = new JMenuItem("Remove Pot");
  42.         JMenuItem actionItem5 = new JMenuItem("Remove Ground");
  43.         JMenuItem actionItem6 = new JMenuItem("Remove Water");
  44.         JMenuItem actionItem7 = new JMenuItem("Brew");
  45.  
  46.         action.add(actionItem1);
  47.         action.add(actionItem2);
  48.         action.add(actionItem3);
  49.         action.addSeparator();
  50.         action.add(actionItem4);
  51.         action.add(actionItem5);
  52.         action.add(actionItem6);
  53.         action.addSeparator();
  54.         action.add(actionItem7);
  55.  
  56.         return action;        
  57.     }
  58.  
  59.     public JMenu createHelpMenu() {
  60.         JMenu help = new JMenu("Help");
  61.         help.setMnemonic(KeyEvent.VK_H);
  62.  
  63.         //Group of 'help' JMenuItems
  64.         JMenuItem helpItem1 = new JMenuItem("About BM Simulator");
  65.  
  66.         help.add(helpItem1);
  67.  
  68.         return help;        
  69.     }
  70.  
  71.     public Container createContentPane() {
  72.         //Create the content-pane-to-be.
  73.         JPanel contentPane = new JPanel(new BorderLayout());
  74.         //contentPane.setOpaque(true);
  75.  
  76.         //Create a scrolled text area.
  77.         JTextArea output = new JTextArea(5, 30);
  78.         output.setEditable(true);
  79.         JScrollPane scrollPane = new JScrollPane(output);
  80.  
  81.         //Add the text area to the content pane.
  82.         contentPane.add(scrollPane, BorderLayout.CENTER);     
  83.  
  84.         contentPane.add(createWestPanel(), BorderLayout.WEST);        
  85.         contentPane.add(createSouthPanel(), BorderLayout.SOUTH);
  86.  
  87.         return contentPane;
  88.     }
  89.  
  90.     public JPanel createWestPanel() {
  91.         JPanel westPanel = new JPanel(new BorderLayout()); //Probably not appropriate Layout
  92.  
  93.         //Panels inside 'westPanel'
  94.         JPanel addActionPanel = new JPanel(new GridLayout(3,1,10,10));
  95.         JPanel removeActionPanel = new JPanel(new GridLayout(3,1,10,10));
  96.         JPanel brewActionPanel = new JPanel(new GridLayout(1,1,10,10));
  97.  
  98.         TitledBorder addActionBorder = new TitledBorder("Add Action");
  99.         TitledBorder removeActionBorder = new TitledBorder("Remove Action");
  100.         TitledBorder brewActionBorder = new TitledBorder("Brew Action");
  101.  
  102.         JButton b1 = new JButton("Add Pot");
  103.         JButton b2 = new JButton("Add Ground");
  104.         JButton b3 = new JButton("Add Water");
  105.         JButton b4 = new JButton("Remove Pot");
  106.         JButton b5 = new JButton("Remove Ground");
  107.         JButton b6 = new JButton("Remove Water");
  108.         JButton b7 = new JButton("Brew");
  109.  
  110.         addActionPanel.add(b1);
  111.         addActionPanel.add(b2);
  112.         addActionPanel.add(b3);
  113.         addActionPanel.setBorder(addActionBorder);
  114.         removeActionPanel.add(b4);
  115.         removeActionPanel.add(b5);
  116.         removeActionPanel.add(b6);
  117.         removeActionPanel.setBorder(removeActionBorder);
  118.         brewActionPanel.add(b7);
  119.         brewActionPanel.setBorder(brewActionBorder);
  120.  
  121.         //Assign Panels to 'westPanel' 
  122.         westPanel.add(addActionPanel, BorderLayout.PAGE_START);
  123.         westPanel.add(removeActionPanel, BorderLayout.CENTER); //Problem With this line
  124.         westPanel.add(brewActionPanel, BorderLayout.PAGE_END);
  125.  
  126.         return westPanel;
  127.     }
  128.  
  129.     public JPanel createSouthPanel() {
  130.         JPanel southPanel = new JPanel();
  131.  
  132.         JLabel statusBar = new JLabel("This test");
  133.         statusBar.setText("Testing");
  134.  
  135.         southPanel.add(statusBar);
  136.  
  137.         return southPanel;
  138.  
  139.     }
  140.  
  141.     private static void createAndShowGUI() {
  142.         //Create and set up the window.
  143.         JFrame frame = new JFrame("Bovinetine Maker Simulator");
  144.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  145.  
  146.         //Create and set up the content pane.
  147.         MenuLook demo = new MenuLook();
  148.         frame.setJMenuBar(demo.createMenuBar());
  149.         frame.setContentPane(demo.createContentPane());
  150.  
  151.         //Display the window.
  152.         frame.setSize(640,380);
  153.         frame.setVisible(true);
  154.     }
  155.  
  156.     public static void main(String[] args) {
  157.         createAndShowGUI();
  158.     }
  159.  
  160. }
  161.  
  162.  
May 15 '08 #4
JosAH
11,448 Expert 8TB
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
May 15 '08 #5
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.
May 16 '08 #6
JosAH
11,448 Expert 8TB
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
May 16 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Beach Potato | last post by:
Dear Y'all: I'm about to start porting a big old project written in anscient version of Delphi to something more stable, robust, supportable and maybe even portable. Since I haven't seriously...
15
by: drdoubt | last post by:
using namespace std In my C++ program, even after applying , I need to use the std namespace with the scope resolution operator, like, std::cout, std::vector. This I found a little bit...
19
by: James Fortune | last post by:
I have a lot of respect for David Fenton and Allen Browne, but I don't understand why people who know how to write code to completely replace a front end do not write something that will automate...
11
by: my-wings | last post by:
I think I've painted myself into a corner, and I'm hoping someone can help me out. I have a table of books (tblBooks), which includes a field (strPubName) for Publisher Name and another field...
7
by: Timothy Shih | last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I wrote a simple function which takes in 2 buffers (one a byte buffer, one a char buffer) and copies the contents of the byte...
48
by: Chad Z. Hower aka Kudzu | last post by:
A few of you may recognize me from the recent posts I have made about Indy <http://www.indyproject.org/indy.html> Those of you coming to .net from the Delphi world know truly how unique and...
4
by: Phil | last post by:
k, here is my issue.. I have BLOB data in SQL that needs to be grabbed and made into a TIF file and placed on the client (could be in temp internet dir). The reason we need it in TIF format is...
4
by: Quas.co.ua | last post by:
Hello all. I need your help. I need C compler to make demo of some technologie. This C compiler I need to write program which after run will be located in one segment of memory and it...
9
by: MrHelpMe | last post by:
Hello again experts, I have successfully pulled data from an LDAP server and now what I want to do is drop the data into a database table. The following is my code that will insert the data but...
20
by: mike | last post by:
I help manage a large web site, one that has over 600 html pages... It's a reference site for ham radio folks and as an example, one page indexes over 1.8 gb of on-line PDF documents. The site...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.