Can someone please tell me how I can add my menubar to the borderlayout NORTH.
I have written it this way.
private void makeFrame()
{
frame = new JFrame("Game");
Container contentPane = frame.getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(menubar, BorderLayout.NORTH);
contentPane.add(new JButton("south"), BorderLayout.SOUTH);
contentPane.add(new JButton("center"), BorderLayout.CENTER);
contentPane.add(new JButton("east"), BorderLayout.EAST);
frame.pack();
frame.setVisible(true);
makeMenuBar();
}
public void makeMenuBar(){
JMenuBar menubar = new JMenuBar();
JMenu menu;
JMenuItem item;
JMenu file = new JMenu("File");
menubar.add(file);
item = new JMenuItem("New Game...");
file.add(item);
item = new JMenuItem("Save As...");
file.add(item);
item = new JMenuItem("Quit");
file.add(item);
}
Can anyone tell me how to add this menubar to border north please.
Generally speaking, you shouldn't, instead, you should use JFrame#setJMenuBar instead
The problem you are having seems to be the fact that you are either adding the menu bar to the container BEFORE it's initialized or are creating a new instance of JMenuBar which is never added to the screen
private void makeFrame()
{
frame = new JFrame("Game");
Container contentPane = frame.getContentPane();
contentPane.setLayout(new BorderLayout());
// ?? Don't know what this initialised to...
contentPane.add(menubar, BorderLayout.NORTH);
//...
makeMenuBar();
}
public void makeMenuBar(){
// A local variable with the same name...??
JMenuBar menubar = new JMenuBar();
Oh, and the instance you are creating in makeMenuBar is only local to the method...so it will never have an effect on the menubar variable you are using in makeFrame...
Related
I'm trying to have one panel appear under the other (textPanel to appear under mainPanel).
But instead, textPanel is appearing overtop of mainPanel:
FlatLightLaf.setup();
FlatLightLaf.supportsNativeWindowDecorations();
UIManager.setLookAndFeel(new FlatLightLaf());
JFrame frame = new JFrame();
frame.setTitle("Title");
frame.setSize(640, 480);
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
JMenuItem menuFileSave = new JMenuItem("Save");
menuFileSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
menu.add(menuFileSave);
menuBar.add(menu);
FlowLayout mainLayout = new FlowLayout(FlowLayout.LEFT);
frame.setJMenuBar(menuBar);
JPanel mainPanel = new JPanel(new BorderLayout());
String datesString[] = {"Tuesday May-03-2022", "Monday May-02-2022"};
#SuppressWarnings("rawtypes")
JComboBox dateDropDown = new JComboBox<>(datesString);
dateDropDown.setFont(new Font("Calibri", Font.PLAIN, 17));
dateDropDown.setVisible(true);
mainPanel.add(dateDropDown);
mainPanel.setVisible(true);
frame.add(mainPanel);
JPanel textPanel = new JPanel(new BorderLayout());
JTextArea textArea = new JTextArea();
textPanel.add(textArea);
textPanel.setVisible(true);
frame.add(textPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
How do I ensure textPanel goes below mainPanel on its own row/line, and not overtop of it?
The default, when adding one component, without further arguments, to a JFrame is that the component in question will take up the entire client area, since JFrame uses a BorderLayout by default. So, your solution is to use a further argument, telling it to add it at the bottom of the layout:
frame.add(textPanel, BorderLayout.PAGE_END);
I have a problem with my application GUI. I would like to create one global JMenuBar and share it to other JPanels, but if i want to assign to multi JPanels i have error:
#
"The menuBar component is added to a parent component more than once.
•panelAll.add(menuBar);
•panelTask.add(menuBar);"
#
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
JPanel panelAll = new JPanel();
frame.getContentPane().add(panelAll, "name_218556506364138");
panelAll.setLayout(null);
JMenuBar menuBar = new JMenuBar();
menuBar.setBounds(0, 0, 795, 21);
panelAll.add(menuBar);
JPanel panelTask = new JPanel();
frame.getContentPane().add(panelTask, "name_218567310779840");
panelTask.setLayout(null);
panelTask.add(menuBar);
JPanel panelMyTask = new JPanel();
frame.getContentPane().add(panelMyTask, "name_218578712986622");
panelMyTask.add(menuBar);
JPanel panelMySoftware = new JPanel();
frame.getContentPane().add(panelMySoftware, "name_218590026900741");
panelMySoftware.add(menuBar);
JPanel panelMyDevices = new JPanel();
frame.getContentPane().add(panelMyDevices, "name_218598029981563");
panelMyDevices.add(menuBar);
}
}
Image
i don't think its a good idea to add a JMenuBar into a JPanel, but if you insist...
a JMenuBar can be added only to one container, so you need to create more instances of the JMenuBar. That should work without problems if you use the command pattern.
//first instance
JMenuBar taskMenuBar = new MyJMenuBarImplementation();
JPanel panelMyTask = new JPanel();
frame.getContentPane().add(panelMySoftware, "name_xxx");
panelMyTask.add(taskMenuBar);
//second instance
JMenuBar softwareMenuBar = new MyJMenuBarImplementation();
JPanel panelMySoftware = new JPanel();
frame.getContentPane().add(panelMySoftware, "name_yyy");
panelMySoftware.add(softwareMenuBar);
//and so on...
I need to create a JMenuBar that will be shown under of JTabbedPane named as shapes. When I changed tab, menu bar will not be shown in the other tab. You can see what I mean from images.
I created a menu bar and add it to JFrame but that is not I mean. If you did not understand a point let me explain.
public class Gui extends JPanel {
JFrame frame;
JTabbedPane tabbedPane;
JMenuBar menuBar;
JMenu createShapes, display, help;
JMenuItem RandomShapes, Rectangle, Circle, Square;
public void createFrame() {
frame = new JFrame();
frame.setSize(1000, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.setVisible(true);
frame.setFocusable(false);
setVisible(true);
setSize(1000, 600);
}
public void createMenus() {
menuBar = new JMenuBar();
//frame.setJMenuBar(menuBar);
createShapes = new JMenu("CreateShapes");
menuBar.add(createShapes);
display = new JMenu("Display");
menuBar.add(display);
help = new JMenu("Help");
menuBar.add(help);
RandomShapes = new JMenuItem("Random Shapes");
createShapes.add(RandomShapes);
Rectangle = new JMenuItem("Rectangle");
createShapes.add(Rectangle);
Circle = new JMenuItem("Circle");
createShapes.add(Circle);
Square = new JMenuItem("Square");
createShapes.add(Square);
}
public Gui() {
createFrame();
createMenus();
frame.add(this);
//===frames part
tabbedPane = new JTabbedPane();
frame.getContentPane().add(tabbedPane);
JPanel gui2 = new JPanel();
tabbedPane.addTab("Shapes", this);
tabbedPane.addTab("Images", gui2);
//===endframespart
}
}
The other tab image
Shapes tab image
Actually, you cannot set menu bar to JTabbedPane.
You need to add JInternalFrame inside one of the tabs of JTabbedPane, then
you can call setJMenuBar of JInternalFrame.
Here is a simple example:
JInternalFrame jInternalFrame = new JInternalFrame();
jMenuBar = new javax.swing.JMenuBar();
jMenu1 = new JMenu("Save");
jMenu2 = new JMenu("Open");
jMenuBar.add(jMenu1);
jMenuBar.add(jMenu2);
jInternalFrame.setJMenuBar(jMenuBar);
tabbedPane.addTab("tab3", jInternalFrame);
I'm working on a Java desktop Application that uses multiple Jpanels as cards in a CardLayout, the problem is that when I switch between the cards the JmenuBar disappears totally or just parts of it.I have a mainFramewhich holds all the Jpanels and the JmenuBar, the other Jpanels have some texts and JButtons and JLabels Here's how I'm implementing this :
This is the Frame that holds everything mainFrame.java
public class mainFrame extends JFrame {
JPanel cards ;
menubar menu= new menubar();
mainPanel card1 = new mainPanel();
Ajout card2= new Ajout();
//ViewAjoutEnf card3= new ViewAjoutEnf();
public mainFrame(){
setResizable(true);
setExtendedState(JFrame.MAXIMIZED_BOTH);
this.add(menu);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cards = new JPanel(new CardLayout());
cards.add(card1, "Card 1");
cards.add(card2, "Card 2");
// cards.add(card3, "Card 3");
getContentPane().add(cards);
}
This is the JmenuBar class menubar.Java:
public class menubar extends JMenuBar{
JMenu menuouvrir = new JMenu("ملف");
JMenuItem ajoutbase = new JMenuItem("فتح");
JMenuItem quiter = new JMenuItem("خروج ");
JMenuItem motpass = new JMenuItem("تبديل كلمة السر");
JMenu menuajout = new JMenu("ادخال ");
JMenuItem ajoutprodui = new JMenuItem("ادخال منتوج");
JMenuItem listproduit = new JMenuItem("لائحة المنتوجات");
JMenu menusortie = new JMenu("اخراج");
JMenuItem sortiproduit = new JMenuItem("اخراج منتوج");
JMenu retour = new JMenu("عودة ");
JMenu menuapropos = new JMenu("?");
public menubar (){
this.setBounds(0, 0, 1370, 30);
this.add(Box.createHorizontalGlue());
this.setFont(new Font("sans-serif", Font.PLAIN, 12));
menuouvrir.add(ajoutbase);
menuouvrir.add(motpass);
menuouvrir.addSeparator();
menuouvrir.add(quiter);
menuajout.add(ajoutprodui);
menuajout.add(listproduit);
menusortie.add(sortiproduit);
this.add(menuapropos);
this.add(retour);
this.add(menusortie);
this.add(menuajout);
this.add(menuouvrir);
this.setVisible(true);
}
and these are the Two Jpanels i have:
mainPanel.Java
public class mainPanel extends JPanel {
JButton but = new JButton("dsdffd");
public mainPanel(){
this.setLayout(null);
this.add(but);
but.setBounds(70,500,70,70);
this.setBackground(Color.white);
this.setVisible(true);
}
}
and Ajou.Java:
public class Ajout extends JPanel{
JButton but = new JButton("dsdffd");
public Ajout(){
this.setLayout(null);
this.add(but);
but.setBounds(70,500,70,70);
this.setBackground(Color.white);
this.setVisible(true);
}
}
Don't extend JMenuBar. There is no reason to do this. You create an instance of JMenuBar and and add JMenu instances to the menu bar.
Don't use a null layout. A JMenuBar has its own layout manager.
I don't see where you add the menu bar to the frame using the setJMenBar(...) method.
Read the section from the Swing tutorial on How to Use Menus for more information and working examples. The examples will also show you how to better structure your code.
Also, don't keep extending panels just to add a single component to the panel. You can add multiple components to any panel. And make sure your panels use a layout manager.
When I run the program, I see a blank window.
How am I able to solve this? Thanks you. What I did wrong?
Here is my code:
public class Environment{
private JFrame frame;
private JMenu jmenu;
private JMenuItem menuItem;
private JMenuBar menuBar;
Environment(){
frame = new JFrame("Notepad");
menuBar = new JMenuBar();
//menuBar.setVisible(true);
jmenu = new JMenu("Test");
menuItem = new JMenuItem("Open");
jmenu.add(menuItem);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new FlowLayout());
frame.setSize(660, 350);
// Set a main menu
frame.setJMenuBar(menuBar);
menuBar.add(jmenu);
frame.setVisible(true);
}
}
Adding label1 and button1 - You can fix layout etc. Suggest you to use Jpanel as well.
public class Enviornment {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame;
JMenu jmenu;
JMenuItem menuItem;
JMenuBar menuBar;
frame = new JFrame("Notepad");
menuBar = new JMenuBar();
menuBar.setVisible(true);
jmenu = new JMenu("Test");
menuItem = new JMenuItem("Open");
jmenu.add(menuItem);
JLabel label1 = new JLabel("My Name");
JButton button1 = new JButton("Button");
frame.add(label1);
frame.add(button1);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new FlowLayout());
frame.setSize(660, 350);
// Set a main menu
frame.setJMenuBar(menuBar);
menuBar.add(jmenu);
frame.setVisible(true);
}
}