怎么把JMenuBar加到JPanel上?

mengweilil 2009-03-25 12:39:49
rt

加到JFrame上没问题。
...全文
343 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
shuieryin 2009-03-25
  • 打赏
  • 举报
回复
没事, 呵呵
mengweilil 2009-03-25
  • 打赏
  • 举报
回复
哦,搞定,多谢多谢。马上结贴。

shuieryin 2009-03-25
  • 打赏
  • 举报
回复
我没加menuItem,改了一下加了的给你



import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;


public class Driver {

public static void main(String[] args) {
JMenuBar menua = new JMenuBar();
JMenu fileMenua = new JMenu("File");
JMenuItem exita = new JMenuItem("Exit", KeyEvent.VK_X);
fileMenua.add(exita);
menua.add(fileMenua);

JPanel p = new JPanel();
p.setPreferredSize(new Dimension(300, 200));

JPanel n = new JPanel();
n.setLayout(new BorderLayout());
n.setPreferredSize(new Dimension(300, 200));

JMenuBar menub = new JMenuBar();
JMenu fileMenub = new JMenu("File");
JMenuItem exitb = new JMenuItem("Exit", KeyEvent.VK_X);
fileMenub.add(exitb);
menub.add(fileMenub);

n.add(menub, BorderLayout.NORTH);

JFrame f = new JFrame("MenuBarTest");
f.add(menua, BorderLayout.NORTH);
f.add(p);
f.add(n, BorderLayout.SOUTH);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}

}


mengweilil 2009-03-25
  • 打赏
  • 举报
回复
比如JFrame上增加JTabbedPanel,然后在这些Panel上加上菜单,虽然少见,不过也有用。

不过运行的结果还是怪怪的啊,不知道原因。





shuieryin 2009-03-25
  • 打赏
  • 举报
回复
为什么要这么做? 一般很少这么用吧



import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;


public class Driver {

public static void main(String[] args) {
JMenuBar menua = new JMenuBar();
JMenu fileMenua = new JMenu("File");
menua.add(fileMenua);

JPanel p = new JPanel();
p.setPreferredSize(new Dimension(300, 200));

JPanel n = new JPanel();
n.setLayout(new BorderLayout());
n.setPreferredSize(new Dimension(300, 200));

JMenuBar menub = new JMenuBar();
JMenu fileMenub = new JMenu("File");
menub.add(fileMenub);

n.add(menub, BorderLayout.NORTH);

JFrame f = new JFrame("MenuBarTest");
f.add(menua, BorderLayout.NORTH);
f.add(p);
f.add(n, BorderLayout.SOUTH);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}

}


mengweilil 2009-03-25
  • 打赏
  • 举报
回复
不是

JPanel jp

JMenubar

这个用jp.add

然后显示有问题。

horizonlyhw 2009-03-25
  • 打赏
  • 举报
回复

Container cp=getContentPane();
cp.add();


?
可以下下package jdk; import javax.swing.*; import java.awt.*; import java.awt.event.*; class ChessPanel extends JPanel{ private int space=20; private int grids=30; private int radius=space/2; private int[][]chesses=new int[grids+1][grids+1]; private int currColor=1; private JMenuBar chessMenuBar=new JMenuBar(); private JMenu optMenu=new JMenu("操作"); private JMenuItem startMenuItem=new JMenuItem("开始"); private JMenuItem exitMenuItem=new JMenuItem("退出"); private ActionListener startHandler=new ActionListener(){ public void actionPerformed(ActionEvent e){ clearGrids(); currColor=1; repaint(); } }; private ActionListener exitHandler=new ActionListener(){ public void actionPerformed(ActionEvent e){ System.exit(0); } }; private MouseListener playChessHandler=new MouseAdapter(){ public void mouseClicked(MouseEvent e){ int x=e.getX(); int y=e.getY(); if(x<=grids*space&&x>=0&&y<=grids*space&&y>=0) if(chesses[round(x)][round(y)]==0){ chesses[round(x)][round(y)]=currColor; currColor=currColor==1?2:1; repaint(); } } }; public int round(float a){ float f=a/space; return Math.round(f); } public ChessPanel(int space,int grids){ this.space=space; this.grids=grids; this.radius=space/2; setBackground(Color.YELLOW); setSize(space*grids,space*grids); startMenuItem.addActionListener(startHandler); exitMenuItem.addActionListener(exitHandler); addMouseListener(playChessHandler); chessMenuBar.add(optMenu); optMenu.add(startMenuItem); optMenu.add(exitMenuItem); } public JMenuBar getMenuBar(){ return chessMenuBar; } private void drawChess(Graphics g,int x,int y,int color){ g.setColor(color==1?Color.WHITE:Color.BLACK); g.fillOval(x*space-radius, y*space-radius, radius*2, radius*2); } public void drawGrids(Graphics g){ g.setColor(Color.DARK_GRAY); for(int i=0;i<=grids;i++) { g.drawLine(0, i*space, grids*space, i*space); g.drawLine(i*space, 0, i*space,grids*space); } } private void clearGrids(){ for(int i=0;i<=grids;i++) for(int j=0;j<=grids;j++) chesses[i][j]=0; } public void paintComponent(Graphics g){ super.paintComponent(g); drawGrids(g); for(int i=0;i<=grids;i++) for(int j=0;j<=grids;j++) if(chesses[i][j]!=0) drawChess(g,i,j,chesses[i][j]); } } public class ChessPlayer extends JFrame{ private ChessPanel chessPanel=new ChessPanel(20,30); public ChessPlayer(String title){ super(title); Container contentPane=getContentPane(); contentPane.add(chessPanel); setJMenuBar(chessPanel.getMenuBar()); setSize(600,600); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[]args){ new ChessPlayer("五子棋"); } }
/MainFrame.java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MainFrame extends JFrame implements ActionListener{ private Container cont; private JPanel panelMenu; private JPanel panelToolBar; private JPanel panelCompent; private JButton btnLeft; private JButton btnCenter; private JButton btnRight; private JButton btnBorder; private JButton btnGrid; private JLabel labHit; private JMenuBar menuBar; private JMenu menuOperation; private JMenu menuHelp; private JMenuItem mItemFlowLeft; private JMenuItem mItemFlowCenter; private JMenuItem mItemFlowRight; private JMenuItem mItemBorder; private JMenuItem mItemGrid; private JToolBar toolBar; private JButton tBtnLeft; private JButton tBtnCenter; private JButton tBtnRight; private JButton tBtnBorder; private JButton tBtnGrid; public MainFrame(){ this.setTitle("第一个界面"); this.setBounds(300, 300, 600, 450); this.setDefaultCloseOperation(this.EXIT_ON_CLOSE); cont = this.getContentPane(); // cont.setLayout(new GridLayout(3, 9)); cont.setLayout(null); initPanel(); initMenu(); initToolBar(); initCompent(); addMenu(); addCompent(); // addBorderCompent(); this.setVisible(true); } public void initPanel(){ panelMenu = new JPanel(); panelMenu.setLayout(new FlowLayout(FlowLayout.LEFT)); panelMenu.setBounds(0, 0, 600, 30); panelToolBar = new JPanel(); panelToolBar.setLayout(new FlowLayout(FlowLayout.LEFT)); panelToolBar.setBounds(0, 30, 600, 50); panelCompent = new JPanel(); panelCompent.setLayout(new GridLayout(3, 2)); panelCompent.setBounds(0, 80, 600, 350); cont.add(panelMenu); cont.add(panelToolBar); cont.add(panelCompent); } public void initMenu(){ menuBar = new JMenuBar(); menuOperation = new JMenu("布局操作"); menuHelp = new JMenu("帮助"); mItemFlowLeft = new JMenuItem("流式左布局"); mItemFlowLeft.addActionListener(this); mItemFlowCenter = new JMenuItem("流式居中"); mItemFlowCenter.addActionListener(this); mItemFlowRight = new JMenuItem("流式右布局"); mItemFlowRight.addActionListener(this); mItemBorder = new JMenuItem("边界布局"); mItemBorder.addActionListener(this); mItemGrid = new JMenuItem("网格布局"); mItemGrid.addActionListener(this); } public void initToolBar(){ toolBar = new JToolBar(); tBtnLeft = new JButton(new ImageIcon("COPY.jpg")); tBtnLeft.addActionListener(this); tBtnLeft.setToolTipText("流式左布局"); tBtnCenter = new JButton(new ImageIcon("HELP.jpg")); tBtnCenter.addActionListener(this); tBtnCenter.setToolTipText("流式居中"); tBtnRight = new JButton(new ImageIcon("find.jpg")); tBtnRight.addActionListener(this); tBtnRight.setToolTipText("流式右布局"); tBtnBorder = new JButton(new ImageIcon("CUT.jpg")); tBtnBorder.addActionListener(this); tBtnBorder.setToolTipText("边界布局"); tBtnGrid = new JButton(new ImageIcon("OPEN.jpg")); tBtnGrid.addActionListener(this); tBtnGrid.setToolTipText("网格布局"); toolBar.add(tBtnLeft); toolBar.add(tBtnCenter); toolBar.add(tBtnRight); toolBar.add(tBtnBorder); toolBar.add(tBtnGrid); panelToolBar.add(toolBar); } public void initCompent(){ btnLeft = new JButton("LEFT"); btnLeft.addActionListener(this); btnRight = new JButton("RIGHT"); btnRight.addActionListener(this); btnCenter = new JButton("CENTER"); btnCenter.addActionListener(this); btnBorder = new JButton("BORDER"); btnBorder.addActionListener(this); btnGrid = new JButton("GRID"); btnGrid.addActionListener(this); labHit = new JLabel("提示"); } public void addMenu(){ menuOperation.add(mItemFlowLeft); menuOperation.add(mItemFlowCenter); menuOperation.add(mItemFlowRight); menuOperation.add(mItemBorder); menuOperation.add(mItemGrid); menuBar.add(menuOperation); menuBar.add(menuHelp); panelMenu.add(menuBar); } public void addCompent(){ panelCompent.add(btnLeft); panelCompent.add(btnCenter); panelCompent.add(btnRight); panelCompent.add(btnBorder); panelCompent.add(btnGrid); panelCompent.add(labHit); } public void addBorderCompent(){ panelCompent.add(btnLeft, "West"); panelCompent.add(btnCenter, "Center"); panelCompent.add(btnRight, "East"); panelCompent.add(btnBorder, "North"); panelCompent.add(btnGrid, "South"); } public void actionPerformed(ActionEvent aEvt){ if((btnLeft == aEvt.getSource()) ||(mItemFlowLeft == aEvt.getSource()) ||(tBtnLeft == aEvt.getSource())){ panelCompent.setLayout(new FlowLayout(FlowLayout.LEFT)); labHit.setText("流式布局管理器左靠齐"); } else { if((btnRight == aEvt.getSource()) ||(mItemFlowRight == aEvt.getSource()) ||(tBtnRight == aEvt.getSource())){ panelCompent.setLayout(new FlowLayout(FlowLayout.RIGHT)); labHit.setText("流式布局管理器右靠齐"); } else { if((btnCenter == aEvt.getSource()) ||(mItemFlowCenter == aEvt.getSource()) ||(tBtnCenter == aEvt.getSource())){ panelCompent.setLayout(new FlowLayout(FlowLayout.CENTER)); labHit.setText("流式布局管理器中靠齐"); } else { if((btnBorder == aEvt.getSource()) ||(mItemBorder == aEvt.getSource()) ||(tBtnBorder == aEvt.getSource())){ panelCompent.setLayout(new BorderLayout()); addBorderCompent(); labHit.setText("边界布局管理器"); } else{ if((btnGrid == aEvt.getSource()) ||(mItemGrid == aEvt.getSource()) ||(tBtnGrid == aEvt.getSource()) ){ panelCompent.setLayout(new GridLayout(3, 2)); // addBorderCompent(); labHit.setText("网格布局管理器"); } } } } } } } //LoginFrame.java import javax.swing.*; import java.awt.*; import java.awt.event.*; class LoginFrame extends JFrame implements ActionListener{ private Container cont; private JLabel labUserName; private JLabel labPassword; private JTextField txtUserName; private JTextField txtPassword; private JButton btnOK; private JButton btnCancel; public LoginFrame(){ this.setTitle("登录界面"); this.setSize(300, 250); this.setLocation(300, 300); this.setDefaultCloseOperation(this.EXIT_ON_CLOSE); initCont(); initCompent(); addCompent(); this.setVisible(true); } public void initCont(){ cont = this.getContentPane(); cont.setLayout(null); } public void initCompent(){ labUserName = new JLabel("用户名:"); labPassword = new JLabel("密 码:"); txtUserName = new JTextField(); txtPassword = new JPasswordField(); btnOK = new JButton("登录"); btnCancel = new JButton("重置"); } public void addCompent(){ cont.add(labUserName); labUserName.setBounds(40,40,80,30); cont.add(labPassword); labPassword.setBounds(40,90,80,30); cont.add(txtUserName); txtUserName.setBounds(120,40,120,30); cont.add(txtPassword); txtPassword.setBounds(120,90,120,30); cont.add(btnOK); btnOK.addActionListener(this); btnOK.setBounds(60,170,80,30); cont.add(btnCancel); btnCancel.addActionListener(this); btnCancel.setBounds(160,170,80,30); } public void actionPerformed(ActionEvent aEvt){ if(aEvt.getSource() == btnOK){ if((txtUserName.getText().equals("denghong")) &&(txtPassword.getText().equals("123"))){ new MainFrame(); this.dispose(); } } else{ if(aEvt.getSource() == btnCancel){ labUserName.setText(""); labPassword.setText(""); } } } } //Test.java public class Test{ public static void main(String[] args){ new LoginFrame(); } }

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧