java,自己写的菜单栏出不来
importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener...
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;
public class Chess extends JFrame implements ActionListener{
Toolkit tk = Toolkit.getDefaultToolkit();
//菜单条
private JMenuBar jMenuBar;
//菜单项
private JMenu jMenu;
public void ChessWindow() {
setTitle("中国象棋");
setSize(800, 600);
setVisible(true);
setResizable(false);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}});
//实例化菜单条
jMenuBar = new JMenuBar();
//获得一个菜单项
jMenu = createFileMenu();
//将菜单项加入菜单条
jMenuBar.add(jMenu);
//菜单条设为不透明
jMenuBar.setOpaque(true);
//在容器中加入菜单条
setJMenuBar(jMenuBar);
}
private JMenu createFileMenu() {
//实例化菜单项
JMenu chessMenu = new JMenu("Chess");
//实例化菜单选项
JMenuItem dGame = new JMenuItem("draw");
//加入 ActionListener
dGame.addActionListener(this);
//将选项加入菜单
chessMenu.add(dGame);
JMenuItem exit = new JMenuItem("Exit");
exit.addActionListener(this);
chessMenu.add(exit);
return chessMenu;
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("Exit")){
System.exit(0);
}
if(e.getActionCommand().equals("draw")) {
this.createImage(10, 10);
}
}
public static void main(String[] org) {
Chess c = new Chess();
c.ChessWindow();
}
} 展开
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;
public class Chess extends JFrame implements ActionListener{
Toolkit tk = Toolkit.getDefaultToolkit();
//菜单条
private JMenuBar jMenuBar;
//菜单项
private JMenu jMenu;
public void ChessWindow() {
setTitle("中国象棋");
setSize(800, 600);
setVisible(true);
setResizable(false);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}});
//实例化菜单条
jMenuBar = new JMenuBar();
//获得一个菜单项
jMenu = createFileMenu();
//将菜单项加入菜单条
jMenuBar.add(jMenu);
//菜单条设为不透明
jMenuBar.setOpaque(true);
//在容器中加入菜单条
setJMenuBar(jMenuBar);
}
private JMenu createFileMenu() {
//实例化菜单项
JMenu chessMenu = new JMenu("Chess");
//实例化菜单选项
JMenuItem dGame = new JMenuItem("draw");
//加入 ActionListener
dGame.addActionListener(this);
//将选项加入菜单
chessMenu.add(dGame);
JMenuItem exit = new JMenuItem("Exit");
exit.addActionListener(this);
chessMenu.add(exit);
return chessMenu;
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("Exit")){
System.exit(0);
}
if(e.getActionCommand().equals("draw")) {
this.createImage(10, 10);
}
}
public static void main(String[] org) {
Chess c = new Chess();
c.ChessWindow();
}
} 展开
展开全部
稍微改写一下你的写法,能看到JMENU但是会报错,不习惯你这种写法(NEW个CHESS再调用C.)import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;
public class Chess extends JFrame implements ActionListener{
Toolkit tk = Toolkit.getDefaultToolkit();
//菜单条
private JMenuBar jMenuBar;
//菜单项
private JMenu jMenu;
public void ChessWindow() {
setTitle("中国象棋");
setSize(800, 600);
setVisible(true);
setResizable(false);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}});
//实例化菜单条
jMenuBar = new JMenuBar();
//获得一个菜单项
jMenu = createFileMenu();
//将菜单项加入菜单条
jMenuBar.add(jMenu);
//菜单条设为不透明
jMenuBar.setOpaque(true);
//在容器中加入菜单条
setJMenuBar(jMenuBar);
Container c = ContainergetPane();
c.add(jMenu);
}
private Container ContainergetPane() {
// TODO Auto-generated method stub
return null;
}
private JMenu createFileMenu() {
//实例化菜单项
JMenu chessMenu = new JMenu("Chess");
//实例化菜单选项
JMenuItem dGame = new JMenuItem("draw");
//加入 ActionListener
dGame.addActionListener(this);
//将选项加入菜单
chessMenu.add(dGame);
JMenuItem exit = new JMenuItem("Exit");
exit.addActionListener(this);
chessMenu.add(exit);
return chessMenu;
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("Exit")){
System.exit(0);
}
if(e.getActionCommand().equals("draw")) {
this.createImage(10, 10);
}
}
public static void main(String[] org) {
Chess ch = new Chess();
ch.ChessWindow();
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;
public class Chess extends JFrame implements ActionListener{
Toolkit tk = Toolkit.getDefaultToolkit();
//菜单条
private JMenuBar jMenuBar;
//菜单项
private JMenu jMenu;
public void ChessWindow() {
setTitle("中国象棋");
setSize(800, 600);
setVisible(true);
setResizable(false);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}});
//实例化菜单条
jMenuBar = new JMenuBar();
//获得一个菜单项
jMenu = createFileMenu();
//将菜单项加入菜单条
jMenuBar.add(jMenu);
//菜单条设为不透明
jMenuBar.setOpaque(true);
//在容器中加入菜单条
setJMenuBar(jMenuBar);
Container c = ContainergetPane();
c.add(jMenu);
}
private Container ContainergetPane() {
// TODO Auto-generated method stub
return null;
}
private JMenu createFileMenu() {
//实例化菜单项
JMenu chessMenu = new JMenu("Chess");
//实例化菜单选项
JMenuItem dGame = new JMenuItem("draw");
//加入 ActionListener
dGame.addActionListener(this);
//将选项加入菜单
chessMenu.add(dGame);
JMenuItem exit = new JMenuItem("Exit");
exit.addActionListener(this);
chessMenu.add(exit);
return chessMenu;
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("Exit")){
System.exit(0);
}
if(e.getActionCommand().equals("draw")) {
this.createImage(10, 10);
}
}
public static void main(String[] org) {
Chess ch = new Chess();
ch.ChessWindow();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询