1

I'm playing around with the JMenu class, and something unexpected happened.

I run the code below, and the JFrame appears, but the MenuBar and contents do not. However, when I resize the JFrame with the mouse, the elements pop into existence.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Main {
  public static void main(String[] args) {
    JFrame frame = new JFrame("test");
    frame.setVisible(true);
    frame.setSize(300,300);     
    frame.setLocationRelativeTo(null); // centered on monitor   
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JMenuBar menubar = new JMenuBar();
    frame.setJMenuBar(menubar);
    menubar.setVisible(true);

    JMenu file = new JMenu("File");
    menubar.add(file);

    JMenuItem exit = new JMenuItem("Exit");
    file.add(exit);

    JMenu help = new JMenu("Help");
    menubar.add(help);

    /**
     * Label and text and button
     */
    JLabel label = new JLabel("Hello there");
    JPanel panel = new JPanel();
    frame.add(panel);
    panel.add(label);

    JButton button = new JButton("Submit Button");
    panel.add(button);
    button.setToolTipText("this is my tooltip text");    
  }
}

Can anyone see anything suspicious that I can't see?

4

1 に答える 1

0

それは単に frame.setVisible(true); だったことがわかりました。

もっと下に置く必要がありました。

于 2012-12-18T06:04:30.320 に答える