I'm working on an application using Swing in Java, however, I have a weird problem in the display, my code works perfectly fine, but the output is weird. When I run the program. The JFrame looks empty or something is missing (JMenuBar, JMenuItem, etc are invisible), then I maximize the screen, and all other stuff becomes visible, then I minimize the screen and it looks visible. I'm pretty sure that the code works fine, it's just a display problem. Can anyone help so that the first display looks fine?
Here is the code
JFrame frame = new JFrame("Menu");
frame.setVisible(true);
frame.setSize(400,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menubar = new JMenuBar();
frame.setJMenuBar(menubar);
JMenu file = new JMenu("File");
menubar.add(file);
JMenuItem exit = new JMenuItem("Exit");
file.add(exit);
JMenu help = new JMenu("Help");
menubar.add(help);
JMenuItem about = new JMenuItem("About");
help.add(about);
class exitaction implements ActionListener {
public void actionPerformed (ActionEvent e) {
System.exit(0);
}
}
exit.addActionListener(new exitaction());
}
Put the code below at the end. It'll work fine.
The method setVisible is an action, just like show() before JDK 1.5.
frame.setVisible(true);
Related
I've created a menubar and added a menu with several items. When I do doClick() on the JMenu(archiveMenu) it highlights the button for the menu but it doesnt show any of the items that are added to it. I've tried doing doClick() before the adding of actionListeners to the items and setVisible(true) but nothing works.
I think it has something to do with the fact that they have seperate actionListeners but I'm so far in to the program that changing to a single actionListener would lead to a huge amount of work. I appreciate any help, thanks!
It looks like this:
// MENU BAR
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu archiveMenu = new JMenu("Archive");
menuBar.add(archiveMenu);
JMenuItem newItem = new JMenuItem("New Map");
JMenuItem loadItem = new JMenuItem("Load places");
JMenuItem saveItem = new JMenuItem("Save");
JMenuItem exitItem = new JMenuItem("Exit");
newItem.addActionListener(new NewMapLis());
loadItem.addActionListener(new LoadLis());
saveItem.addActionListener(new SaveLis());
exitItem.addActionListener(new ExitLis());
archiveMenu.add(newItem);
archiveMenu.add(loadItem);
archiveMenu.add(saveItem);
archiveMenu.add(exitItem);
archiveMenu.doClick();
What is happening is that when doClick() is called the window is not fully loaded yet.
you need to perform it after loading.
for example, you could call it when the frame is opened:
myFrame.addWindowListener(new WindowAdapter() {
#Override
public void windowOpened(WindowEvent e) {
archiveMenu.doClick();
}
});
I'm trying to set some text to be displayed where the accelerator binding is usually displayed, for a JMenuItem.
The demarcated Ctrl+Z text in the following image is an example of what I'm trying to set, for another JMenuItem.
I don't actually want to set an accelerator for this JMenuItem, though.
I've poked around the source for several classes, like JMenuItem and BasicMenuItemUI, to no avail.
What's the simplest way to achieve this?
Thanks in advance :)
I assume the reason you want this is so you can prevent the menu from triggering the undo action a second time, when the key combination is already bound on a component on the frame, but this shouldn't be necessary. If the component consumes the key event, the menu won't detect it.
Here's an example with a JTextArea to see what I mean:
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JMenuBar menu = new JMenuBar();
frame.setJMenuBar(menu);
JMenu menuEdit = new JMenu("Edit");
menu.add(menuEdit);
JMenuItem menuEditUndo = new JMenuItem("Undo");
menuEdit.add(menuEditUndo);
menuEditUndo.setAccelerator(KeyStroke.getKeyStroke("control Z"));
menuEditUndo.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("menu");
}
});
JTextArea textArea = new JTextArea(20, 40);
textArea.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "undo");
textArea.getActionMap().put("undo", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("text");
}
});
frame.add(new JScrollPane(textArea));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
Both the text area and the menu item have bound to the same key combo, but pressing Ctrl+Z while the text area has focus prints only "text" and never "menu". I.e., the action does not happen twice. Although this uses a JTextArea, it should be true of any component.
The code is here:
I don't think the System.exit(); is working properly.
Am I using the wrong method or what?
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu mnRWindow = new JMenu("RWindow");
menuBar.add(mnRWindow);
JMenuItem mntmMore = new JMenuItem("More");
mnRWindow.add(mntmMore);
JMenuItem mntmExit = new JMenuItem("Exit");
mntmExit.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
System.exit(DISPOSE_ON_CLOSE);
}
});
mnRWindow.add(mntmExit);
Don't use a MouseListener with a JMenuItem.
A JMenuItem is designed to be used with an ActionListener. Read the section from the swing tutorial on How to Use Menus for more information and working examples.
Also, DISPOSE_ON_CLOSE is NOT a value you should be using for the System.exit(...) method. That variable should only be used with the setDefaultCloseOperation(...) method of the frame. Just use a "0" for the value.
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);
This is my code so far:
public static void main(String[] args){
JFrame frame = new JFrame("Breakout!");
frame.setLocation(300, 300);
//Making the menubar
JMenuBar mb = new JMenuBar();
frame.setJMenuBar(mb);
JMenu fileMenu = new JMenu("File");
mb.add(fileMenu);
JMenuItem newAction = new JMenuItem("About");
fileMenu.add(newAction);
newAction.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.out.println("open new window here");
}
});
BreakoutCourt c = new BreakoutCourt();
frame.add(c);
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
I am making a Breakout game. I want to make an About window that displays information about the game (like, how to play it and so on). How would I go about doing that? Thank you for the help! I'm very new to Java Swing, so yeah.
You could do a simple one with the message type of JOptionPane:
JOptionPane.showMessageDialog(frame, "Breakout! v1.0");
(You'll need to make the frame final to do this, since it's being accessed from within the anonymous action listener).
For more control over what is displayed, and whether it should be modal or not, you could look at JDialog. There's an overview of using dialogs in Swing here: http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html.