the code that I wrote does not show the menubar, any idea what I should do?
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//What happens on close.
this.setSize(1100, 750);//Initial size of the frame.
menuBar = new MenuBar();
east = new EastPanel();
central = new CentralPanel();
south = new SouthPanel();
//Add the menu bar to the frame.
this.setJMenuBar(menuBar.getComposition());
//Frame's other components.
this.add(central.getCentralPanel());
//this.add(msBoard.getMessagesBoard(), BorderLayout.SOUTH);
this.add(east.getEastPanel(), BorderLayout.EAST);
this.add(south.getSouthPanel(), BorderLayout.SOUTH);
//this.add(menuBar);
//Load the card images.
//cards = new CardImages();
//cards.loadCards();
//Initialize cardsPerPlayer list.
//cardsPerPlayer = new ArrayList<ImagePanel>();
this.setVisible(true);
where getComposition is
public JMenuBar getComposition(){
return this.Composition;
}
and Composition is private JMenuBar
Adding MenuBar is not enough. You should attach it to the current JFrame object.
Also make sure you're adding some menu items into it.
Example:
frame.setJMenuBar(theJMenuBar);
Propably because You didint add anything to this menubar.
In eclipse when ive added menu bar (using builder) without any items i didint see it but after i add menu to menubar it will works.
Related
I'm creating an undecorated jFrame, and I wanted to add a jMenuBar to this jFrame to allow me to use drop-down menus for various things.
But, the jMenuBar always defaults to the very top of my jFrame, and it being undecorated, it pushes down a jPanel I had set there with the "Minimize" and "Close" buttons, and trying to move it anywhere else is unsuccessful.
I've tried using a jToolBar, and while it allows for more customization in terms of placement, I can't find a way to add drop-down menus onto the buttons there.
Is there a way to move a jMenuBar somewhere else than the top or to add a drop-down menu to a jToolBar?
You should have a look at Layout Managers.
Here is a simple example to achieve what you want, using a BorderLayout:
public static void main(String[] args) {
JFrame jf = new JFrame();
JPanel container = new JPanel();
container.setLayout(new BorderLayout());
JLabel label = new JLabel("North");
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("A south menu");
menu.add(new JMenuItem("A south item"));
menuBar.add(menu);
container.add(label, BorderLayout.NORTH);
container.add(menuBar, BorderLayout.SOUTH);
jf.add(container);
jf.setPreferredSize(new Dimension(200,100));
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
jf.pack();
}
result:
I'm making a game of checkers for my A2 Computing coursework which is due in within a week. I have completely finished the game, and only thing I have left to do is connect the two JPanels together via a CardLayout that I have made. However I am unsure how to do so, if anyone could possibly tell me how I would be extremely grateful and I've been trying to figure it out for awhile and It just is messing up my structure. Thank you
Here is the code from my CardLayout:
public class CLayout extends JPanel implements ItemListener {
private JFrame ourFrame = new JFrame("Main Menu");
private JPanel ourMasterPanel, comboxPanel, cardPanel, cardPanelOne, cardPanelTwo;
private String[] boxitems = { "Play Checkers", "Options"};
private JComboBox<String> ourBox = new JComboBox<String>(boxitems);
private JButton[] ourButtons = new JButton[]
{
new JButton("Single Player"),
new JButton("Multiplayer"),
new JButton("Rules for Checkers"),
new JButton("Fun Facts about Checkers"),
new JButton("Checkers Strategy and Tips"),
new JButton("Exit")
};
CLayout()
{
ourMasterPanel = (JPanel) ourFrame.getContentPane();
ourMasterPanel.setLayout(new BorderLayout());
comboxPanel = new JPanel();
comboxPanel.setLayout(new FlowLayout());
comboxPanel.add(ourBox);
ourBox.addItemListener(this);
ourBox.setEditable(false);
cardPanel = new JPanel();
cardPanel.setLayout(new CardLayout());
cardPanelOne = new JPanel();
cardPanelOne.add(ourButtons[0]);
cardPanelOne.add(ourButtons[1]);
cardPanelTwo = new JPanel();
cardPanelTwo.add(ourButtons[2]);
cardPanelTwo.add(ourButtons[3]);
cardPanelTwo.add(ourButtons[4]);
cardPanelTwo.add(ourButtons[5]);
cardPanel.add(cardPanelOne,boxitems[0]);
cardPanel.add(cardPanelTwo,boxitems[1]);
ourMasterPanel.add(comboxPanel,BorderLayout.NORTH);
ourMasterPanel.add(cardPanel,BorderLayout.SOUTH);
ourFrame.setVisible(true);
ourFrame.setSize(350,250);
ourFrame.setResizable(false);
ourFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ourFrame.pack();
ourFrame.validate();
}
#Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
CardLayout cardLayout = (CardLayout)(cardPanel.getLayout());
cardLayout.show(cardPanel, (String)e.getItem());
}
}
I have kept the code I am displaying to a minimal, hence I have removed all the action listeners for my buttons, anyway the problem I have is that, I would like it so that when the user clicks on the 'Multiplayer' button which is the array button ourButtons[1], it will then transition into my main game screen so that the user can then play a game of checkers.
Here is the main important GUI from my CheckerBoard class:
public class CheckerBoard extends JPanel implements ActionListener, MouseListener {
// Main routine that opens an Applet that shows a CheckerBoard
public static void main(String[] args) {
new CLayout();
}
public static void main1(String[] args)
{
JFrame application = new JFrame("Checkers"); // Sets the title at the top of the application as 'Checkers'
JMenuBar bar = new JMenuBar(); // Adds the Menu Bar
JMenu fileMenu = new JMenu("File"); // Adds a File Tab to the Menu Bar
JMenu helpMenu = new JMenu("Help"); // Adds a Help Tab to the Menu Bar
JMenuItem exit = new JMenuItem("Exit"); // Adds the Exit sub-tab as an Item of the JMenu
JMenuItem mainMenu = new JMenuItem("Main Menu"); // Adds the Main Menu sub-tab as an Item of the JMenu
final JMenuItem rules = new JMenuItem("Rules"); // Adds the Rules of Checkers sub-tab as an Item of the JMenu
final JMenuItem checkersStrategyandTips = new JMenuItem("Checkers Strategy and Tips"); // Adds the Checkers Strategy and Tips sub-tab as an item of the JMenu
final JMenuItem funFactsaboutCheckers = new JMenuItem("Fun Facts about Checkers"); // Adds the Fun Facts about Checkers sub-tab as an item of the JMenu
helpMenu.add(funFactsaboutCheckers); // Adds the Fun Facts about Checkers into the Help tab
helpMenu.add(checkersStrategyandTips); // Adds the How to Play tab into the Help Tab
helpMenu.add(rules); // Adds the Rules of Checkers tab into the Help tab
fileMenu.add(mainMenu);// Adds the Main Menu sub-tab into the File tab
fileMenu.addSeparator(); // Adds a line in between the Main Menu sub-tab and the Exit sub-tab
fileMenu.add(exit); // Adds the Exit sub-tab into the Menu tab
bar.add(fileMenu); // Adds the Menu tab to the Menu bar
bar.add(helpMenu); // Adds the Help tab to the Menu Bar
application.setJMenuBar(bar); // Adds the Menu Bar to the application window
CheckerBoard content = new CheckerBoard(); // Sets the CheckerBoard values into the content to be used in the next line
application.setContentPane(content); // Container holds the values together, Content pane of the CheckerBoard
application.pack(); // Use preferred size of content to set size of application.
Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
application.setLocation( (screensize.width - application.getWidth())/2,
(screensize.height - application.getHeight())/2 );
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); // Sets so that the application can be exited when the application is closed
application.setResizable(false); // This makes it so that the user can't change the application's size.
application.setVisible(true); // Sets it so that the application can actually be seen
}
private JButton NewGameButton; // Button for starting a new game.
private JButton ResignButton; // Button that a player can use to end the game by resigning
private JLabel MessageDisplay; // Label for displaying messages to the user.
public CheckerBoard() {
// This is going to be a constructor, this constructor will set the layout manager for the panel to be null, it will then add components to the panel
// and it will set their bounds.
setLayout(null); // So that it will match my requirement specification, I will do the layout myself
setBackground(new Color(0,120,0)); // Dark Green Background colour.
setPreferredSize(new Dimension(350,250)); // The size of the Panel
BoardComplete checkerboard = new BoardComplete();
add(checkerboard); // This will create the components and add them to the content pane
add(NewGameButton);
add(ResignButton);
add(MessageDisplay);
// I will now have to produce a method to set the position and size of each component by calling its setBounds() method
checkerboard.setBounds(20,20,164,164); // Sets the board dimensions
NewGameButton.setBounds(210, 60, 120, 30);
ResignButton.setBounds(210, 120, 120, 30);
MessageDisplay.setBounds(20, 200, 350, 30);
}
Hopefully what I am aiming for is understandable, any help or advice would be extremely appreciated. Thank you. I'm sorry for the large amount of code, anyway I would be extremely grateful for any help as this is the last problem that I am facing before my coding is finished xD Thank you
you can also explicitly name you Checkerboard panel with something like "Checkerboard", so when you add the Checkerboard to your CardPanel, you can use"dummycardpanel.add(checkerboard, "Checkerboard");" and use show(JPanel, "Checkerboard") as your show method to do it, at least for whatever button you want to fire up the game(that may not work for other buttons which will need their own listeners most likely, but the idea will be mostly the same.
I did something similar recently with a single overridden ActionListener and set the ActionCommands for all my JButtons manually, then used a conditionals branch inside the listener after assigning the listener to all my buttons. a different way of doing it, but either way can work.
I hope this helps
Here I've created the JFrame object, 'window'. And set it to 'true'.
import java.awt.*;
public static void main(String[] args) {
JFrame window = new JFrame("GUI Test");
window.setSize(250, 100);
window.setLocation(100, 100);
window.setVisible(true);
Here I've added menu items to the 'menu'. And set visibility as 'true'.
JMenuItem home = new JMenuItem("Home");
JMenuItem about = new JMenuItem("About");
JMenuItem tag = new JMenuItem("Tag");
JMenu menu = new JMenu("menu");
menu.add(home);
menu.add(about);
menu.add(tag);
menu.setVisible(true);
}
I'm not getting any errors. Then what am I missing? Why doesn't my menu display in the 'window' object?
You need to add your JMenus to a JMenuBar and set to to the JFrame using JFrame#setJMenuBar
See How to use Menus for more details
Frirst creat JMenubar and add the JMenu into that.
JMenuBar menubar = new JMenuBar();
Add the JMenu into JMenuBar and JMenuar into JFrame to display in the window.
menubar.add(menu);
window.setJMenuBar(menubar);
You need to add JMenuBar with window,
JMenuBar menuBar= new JMenuBar();
.....
menuBar.add(menu);//Add menu with JMenuBar.
window.setJMenuBar(menuBar);
Add the Jmenu into in you JFrame
window.add(menu);
and also set the layout to JFrame
window.setLayout(new Flawlayout()); // you can try other layout too
And best way is
How to use Menus
Here is the code I am using:
JMenu menu = new JMenu("Menu");
JMenuItem item = new JMenuItem("Add");
item.add(new JButton("SOMETHING A BIT WORDY"));
menu.add(item);
// ...
JMenuBar menuBar = new JMenuBar();
menuBar.addMenu(menu);
JFrame frame = new JFrame();
frame.setJMenuBar(menuBar);
// ...
frame.pack();
frame.setVisible();
However, the button (inside of the menu item) appears small and only contains the text "..." which is used when the button's size cannot fit the intended text. Is there any way to make my JMenuItem "grow" to fit my JButton (or JTextArea, or JLabel, or whatever the Component may be)?
I totally agree with others who posted their comments, it may be an odd mix to put an button in the menuitem, if you insist to this, setPreferredSize can solve your problem.
I used a bad hard code implementation, if anyone know how to set the width dynamically, please guide me too. :P thx in advance.
JMenu menu = new JMenu("Menu");
JMenuItem item = new JMenuItem("Add");
item.add(new JButton("SOMETHING A BIT WORDY"));
item.setLayout(new FlowLayout()); // set FlowLayout for item
item.setPreferredSize(new Dimension(200, 100)); // hard code implementation :-(
menu.add(item);
// ...
JMenuBar menuBar = new JMenuBar();
menuBar.add(menu);
JFrame frame = new JFrame();
frame.setJMenuBar(menuBar);
// ...
frame.pack();
frame.setVisible(true);
I've got a JMenu and I want to change the window's content according to what button from the menu is pressed. I managed to show the panel as a popup, but I want it to be displayed in the same window with the menu. This is my code so far :
public class GUImenu extends JFrame
{
private JMenuBar menuBar;
private JMenu menu;
private JMenu subMenu;
private JMenuItem item1;
private JMenuItem item2;
private JMenuItem item3;
private JMenuItem item4;
private JMenuItem item5;
private JMenuItem item6;
public GUImenu()
{
super("Example Menu System");// Call the JFrame constructor.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Specify an action for the close button.
buildMenuBar();
// Pack and display the window.
pack();
setSize(1000, 250); // set frame size
setVisible(true);
}
private void buildMenuBar()
{
// Create the menu bar.
menuBar = new JMenuBar();
// Create the file and text menus.
menu = new JMenu("Menu"); menuBar.add(menu);
subMenu = new JMenu("Create Customer");
item1 = new JMenuItem("Ordinary Customer"); subMenu.add(item1);
item1.addActionListener(new showOrdinaryCust());
item6 = new JMenuItem("Privileged Customer"); subMenu.add(item6);
menu.add(subMenu);
item2 = new JMenuItem("View Customers Who Didn't Pay"); menu.add(item2);
item3 = new JMenuItem("Remove Client");menu.add(item3);
item4 = new JMenuItem("Create Order"); menu.add(item4);
item5 = new JMenuItem("Search..."); menu.add(item5);
setJMenuBar(menuBar);
}
public static void main(String[] args)
{
new GUImenu();
}
private class showOrdinaryCust implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if(event.getSource()==item1)
GUIpanel.main(null);
}
}
}
I would try to fill the entire window with a CardLayout. CardLayout is meant to switch its contents between separate views. Simply set up multiple cards for each of the panels you want to show and have the menu switch between them.
If you use windows or dialogs you will latter have to deal with the focus, the closing, minimizing, maximizing, re-size, centering, visibility...
In your case i would recommend you to pick a good layout to suit your needs(Probably the easiest way to achieve your goal).
What do you think about tabbed panes?
See this link: http://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html
building guis is a little complex, but worth the time spent to understand what options are. This is a good place to start as it explains various java gui layouts, including using a layout manager. http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html.
For future posts, your example should be complete, including imports so we can copy and paste code, compile and look at.
It is simple enough. I have implemented this thing as follows :
First get content pane of your JFrame, say in container. Make this container object static.
private static Container container;
now get content pane.
container = this.getContentPane();
Now on click on menu call some method that will do some thing like this :
container.removeAll();
container.add(new JPanel()); //Add object of your panel you want to show.
container.revalidate();
This method is helpful in case you want to show multiple panels in same JFrame.