Eclipse - Java import似乎丢失了



我正在玩JMenu类,发生了一些意想不到的事情。

我运行下面的代码,JFrame出现,但MenuBar和内容没有。但是,当我用鼠标调整JFrame的大小时,元素会弹出。

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");    
  }
}

有人能看到我看不到的可疑事物吗?

原来它只是 frame.setVisible(true);

我需要把它放在下面更远的地方。

最新更新