panel挡住菜单栏
如下程序片断,菜单被panel挡住,显示不出来,去掉panel就没问题了,会是什么问题呢? import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class SaoLei extends JFrame implements ActionListener {
JButton [][] Mine=new JButton[8][8];
JButton Start=new JButton();
Container Con=this.getContentPane();
Panel P1;
JMenuBar mb;
JMenu m1,m2;
JMenuItem MI1,MI2,MI3,MI4,MI5;
public SaoLei() {
Start=new JButton("开始");
Start.setSize(200,200);
mb=new JMenuBar();
m1=new JMenu("文件");
m2=new JMenu("选项");
MI1= new JMenuItem("开始");
MI2= new JMenuItem("结束");
MI3= new JMenuItem("退出");
MI4= new JMenuItem("历史记录");
MI5= new JMenuItem("帮助");
P1=new Panel();
P1.setLayout(new GridLayout(8,8));
P1.set
for(int i=0;i<8;i++)
for(int j=0;j<8;j++)
{
Mine[i][j]=new JButton();
P1.add(Mine[i][j]);
}
Con.setLayout(new BorderLayout());
mb.add(m1);
mb.add(m2);
m1.add(MI1);
m1.add(MI2);
m1.addSeparator();
m1.add(MI3);
m2.add(MI4);
m2.add(MI5);
Con.add(Start,BorderLayout.NORTH);
Con.add(P1,BorderLayout.CENTER);
this.setJMenuBar(mb);
pack();
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
}
public static void main(String [] args)
{
new SaoLei();
}
}
----------------解决方案--------------------------------------------------------
这个问题是awt和swing混用产生的结果。
楼主可以把菜单设为重量级组件,或者把panel改为swing的。
程序代码:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
public class SaoLei extends JFrame implements ActionListener {
// JPopupMenu.setDefaultLightWeightPopupEnabled(false);//将菜单设置为重量级组件
JButton[][] Mine = new JButton[8][8];
JButton Start = new JButton();
Container Con = this.getContentPane();
JPanel P1;
JMenuBar mb;
JMenu m1, m2;
JMenuItem MI1, MI2, MI3, MI4, MI5;
public SaoLei() {
Start = new JButton("开始");
Start.setSize(200, 200);
mb = new JMenuBar();
m1 = new JMenu("文件");
m2 = new JMenu("选项");
MI1 = new JMenuItem("开始");
MI2 = new JMenuItem("结束");
MI3 = new JMenuItem("退出");
MI4 = new JMenuItem("历史记录");
MI5 = new JMenuItem("帮助");
P1 = new JPanel();
P1.setLayout(new GridLayout(8, 8));
for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++) {
Mine[i][j] = new JButton();
P1.add(Mine[i][j]);
}
Con.setLayout(new BorderLayout());
mb.add(m1);
mb.add(m2);
m1.add(MI1);
m1.add(MI2);
m1.addSeparator();
m1.add(MI3);
m2.add(MI4);
m2.add(MI5);
Con.add(Start, BorderLayout.NORTH);
Con.add(P1, BorderLayout.CENTER);
this.setJMenuBar(mb);
pack();
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
}
public static void main(String[] args) {
new SaoLei();
}
}
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
public class SaoLei extends JFrame implements ActionListener {
// JPopupMenu.setDefaultLightWeightPopupEnabled(false);//将菜单设置为重量级组件
JButton[][] Mine = new JButton[8][8];
JButton Start = new JButton();
Container Con = this.getContentPane();
JPanel P1;
JMenuBar mb;
JMenu m1, m2;
JMenuItem MI1, MI2, MI3, MI4, MI5;
public SaoLei() {
Start = new JButton("开始");
Start.setSize(200, 200);
mb = new JMenuBar();
m1 = new JMenu("文件");
m2 = new JMenu("选项");
MI1 = new JMenuItem("开始");
MI2 = new JMenuItem("结束");
MI3 = new JMenuItem("退出");
MI4 = new JMenuItem("历史记录");
MI5 = new JMenuItem("帮助");
P1 = new JPanel();
P1.setLayout(new GridLayout(8, 8));
for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++) {
Mine[i][j] = new JButton();
P1.add(Mine[i][j]);
}
Con.setLayout(new BorderLayout());
mb.add(m1);
mb.add(m2);
m1.add(MI1);
m1.add(MI2);
m1.addSeparator();
m1.add(MI3);
m2.add(MI4);
m2.add(MI5);
Con.add(Start, BorderLayout.NORTH);
Con.add(P1, BorderLayout.CENTER);
this.setJMenuBar(mb);
pack();
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
}
public static void main(String[] args) {
new SaoLei();
}
}
----------------解决方案--------------------------------------------------------
受教了
----------------解决方案--------------------------------------------------------
回复 2楼 linjx0123
谢啦、、、 ----------------解决方案--------------------------------------------------------
不要混用重量级组建和轻量级组建,在swing之后请尽量使用轻量级的swing组建。
awt组建每个组建都会有一个系统等同体,消耗资源比较大。
[ 本帖最后由 baifenghan 于 2010-5-22 02:13 编辑 ]
----------------解决方案--------------------------------------------------------