我用的是eclipse
import java.awt.Dimension;
import java.awt.Toolkit;import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class JMenuBarApp extends JFrame{ JMenu m1=new JMenu("File");
JMenu m2=new JMenu("Help");
JMenuBar mb1=new JMenuBar();
JMenuItem mi1=new JMenuItem("New");
JMenuItem mi2=new JMenuItem("Save");
JMenuItem mi3=new JMenuItem("Open");
JMenuItem mi4=new JMenuItem("Exit");
JMenuItem mi5=new JMenuItem("HelpFile");
public JMenuBarApp() {
this.setTitle("http://nicholas.com");
Dimension ds=Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth=ds.width;
int screenHeight=ds.height;
int x=(screenWidth-this.getWidth())/2;
int y=(screenHeight-this.getHeight())/2;
this.setLocation(x, y);
m1.add(mi1); m1.add(mi2); m1.add(mi3); m1.add(mi4);
m2.add(mi5);
this.setJMenuBar(mb1);
this.setVisible(true);
}
public static void main(String[] args) {
new JMenuBarApp();
}}
import java.awt.Dimension;
import java.awt.Toolkit;import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class JMenuBarApp extends JFrame{ JMenu m1=new JMenu("File");
JMenu m2=new JMenu("Help");
JMenuBar mb1=new JMenuBar();
JMenuItem mi1=new JMenuItem("New");
JMenuItem mi2=new JMenuItem("Save");
JMenuItem mi3=new JMenuItem("Open");
JMenuItem mi4=new JMenuItem("Exit");
JMenuItem mi5=new JMenuItem("HelpFile");
public JMenuBarApp() {
this.setTitle("http://nicholas.com");
Dimension ds=Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth=ds.width;
int screenHeight=ds.height;
int x=(screenWidth-this.getWidth())/2;
int y=(screenHeight-this.getHeight())/2;
this.setLocation(x, y);
m1.add(mi1); m1.add(mi2); m1.add(mi3); m1.add(mi4);
m2.add(mi5);
this.setJMenuBar(mb1);
this.setVisible(true);
}
public static void main(String[] args) {
new JMenuBarApp();
}}
....
讓他顯示
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/package jndi;/**
*
* @author silence
*/
import java.awt.Dimension;
import java.awt.Toolkit; import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class JMenuBarApp extends JFrame{ JMenu m1=new JMenu("File");
JMenu m2=new JMenu("Help");
JMenuBar mb1=new JMenuBar();
JMenuItem mi1=new JMenuItem("New");
JMenuItem mi2=new JMenuItem("Save");
JMenuItem mi3=new JMenuItem("Open");
JMenuItem mi4=new JMenuItem("Exit");
JMenuItem mi5=new JMenuItem("HelpFile"); public JMenuBarApp() {
this.setTitle("http://nicholas.com");
this.setSize(500,500);
Dimension ds=Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth=ds.width;
int screenHeight=ds.height;
int x=(screenWidth-this.getWidth())/2;
int y=(screenHeight-this.getHeight())/2;
this.setLocation(x, y);
m1.add(mi1); m1.add(mi2); m1.add(mi3); m1.add(mi4);
m2.add(mi5);
mb1.add(m1);
mb1.add(m2);
this.setJMenuBar(mb1);
this.setVisible(true); }
public static void main(String[] args) { new JMenuBarApp();
} }