日期:2014-05-20  浏览次数:20779 次

懂java的速来帮忙
这是一个用java实现菜单的简单例子,但小弟我不才,有几点看不懂,望高手指点!!!



import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
public class MenuTest extends JFrame {
public static void main(String[] args) {
  JFrame f = new MenuTest("MenuTest");
  f.setSize(new Dimension(300,230));//此中Dimension 的作用是什么啊?? 
  Toolkit tk = Toolkit.getDefaultToolkit();//此中toolkit 的作用是什么啊?? Dimension d=tk.getScreenSize();
  f.setLocation((d.width-f.getWidth())/2, (d.height-f.getHeight())/2);
  f.setVisible(true);
}
public MenuTest(){}
public MenuTest(String title){
super(title);
}
private JMenuBar menubar = new JMenuBar();
private JMenu fileMenu = new JMenu("File");
private JMenuItem newMenuItem = new JMenuItem("new");
private JMenuItem openMenuItem = new JMenuItem("open");
private JMenuItem saveMenuItem = new JMenuItem("save");
private JMenuItem saveAsMenuItem = new JMenuItem("save as...");
private JMenuItem exitMenuItem = new JMenuItem("exit");

private JMenu configMenu = new JMenu("config");
private JMenuItem backgroudMusicMenuItem=new JMenuItem("back music");
private JMenu backColorMenu = new JMenu("back color");
private JRadioButtonMenuItem redItem = new JRadioButtonMenuItem("red");
private JRadioButtonMenuItem yellowItem = new JRadioButtonMenuItem("yellow");
private JRadioButtonMenuItem blueItem = new JRadioButtonMenuItem("blue");
//以下这对大括号怎么回事,并不是在方法函数后面啊??起什么作用!!
{
this.setJMenuBar(menubar);
menubar.add(fileMenu);
menubar.add(configMenu);

fileMenu.add(newMenuItem);
fileMenu.add(openMenuItem);
fileMenu.add(saveMenuItem);
fileMenu.add(saveAsMenuItem);
fileMenu.addSeparator();
fileMenu.add(exitMenuItem);

configMenu.add(backgroudMusicMenuItem);
configMenu.add(backColorMenu);
backColorMenu.add(redItem);
backColorMenu.add(yellowItem);
backColorMenu.add(blueItem);
ButtonGroup colorGroup = new ButtonGroup();
colorGroup.add(redItem);
colorGroup.add(yellowItem);
colorGroup.add(blueItem);
}

}

解释越详细越好!!

------解决方案--------------------
传说中的swing?
------解决方案--------------------
Dimension就是表示width,height的一个对象。
获取默认工具包。

就是一个代码块,删了也可以。一般用static{}静态代码块来加载一些信息。

------解决方案--------------------
swing 啊
------解决方案--------------------
http://007ej.com/user.asp
http://happyran.zbpifa.com

------解决方案--------------------
setSize方法的参数是Dimension类型的, f.setSize(new Dimension(300,230));表示设置窗口的大小为300*230;

Dimension d=tk.getScreenSize();获得了你的电脑屏幕的大小 

f.setLocation((d.width-f.getWidth())/2, (d.height-f.getHeight())/2);实现了让窗口居于屏幕中央.