JMenu的问题

wsj 2003-04-01 05:34:21
如何才能让JMenu在JFrame中靠左,纵向显示:

-----------------------------------
|File| |
|Help| |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
-----------------------------------


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Frame2 extends JFrame {
private JMenuBar jMenuBar1 = new JMenuBar();
private JMenu jMenu1 = new JMenu();
private JMenu jMenu2 = new JMenu();
private JMenuItem jMenuItem1 = new JMenuItem();
private JMenuItem jMenuItem2 = new JMenuItem();
private BorderLayout borderLayout1 = new BorderLayout();

public Frame2() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Frame2 frame2 = new Frame2();
frame2.setSize(640,480);
frame2.show();
}
private void jbInit() throws Exception {
this.getContentPane().setLayout(borderLayout1);
//jMenuBar1.setLayout(null);
jMenu1.setText("File");
jMenuItem1.setText("Exit");
jMenu1.setBounds(0,0,20,20);
jMenuBar1.add(jMenu1);
jMenuItem1.setBounds(0,0,20,20);

jMenu2.setText("Help");
jMenuItem2.setText("About");
jMenu2.setBounds(0,20,20,20);
jMenuItem2.setBounds(0,20,20,20);
jMenu2.add(jMenuItem2);
jMenuBar1.add(jMenu2);
this.getContentPane().add(jMenuBar1, BorderLayout.WEST);
}
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
}
...全文
26 点赞 收藏 7
写回复
7 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
cocolovevienna 2003-04-02
从楼上兄弟那儿找到一个基本相似的一个代码,你可以参考一下。



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/** @author ges */

public class MenuLayoutDemo extends JFrame {
protected JMenuBar menuBar;

public MenuLayoutDemo() {
super("MenuLayoutDemo");
menuBar = new JMenuBar();
menuBar.setLayout(new BoxLayout(menuBar, BoxLayout.Y_AXIS));
addNewMenu("Menu 1");
addNewMenu("Menu 2");
addNewMenu("Menu 3");
getContentPane().add(menuBar, BorderLayout.WEST);

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}

public void addNewMenu(String title) {
JMenu m = (JMenu)menuBar.add(new HorizontalMenu(title));
m.add("Menu item");
m.add("Menu item");
m.add("Menu item");
JMenu m1 = (JMenu)m.add(new HorizontalMenu("Submenu"));
m1.add("Submenu item");
m1.add("Submenu item");
}

public static void main(String args[]) {
MenuLayoutDemo f = new MenuLayoutDemo();
f.pack();
f.setVisible(true);
}

class HorizontalMenu extends JMenu {
HorizontalMenu(String label) {
super(label);
JPopupMenu pm = getPopupMenu();
pm.setLayout(new BoxLayout(pm, BoxLayout.X_AXIS));
setMinimumSize(getPreferredSize());
}

public void setPopupMenuVisible(boolean b) {
boolean isVisible = isPopupMenuVisible();
if (b != isVisible) {
if ((b==true) && isShowing()) {
// Set location of popupMenu (pulldown or pullright)
// Perhaps this should be dictated by L&F
int x = 0;
int y = 0;
Container parent = getParent();
if (parent instanceof JPopupMenu) {
x = 0;
y = getHeight();
} else {
x = getWidth();
y = 0;
}
getPopupMenu().show(this, x, y);
} else {
getPopupMenu().setVisible(false);
}
}
}
}
}

回复
cocolovevienna 2003-04-02
对啊,好像微软msdn上的那种靠左菜单吧!
回复
apan_bj 2003-04-02
关注。
回复
meetjava 2003-04-02
JMenuBar是可以设置Layout,设成GridLayout或者BoxLayout.Y_AXIS

应该可以

参考:
http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html
回复
carfi 2003-04-01
实在不知道怎么搞法,帮你顶一下吧
回复
ringoqin 2003-04-01
关注!!!
回复
cooker8 2003-04-01
我没做过
回复
发帖
Java SE
创建于2007-09-28

6.1w+

社区成员

Java 2 Standard Edition
申请成为版主
帖子事件
创建了帖子
2003-04-01 05:34
社区公告
暂无公告