1.JAVA中的菜单类(原创 Copy to clipboard Posted by: floatPosted on: 2005-12-17 22:12JAVA中的菜单类在Javax.swing包中,一共有三个菜单子类:JMenuBar,JMenu和JMenuItem类。首先来看一下他们之间的关系,然后通过代码,来学习他们的使用。这是一个他们的关系图,大家可以看到,一切都是基于在JMenuBar之上的。JMenuBar就像一个机箱提供给JMenu一个允许放置的接口,然后JMenu就像主板一样,能插JMenuItem,而JMenuItem就是我们的显卡和CPU。有两点需要大家注意:在电脑中,机箱不是必须的,可在这里,JMenuBar是必须的。还有一点是电脑中不能有多个主板,可这里能有多个JMenu对象,我的比喻可能不是很恰当,但事实就是这么回事,只需要领会意思即可!我们来看下怎么声明JMenuBar类的对象:class FrameTest extends Frame { super("The tittle"); //传递标题 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //使窗口可以正常关闭 this.setVisible(true); //可视为真 this.setSize(240,240); //窗体大小}class PanelTest extends FrameTest{JPanel pt //声明一个JPanel的容器; PanelTest() { pt=new JPanel(); //实例化pt pt.setBackground(Color.cyan); //窗体的颜色为青色 this.getContentPane().add(pt); //将pt添加到Frame中去 }}class menuTest extends PanelTest{ JMenuBar jb; //声明一个JMenuBar型的容器 menuTest() { //构造函数 jb=new JMenuBar() //实例化这个容器}当然这时没什么意义,只是实例化一个空的容器,也就是说,这时jb拥有了主板的位置,可是并没有装主板。所以,即使调用了,也是空的,下面我们来看下怎么装“主板”:class menuTest extends PanelTest{ JMenuBar jb; //声明一个JMenuBar型的容器 JMenu jm1,jm2,jm3; 声明三个JMenu型的容器 menuTest() { //构造函数 jb=new JMenuBar() //实例化这个容器jm1=new JMenu("标题1"); //实例化这三个容器 jm2=new JMenu("标题2"); jm3=new JMenu("标题3"); jb.add(jm1); //将这三个容器,加到jb中,这时就能看到窗体上有了菜单,但点击//没有反映 jb.add(jm2); jb.add(jm3); } 下面我们来看下,将具体选项也添加进去,这样我们的菜单栏就可以使用了 menuTest() { //构造函数 jb=new JMenuBar() //实例化这个容器jm1=new JMenu("标题1"); //实例化这三个容器 jm2=new JMenu("标题2"); jm3=new JMenu("标题3"); jb.add(jm1); //将这三个容器,加到jb中,这时就能看到窗体上有了菜单,但点击//没有反映 jb.add(jm2); jb.add(jm3); ji1=new JMenuItem("选项1"); ji2=new JMenuItem("选项2"); ji3=new JMenuItem("选项3"); jm1.add(ji1); jm2.add(ji2); jm3.add(ji3); } 这样一个完整的菜单栏就构建好了,运行结果如下:呵呵,这样就实现了JAVA的菜单功能。谢谢大家观看。QQ:79990675完整代码:class FrameTest extends Frame { super("The tittle"); //传递标题 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //使窗口可以正常关闭 this.setVisible(true); //可视为真 this.setSize(240,240); //窗体大小}class PanelTest extends FrameTest{JPanel pt //声明一个JPanel的容器; PanelTest() { pt=new JPanel(); //实例化pt pt.setBackground(Color.cyan); //窗体的颜色为青色 this.getContentPane().add(pt); //将pt添加到Frame中去 }}class menuTest extends PanelTest{ JMenuBar jb; //声明一个JMenuBar型的容器 JMenu jm1,jm2,jm3; JMenuItem ji1,ji2,ji3; menuTest() { //构造函数 jb=new JMenuBar() //实例化这个容器 jm1=new JMenu("标题1"); jm2=new JMenu("标题2"); jm3=new JMenu("标题3"); jb.add(jm1); jb.add(jm2); jb.add(jm3); ji1=new JMenuItem("选项1"); ji2=new JMenuItem("选项2"); ji3=new JMenuItem("选项3"); jm1.add(ji1); jm2.add(ji2); jm3.add(ji3);}原创帖,有图片,发不上来,想看的到我BLOG:http://bfjava.blogbus.com/logs/2005/12/1716564.html 2.Re:JAVA中的菜单类(原创 [Re: float] Copy to clipboard Posted by: whyPosted on: 2005-12-17 23:40You may try to add the images using the [img ] tag.However, don't use BMP format! They are huge, save them as GIF or JPEG.Here's the reduction of the two images by simply saving them as GIF in the Paint program:1134829029.bmp 398KB --> 6KB1134829125.bmp 128KB --> 4KB 3.Re:JAVA中的菜单类(原创 [Re: float] Copy to clipboard Posted by: JcatPosted on: 2005-12-18 18:47最近忙着期末考试,好久没来了,小家伙有长进嘛。不过你的代码有很多问题哦(以后贴代码之前,起码保证别人能javac呀),我试着改了改,未果~~索性帮你补充一段代码,你看看是不是你所要表达的意思。PS.注意你书写的规范性,比如类名首字母大写等……import java.awt.BorderLayout;import javax.swing.*;public class MenuTest extends JFrame { BorderLayout borderLayout1 = new BorderLayout(); JMenuBar jMenuBar1 = new JMenuBar(); JMenu jMenu1 = new JMenu(); JMenuItem jMenuItem1 = new JMenuItem(); JMenu jMenu2 = new JMenu(); JMenuItem jMenuItem2 = new JMenuItem(); JMenuItem jMenuItem3 = new JMenuItem(); JMenuItem jMenuItem4 = new JMenuItem(); JLabel jLabel1 = new JLabel(); public MenuTest() { try { jbInit(); } catch (Exception exception) { exception.printStackTrace(); } } private void jbInit() throws Exception { getContentPane().setLayout(borderLayout1); this.setJMenuBar(jMenuBar1); jMenu1.setText("主板1"); jMenu2.setText("主板2"); jMenuItem1.setText("芯片1"); jMenuItem3.setText("硬盘1"); jMenuItem2.setText("芯片2"); jMenuItem4.setText("硬盘2"); jLabel1.setText("上面那栏就是你所谓的机箱"); jMenuBar1.add(jMenu1); jMenuBar1.add(jMenu2); jMenu1.add(jMenuItem3); jMenu1.add(jMenuItem1); jMenu2.add(jMenuItem4); jMenu2.add(jMenuItem2); this.getContentPane().add(jLabel1, java.awt.BorderLayout.CENTER); } public static void main(String[] args){ MenuTest inst= new MenuTest(); inst.pack(); inst.show(); }}效果图

评论