473,657 Members | 2,595 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help in my code

crystal2005
44 New Member
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.
May 12 '08 #1
6 1663
JosAH
11,448 Recognized Expert MVP
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
crystal2005
44 New Member
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
crystal2005
44 New Member
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 Recognized Expert MVP
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
crystal2005
44 New Member
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 Recognized Expert MVP
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
2640
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 touched C for large implementations, I'm seeking advice on what to use for development. My ultimate goal is to spend as less time on it as possible. I'll be writing it in Windows 32-bit environment, probably Win2000 or Win98. Planning to use a...
15
3673
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 cumbersome to always include std. I somewhere found a trick to overcome this problem. By using using std::cout;
19
4091
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 the code that implements managing unbound controls on forms given the superior performance of unbound controls in a client/server environment. I can easily understand a newbie using bound controls or someone with a tight deadline. I guess I need...
11
2794
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 (strPubCity) for Publisher City. These two fields have a many-to-one relationship with tables, (tlkpPubName and tlkpPubCity) respectively. The lookup tables only have one field (strPubName and strPubCity), which is their primary key. I also have...
7
3301
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 buffer into the character pointer. The code looks like the following: #include <stdio.h> #include <stdlib.h> #include "stdafx.h" BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call,
48
3223
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 "huge" Indy is both as a project, in support, development, and use. But Indy is new to the .net world. Indy is a HUGE library implementing over 120 internet protocols and standards and comes with complete source. Its an open source project, but not...
4
7394
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 there are multiple pages per invoice. How can I grab the data, make the TIF, place it on the client and then Open with the clients default program for veiwing TIF's (usually Microsoft Picture and Fax Viewer). Please help.
4
2748
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 generates another code an write it into another (second) segment. And after some fragment of code is generated program from first segment runnes just generated code from second segment which may return in first
9
2187
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 that has problems. FullName=Request.Form("Name") Email=Request.Form("Email") GivenName=Request.Form("GivenName")
20
4255
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 is structured as an upside-down tree, and (if I remember correctly) never more than 4 levels. The site basically grew (like the creeping black blob) ... all the pages were created in Notepad over the last
0
8397
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8310
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8827
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
4315
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2731
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1957
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1620
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.