java菜單和組件代碼 java菜單快捷鍵

java的菜單代碼怎么寫?

import java.awt.*;

企業(yè)建站必須是能夠以充分展現(xiàn)企業(yè)形象為主要目的,是企業(yè)文化與產(chǎn)品對外擴(kuò)展宣傳的重要窗口,一個合格的網(wǎng)站不僅僅能為公司帶來巨大的互聯(lián)網(wǎng)上的收集和信息發(fā)布平臺,成都創(chuàng)新互聯(lián)公司面向各種領(lǐng)域:成都廣告設(shè)計(jì)網(wǎng)站設(shè)計(jì)、成都全網(wǎng)營銷解決方案、網(wǎng)站設(shè)計(jì)等建站排名服務(wù)。


import java.awt.event.*;

import javax.swing.*;

public class MyMenu extends JFrame{

JMenuBar jmbar=new JMenuBar();

JMenu jmenu=new JMenu("顏色");

JMenuItem jmt1=new JMenuItem("紅色"),

jmt2=new JMenuItem("黃色"),

jmt3=new JMenuItem("藍(lán)色");

JPanel jp=new JPanel();

MyMenu(){

setTitle("菜單測試");

setSize(400,300);

setJMenuBar(jmbar);

jmbar.add(jmenu);

jmenu.add(jmt1);

jmenu.add(jmt2);

jmenu.add(jmt3);

add(jp);

jmt1.addActionListener(new MenuAction(this));

jmt2.addActionListener(new MenuAction(this));

jmt3.addActionListener(new MenuAction(this));

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

}

public static void main(String[] args) {

new MyMenu();

}

}

class MenuAction implements ActionListener{

MyMenu m;

MenuAction(MyMenu m){

this.m=m;

}

public void actionPerformed(ActionEvent e){

String color=e.getActionCommand();

if(color=="紅色")m.jp.setBackground(Color.red);

else if(color=="黃色")m.jp.setBackground(Color.yellow);

else if(color=="藍(lán)色")m.jp.setBackground(Color.blue);

}

}

不知道你要什么事件代碼,我寫了個比較簡單的你看適合不。

java添加菜單條和按鈕

試試這樣行不?

public class Mazelp {/*extends JFrame*/ //implements ActionListener{

private static final int wid = 10;

private static final int hei = 10;

JFrame jf;

JButton jb1,jb2;

JButton jb[];

JPanel p1,p2;

private Stack stack = new Stack();//Stack 類表示后進(jìn)先出(LIFO)的對象堆棧。

MenuBar menu;

Menu file;

MenuItem closeMenu;

public Mazelp() {

jf=new JFrame("迷宮");//申請內(nèi)存空間設(shè)置標(biāo)題

jf.setBounds(300,240,500,500); //調(diào)整迷宮出現(xiàn)的位置(300,240)及大小(500,500)

jf.setResizable(false);//窗體不可拉伸

menu = new MenuBar();//設(shè)置菜單條

file = new Menu("文件");//設(shè)置菜單欄

closeMenu = new MenuItem("關(guān)閉");//設(shè)置菜單項(xiàng)

//closeMenu.addActionListener(this);//添加監(jiān)聽對菜單項(xiàng)

p1=new JPanel();

//jf.add(menu);

jf.setMenuBar(menu);

menu.add(file);//將菜單欄添加到菜單條上

file.add(closeMenu);//將菜單項(xiàng)添加到菜單欄

jf.getContentPane().add(p1);

p1.setLayout(new GridLayout(10,10)); //p1用網(wǎng)格布局,10行10列

jb=new JButton[100];//作為迷宮的墻和路

for(int i=0;ijb.length;i++){

jb[i]=new JButton(Integer.toString(i));//創(chuàng)建按鍵的名字,Integer型的名字為i的字符串

if((i=0i=9)||(i=90i=99)||i%10==0||i%10==9||i==13||i==17||i==23||i==27||i==35||i==36||i==42||i==43||i==44||i==54||i==62||i==66||i==72||i==73||i==74||i==76||i==77||i==81){

jb[i].setBackground(Color.red);//將墻涂色

}

else {

jb[i].setBackground(Color.yellow);

}

jb[i].setSize(10,10);

p1.add(jb[i]);

jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE); //聲明點(diǎn)“X”圖標(biāo)后結(jié)束窗體所在的應(yīng)用程序

jf.setVisible(true); //表明以上創(chuàng)建的所有窗體、按鍵等組件都是可見

}

}

java新手,創(chuàng)建一個菜單欄,但是連窗口都不顯示,程序運(yùn)行,什么都沒有,就終止。。代碼如下

public??aa(){//構(gòu)造方法不要加void

JFrame?f=new?JFrame("菜單組件");

JMenuBar?bar?=new?JMenuBar();

???JMenuItem?j1=new?JMenuItem("Open");

???JMenuItem?j2=new?JMenuItem("Save");

???JMenuItem?j3=new?JMenuItem("Save?as?Template");

???JMenu?m1?=?new?JMenu("File");

???mypanel?mp=new?mypanel();

???f.add(bar);

???f.setJMenuBar(bar);//this改成f

???bar.add(m1);

???m1.add(j1);

???m1.add(j2);

???m1.add(j3);

???f.setSize(800,600);

???f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

???f.pack();

???f.setVisible(true);

???

}

分享文章:java菜單和組件代碼 java菜單快捷鍵
地址分享:http://muchs.cn/article6/ddcigig.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、云服務(wù)器網(wǎng)站收錄、、手機(jī)網(wǎng)站建設(shè)、域名注冊

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管