62,614
社区成员
发帖
与我相关
我的任务
分享
public void setJMenuBar(JMenuBar menubar) {
getRootPane().setMenuBar(menubar);
}
@Deprecated
public void setMenuBar(JMenuBar menu){
if(menuBar != null && menuBar.getParent() == layeredPane)
layeredPane.remove(menuBar);
menuBar = menu;
if(menuBar != null)
layeredPane.add(menuBar, JLayeredPane.FRAME_CONTENT_LAYER);
}
protected void addImpl(Component comp, Object constraints, int index)
{
if(isRootPaneCheckingEnabled()) {
getContentPane().add(comp, constraints, index);
}
else {
super.addImpl(comp, constraints, index);
}
}
public Container getContentPane() {
return getRootPane().getContentPane();
}
public Container getContentPane() { return contentPane; }
import javax.swing.*;
import java.awt.*;
public class TestMenuBar {
JFrame frame;
JMenuBar menuBar,menuBar2;
Container contentPane;
JPanel pan;
public TestMenuBar(){
frame=new JFrame();
menuBar=new JMenuBar();
String str[]={"文件","操作","帮助"};
for(int i=0;i<str.length;i++){
menuBar.add(new JMenu(str[i]));
}
String str2[]={"退出"};
menuBar2=new JMenuBar();
for(int i=0;i<str2.length;i++){
menuBar2.add(new JMenu(str2[i]));
}
//frame.setJMenuBar(menuBar);
contentPane=frame.getContentPane();
contentPane.setLayout(new GridBagLayout());
pan=new JPanel(){
public void paint(Graphics g){
g.setColor(Color.white);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
}
};
pan.setPreferredSize(new Dimension(400,400));
add(contentPane,pan,0,2,1,1);
add(contentPane,menuBar,0,0,1,0);
add(contentPane,menuBar2,0,1,1,0);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void add(Container main,Container child,int gridx,int gridy,double weightx,double weighty){
GridBagConstraints c=new GridBagConstraints();
c.gridx=gridx;
c.gridy=gridy;
c.weightx=weightx;
c.weighty=weighty;
c.fill=GridBagConstraints.BOTH;
main.add(child,c);
}
public static void main(String args[]){
new TestMenuBar();
}
}