package mm
import java.io.*
import java.awt.*
import javax.swing.*
public class HelloWorld {
public static void main(String[] args){
JFrame w=new JFrame("SKH")
Container con=w.getContentPane()
//Color
con.setBackground(Color.pink)
//Location
w.setBounds(60, 100, 388, 308)
//可见性
w.setVisible(true)
//单击右上角容器的行为
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
//创建菜单项
JMenuBar menubar=new JMenuBar()
JMenu menu,submenu
JMenuItem a,b
menu=new JMenu("菜单")
submenu=new JMenu("子菜单")
a=new JMenuItem("菜单项1")
b=new JMenuItem("菜单项2")
menu.add(a)
//添加分割符;
menu.addSeparator()
menu.add(b)
menu.addSeparator()
menu.add(submenu)
submenu.add(new JMenuItem("QWQ"))
menubar.add(menu)
w.setJMenuBar(menubar)
}
}
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)
}
}
}
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)
}
}
不知道你要什么事件代码,我写了个比较简单的你看适合不。