本文目錄一覽:
JAVA遊戲菜單
java設置遊戲菜單可以很樸素 ,也可以比較華麗,簡單的寫了兩個參考效果
分析菜單導航到遊戲的過程, 我們可以在同一個容器里實現, 也可以在不同的窗口裡實現.
我們要根據具體 需求分析是切換窗口還是切換容器;
樸素版本 使用了的不同窗口的切換來實現 參考的代碼如下
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//這個代表掃雷窗口
class SL extends JFrame {
public SL() {
getContentPane().setBackground(Color.BLUE);
setTitle(“掃雷”);
setSize(MenuFrame.W, MenuFrame.H);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
}
//這個代表圍棋窗口
class WQ extends JFrame {
public WQ() {
getContentPane().setBackground(Color.ORANGE);
setTitle(“圍棋”);
setSize(MenuFrame.W, MenuFrame.H);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
}
//這個代表菜單選擇窗口
public class MenuFrame extends JFrame implements ActionListener {
public static final int W = 300;
public static final int H = 200;
JButton jb1, jb2;
public MenuFrame() {
JPanel jp = new JPanel();
BoxLayout box = new BoxLayout(jp, BoxLayout.Y_AXIS);//垂直方向的布局
jp.setLayout(box);
jb1 = new JButton(“益智掃雷”);
jb1.addActionListener(this);
jb2 = new JButton(“圍棋春秋”);
jb2.addActionListener(this);
JButton jb3=new JButton(“再續前緣”);
JButton jb4=new JButton(“退隱江湖”);
JButton jb5=new JButton(“幫助文檔”);
jp.add(jb1);
jp.add(jb2);
jp.add(jb3);
jp.add(jb4);
jp.add(jb5);
add(jp);
setLayout(new FlowLayout());
setTitle(“java Game Center”);
setSize(W, H);
setLocationRelativeTo(null);//窗口居中
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() – {
new MenuFrame().setVisible(true);//啟動菜單窗口
});
}
@Override
public void actionPerformed(ActionEvent e) {
JButton jb = (JButton) e.getSource();
if (jb == jb1) {
//隱藏關閉菜單窗口
this.setVisible(false);
this.dispose();
//打開掃雷窗口
new SL().setVisible(true);
} else if (jb == jb2) {
this.setVisible(false);
this.dispose();
new WQ().setVisible(true);
}
}
}
如何給Java窗體添加菜單欄
5步。
1、創建菜單(如文件)
2、創建菜單項(如新建、打開、保存)
3、將菜單項添加到菜單(如將新建、打開、保存菜單項添加到文件菜單)
4、創建菜單欄,將菜單添加到菜單欄
5、設置窗口的菜單欄
核心代碼:
//創建窗口
JFrame w=new JFrame(“包含菜單欄的窗口”);
//創建文件菜單
JMenu file=new JMenu(“文件”);
//創建新建菜單項
JMenuItem n=new JMenuItem(“新建”);
//創建打開菜單項
JMenuItem o=new JMenuItem(“打開”);
//創建保存菜單項
JMenuItem s=new JMenuItem(“保存”);
//將新建菜單項添加到文件菜單
file.add(n);
//將打開菜單項添加到文件菜單
file.add(o);
//將保存菜單項添加到文件菜單
file.add(s);
//創建菜單欄
JMenuBar br=new JMenuBar();
//將文件菜單添加到菜單欄
br.add(file);
//為窗口設置菜單欄
w.setJMenuBar(br);
Java中菜單組件的類是什麼?
JMenuBar菜單欄
JMenu 菜單
將菜單add到菜單欄,最後將菜單欄添加到面板上(setJMenuBar(菜單欄對象);)
例子:
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class MyMenu extends JFrame {
private static final long serialVersionUID = -367679212385637764L;
/**
* 創建一個新的實例 MyMenu.
*/
public MyMenu() {
// TODO Auto-generated constructor stub
/**
* 菜單名稱
*/
String[] buttons = new String[]{“提交”, “保存”, “清除”, “退出”};
String[] colors = new String[] {“紅色”, “黃色”, “默認”};
/**
* 菜單欄
*/
JMenuBar menubar = new JMenuBar();
JMenu jmManager = new JMenu(“管理”);
for(String text : buttons) {
JMenuItem item = new JMenuItem(text);
jmManager.add(item);
}
JMenu jmColor = new JMenu(“顏色”);
for(String text : colors) {
JMenuItem item = new JMenuItem(text);
jmColor.add(item);
}
/**
* 添加菜單
*/
menubar.add(jmManager);
menubar.add(jmColor);
this.setJMenuBar(menubar);
/**
* 面板基本設置
*/
this.setTitle(“Menu Demo”);
this.setSize(200, 200);
this.setResizable(false);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new MyMenu();
}
}
運行結果如下:
java的菜單代碼怎麼寫?
import java.awt.*;
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(“藍色”);
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==”藍色”)m.jp.setBackground(Color.blue);
}
}
不知道你要什麼事件代碼,我寫了個比較簡單的你看適合不。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/258500.html