为什么菜单栏还有按钮只显示一个
importjava.awt.BorderLayout;importjava.awt.FlowLayout;importjava.awt.event.*;importja...
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JMenuItem;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
public class ChessBoard {
private JFrame jframe;
private JPanel toolbar;
private JButton jbStart;
private JButton jbBack;
private JButton jbExit;
private JMenuBar menuBar;
private JMenu sysMenu;
private JMenuItem jmStart;
private JMenuItem jmBack;
private JMenuItem jmExit;
public ChessBoard(){
jframe=new JFrame("五子棋单机版");
menuBar=new JMenuBar();
jframe.setJMenuBar(menuBar);
sysMenu=new JMenu("系统");
jmBack=new JMenuItem("退出");
jmStart=new JMenuItem("重新开始");
jmExit=new JMenuItem("悔棋");
sysMenu.add(jmBack);
sysMenu.add(jmStart);
sysMenu.add(jmExit);
//MyItemListener lis=new MyItemListener();
jmBack.addActionListener(null);
jmStart.addActionListener(null);
jmExit.addActionListener(null);
menuBar.add(sysMenu);
toolbar=new JPanel();
jbStart=new JButton("重新开始");
jbBack=new JButton("退出");
jbExit=new JButton("悔棋");
toolbar.add(jbStart);
toolbar.add(jmBack);
toolbar.add(jmExit);
toolbar.setLayout(new FlowLayout(FlowLayout.CENTER));
jbStart.addActionListener(null);
jbBack.addActionListener(null);
jbExit.addActionListener(null);
jframe.add(toolbar,BorderLayout.SOUTH);
jframe.setSize(300, 600);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.pack();
jframe.setVisible(true);
}
public static void main(String[] args){
ChessBoard c=new ChessBoard();
}
} 展开
import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JMenuItem;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
public class ChessBoard {
private JFrame jframe;
private JPanel toolbar;
private JButton jbStart;
private JButton jbBack;
private JButton jbExit;
private JMenuBar menuBar;
private JMenu sysMenu;
private JMenuItem jmStart;
private JMenuItem jmBack;
private JMenuItem jmExit;
public ChessBoard(){
jframe=new JFrame("五子棋单机版");
menuBar=new JMenuBar();
jframe.setJMenuBar(menuBar);
sysMenu=new JMenu("系统");
jmBack=new JMenuItem("退出");
jmStart=new JMenuItem("重新开始");
jmExit=new JMenuItem("悔棋");
sysMenu.add(jmBack);
sysMenu.add(jmStart);
sysMenu.add(jmExit);
//MyItemListener lis=new MyItemListener();
jmBack.addActionListener(null);
jmStart.addActionListener(null);
jmExit.addActionListener(null);
menuBar.add(sysMenu);
toolbar=new JPanel();
jbStart=new JButton("重新开始");
jbBack=new JButton("退出");
jbExit=new JButton("悔棋");
toolbar.add(jbStart);
toolbar.add(jmBack);
toolbar.add(jmExit);
toolbar.setLayout(new FlowLayout(FlowLayout.CENTER));
jbStart.addActionListener(null);
jbBack.addActionListener(null);
jbExit.addActionListener(null);
jframe.add(toolbar,BorderLayout.SOUTH);
jframe.setSize(300, 600);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.pack();
jframe.setVisible(true);
}
public static void main(String[] args){
ChessBoard c=new ChessBoard();
}
} 展开
1个回答
展开全部
主要是因为下面这两句
//toolbar.add(jmBack);
//toolbar.add(jmExit);
在java API中有这样一句话,你可以好好理解一下,说的就是这种情况
Menu items, like other components, can be in at most one container. If you try to add a menu item to a second menu, the menu item will be removed from the first menu before being added to the second.
//toolbar.add(jmBack);
//toolbar.add(jmExit);
在java API中有这样一句话,你可以好好理解一下,说的就是这种情况
Menu items, like other components, can be in at most one container. If you try to add a menu item to a second menu, the menu item will be removed from the first menu before being added to the second.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询