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.
6 1647
Look at your menu bar class: -
class MenuBarPanel extends JMenuBar{
-
-
public JMenuBar MainMenuBar() {
-
JMenuBar menu = new JMenuBar();
-
-
menu.add(CreateFileMenu());
-
menu.add(new JSeparator());
-
menu.add(CreateActionMenu());
-
-
return menu;
-
}
-
...
-
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
Look at your menu bar class: -
class MenuBarPanel extends JMenuBar{
-
-
public JMenuBar MainMenuBar() {
-
JMenuBar menu = new JMenuBar();
-
-
menu.add(CreateFileMenu());
-
menu.add(new JSeparator());
-
menu.add(CreateActionMenu());
-
-
return menu;
-
}
-
...
-
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
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. -
import java.awt.*;
-
import java.awt.event.*;
-
import javax.swing.*;
-
import javax.swing.border.*;
-
-
public class MenuLook {
-
-
public JMenuBar createMenuBar() {
-
JMenuBar menuBar = new JMenuBar(); //Create the menu bar.
-
-
menuBar.add(createFileMenu());
-
menuBar.add(createActionMenu());
-
menuBar.add(createHelpMenu());
-
-
return menuBar;
-
}
-
-
public JMenu createFileMenu() {
-
JMenu file = new JMenu("File");
-
file.setMnemonic(KeyEvent.VK_F);
-
-
//Group of 'File' JMenuItems
-
JMenuItem fileItem1 = new JMenuItem("Reset", KeyEvent.VK_R);
-
JMenuItem fileItem2 = new JMenuItem("Exit", KeyEvent.VK_R);
-
-
file.add(fileItem1);
-
file.addSeparator();
-
file.add(fileItem2);
-
-
return file;
-
}
-
-
public JMenu createActionMenu() {
-
JMenu action = new JMenu("Action");
-
action.setMnemonic(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("Remove Pot");
-
JMenuItem actionItem5 = new JMenuItem("Remove Ground");
-
JMenuItem actionItem6 = new JMenuItem("Remove Water");
-
JMenuItem actionItem7 = new JMenuItem("Brew");
-
-
action.add(actionItem1);
-
action.add(actionItem2);
-
action.add(actionItem3);
-
action.addSeparator();
-
action.add(actionItem4);
-
action.add(actionItem5);
-
action.add(actionItem6);
-
action.addSeparator();
-
action.add(actionItem7);
-
-
return action;
-
}
-
-
public JMenu createHelpMenu() {
-
JMenu help = new JMenu("Help");
-
help.setMnemonic(KeyEvent.VK_H);
-
-
//Group of 'help' JMenuItems
-
JMenuItem helpItem1 = new JMenuItem("About BM Simulator");
-
-
help.add(helpItem1);
-
-
return help;
-
}
-
-
public Container createContentPane() {
-
//Create the content-pane-to-be.
-
JPanel contentPane = new JPanel(new BorderLayout());
-
//contentPane.setOpaque(true);
-
-
//Create a scrolled text area.
-
JTextArea output = new JTextArea(5, 30);
-
output.setEditable(true);
-
JScrollPane scrollPane = new JScrollPane(output);
-
-
//Add the text area to the content pane.
-
contentPane.add(scrollPane, BorderLayout.CENTER);
-
-
contentPane.add(createWestPanel(), BorderLayout.WEST);
-
contentPane.add(createSouthPanel(), BorderLayout.SOUTH);
-
-
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 removeActionPanel = new JPanel(new GridLayout(3,1,10,10));
-
JPanel brewActionPanel = new JPanel(new GridLayout(1,1,10,10));
-
-
TitledBorder addActionBorder = new TitledBorder("Add Action");
-
TitledBorder removeActionBorder = new TitledBorder("Remove Action");
-
TitledBorder brewActionBorder = new TitledBorder("Brew 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(addActionBorder);
-
removeActionPanel.add(b4);
-
removeActionPanel.add(b5);
-
removeActionPanel.add(b6);
-
removeActionPanel.setBorder(removeActionBorder);
-
brewActionPanel.add(b7);
-
brewActionPanel.setBorder(brewActionBorder);
-
-
//Assign Panels to 'westPanel'
-
westPanel.add(addActionPanel, BorderLayout.PAGE_START);
-
westPanel.add(removeActionPanel, BorderLayout.CENTER); //Problem With this line
-
westPanel.add(brewActionPanel, BorderLayout.PAGE_END);
-
-
return westPanel;
-
}
-
-
public JPanel createSouthPanel() {
-
JPanel southPanel = new JPanel();
-
-
JLabel statusBar = new JLabel("This test");
-
statusBar.setText("Testing");
-
-
southPanel.add(statusBar);
-
-
return southPanel;
-
-
}
-
-
private static void createAndShowGUI() {
-
//Create and set up the window.
-
JFrame frame = new JFrame("Bovinetine Maker Simulator");
-
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
-
//Create and set up the content pane.
-
MenuLook demo = new MenuLook();
-
frame.setJMenuBar(demo.createMenuBar());
-
frame.setContentPane(demo.createContentPane());
-
-
//Display the window.
-
frame.setSize(640,380);
-
frame.setVisible(true);
-
}
-
-
public static void main(String[] args) {
-
createAndShowGUI();
-
}
-
-
}
-
-
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
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.
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
| |