
正文
java三级菜单代码 java 查询三级菜单表
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java编写鼠标悬停在二级菜单,点击三级菜单?
import java.awt.*;
import javax.swing.*;
public class ErjiMenu extends JFrame
{
JPanel jp=new JPanel();
JMenuBar menubar=new JMenuBar();
JMenu m1=new JMenu("菜单");
JMenu m2=new JMenu("菜单");
JMenuItem item1=new JMenuItem("菜单项");
JMenuItem item2=new JMenuItem("菜单项1");
JMenuItem item3=new JMenuItem("菜单项2");
public ErjiMenu()
{
jp.setLayout(new BorderLayout());
m2.add(item2); //m2添加菜单项。
m2.add(item3);
m1.add(item1);
m1.add(m2); //m1把m2添加进去java三级菜单代码,作为二级菜单.
menubar.add(m1);
jp.add(menubar,BorderLayout.NORTH);
this.add(jp);
this.setBounds(100,100,380,200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
new ErjiMenu();
}
}
不用JPopupMenu,更简单。
相关问答
Q1: 如何用Java实现树形结构啊?
package tree;
import java.util.LinkedList;
import java.util.List;
/**
* 功能:把一个数组java三级菜单代码的值存入二叉树中java三级菜单代码,然后进行3种方式的遍历
*
* 参考资料0:数据结构(C语言版)严蔚敏
*
* 参考资料1:
*
* 参考资料2:
*
* @author ocaicai@yeah.net @date: 2011-5-17
*
*/
public class BinTreeTraverse2 {
private int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
private static ListNode nodeList = null;
/**
* 内部类:节点
*
* @author ocaicai@yeah.net @date: 2011-5-17
*
*/
private static class Node {
Node leftChild;
Node rightChild;
int data;
Node(int newData) {
leftChild = null;
rightChild = null;
data = newData;
}
}
public void createBinTree() {
nodeList = new LinkedListNode();
// 将一个数组的值依次转换为Node节点
for (int nodeIndex = 0; nodeIndex array.length; nodeIndex++) {
nodeList.add(new Node(array[nodeIndex]));
}
// 对前lastParentIndex-1个父节点按照父节点与孩子节点的数字关系建立二叉树
for (int parentIndex = 0; parentIndex array.length / 2 - 1; parentIndex++) {
// 左孩子
nodeList.get(parentIndex).leftChild = nodeList
.get(parentIndex * 2 + 1);
// 右孩子
nodeList.get(parentIndex).rightChild = nodeList
.get(parentIndex * 2 + 2);
}
// 最后一个父节点:因为最后一个父节点可能没有右孩子java三级菜单代码,所以单独拿出来处理
int lastParentIndex = array.length / 2 - 1;
// 左孩子
nodeList.get(lastParentIndex).leftChild = nodeList
.get(lastParentIndex * 2 + 1);
// 右孩子,如果数组的长度为奇数才建立右孩子
if (array.length % 2 == 1) {
nodeList.get(lastParentIndex).rightChild = nodeList
.get(lastParentIndex * 2 + 2);
}
}
/**
* 先序遍历
*
* 这三种不同的遍历结构都是一样的java三级菜单代码,只是先后顺序不一样而已
*
* @param node
* 遍历的节点
*/
public static void preOrderTraverse(Node node) {
if (node == null)
return;
System.out.print(node.data + " ");
preOrderTraverse(node.leftChild);
preOrderTraverse(node.rightChild);
}
/**
* 中序遍历
*
* 这三种不同的遍历结构都是一样的java三级菜单代码,只是先后顺序不一样而已
*
* @param node
* 遍历的节点
*/
public static void inOrderTraverse(Node node) {
if (node == null)
return;
inOrderTraverse(node.leftChild);
System.out.print(node.data + " ");
inOrderTraverse(node.rightChild);
}
/**
* 后序遍历
*
* 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已
*
* @param node
* 遍历的节点
*/
public static void postOrderTraverse(Node node) {
if (node == null)
return;
postOrderTraverse(node.leftChild);
postOrderTraverse(node.rightChild);
System.out.print(node.data + " ");
}
public static void main(String[] args) {
BinTreeTraverse2 binTree = new BinTreeTraverse2();
binTree.createBinTree();
// nodeList中第0个索引处的值即为根节点
Node root = nodeList.get(0);
System.out.println("先序遍历:");
preOrderTraverse(root);
System.out.println();
System.out.println("中序遍历:");
inOrderTraverse(root);
System.out.println();
System.out.println("后序遍历:");
postOrderTraverse(root);
}
}
Q2: java 如何用用数组设置一个窗体的三级菜单?
这不简单吗,直接循环加上去就可以了。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class List extends JFrame{
public List(){
initialization();
setTitle("三级菜单");
setBounds(300,300,400,300);
setLayout(null);
Container c=getContentPane();
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public void initialization(){
JMenu jmenuTwo=null;
String[] menubar={"编辑","帮助"};
String[][] submenu={{"复制","粘贴","","历史记录"},{"关于三级菜单"}};
String[] threemenu={"撤消","恢复"};
JMenuBar jmenubar=new JMenuBar();//创建菜单栏
for(int i=0;imenubar.length;i++){
JMenu jmenu=new JMenu(menubar[i]);
jmenubar.add(jmenu);
for(int j=0;jsubmenu[i].length;j++){
if("".equals(submenu[i][j])){
jmenu.addSeparator();//添加一条横线
}else{
jmenuTwo=new JMenu(submenu[i][j]);
jmenu.add(jmenuTwo);
if(j2i==0){
for(int k=0;kthreemenu.length;k++){
JMenuItem menu3=new JMenuItem(threemenu[k]);
jmenuTwo.add(menu3);
}
}
}
}
this.setJMenuBar(jmenubar);//JFrame加菜单栏时用setJmenuBar(),不用add /
}
}
public static void main(String[] args){
new List();
}
}
Q3: java的菜单代码怎么写?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyMenu extends JFrame{
JMenuBar jmbar=new JMenuBar();
JMenu jmenu=new JMenu("颜色");
JMenuItem jmt1=new JMenuItem("红色"),
jmt2=new JMenuItem("黄色"),
jmt3=new JMenuItem("蓝色");
JPanel jp=new JPanel();
MyMenu(){
setTitle("菜单测试");
setSize(400,300);
setJMenuBar(jmbar);
jmbar.add(jmenu);
jmenu.add(jmt1);
jmenu.add(jmt2);
jmenu.add(jmt3);
add(jp);
jmt1.addActionListener(new MenuAction(this));
jmt2.addActionListener(new MenuAction(this));
jmt3.addActionListener(new MenuAction(this));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new MyMenu();
}
}
class MenuAction implements ActionListener{
MyMenu m;
MenuAction(MyMenu m){
this.m=m;
}
public void actionPerformed(ActionEvent e){
String color=e.getActionCommand();
if(color=="红色")m.jp.setBackground(Color.red);
else if(color=="黄色")m.jp.setBackground(Color.yellow);
else if(color=="蓝色")m.jp.setBackground(Color.blue);
}
}
不知道你要什么事件代码,我写了个比较简单的你看适合不。
Q4: java 如何生成三级树形菜单?
是要返回一个json串吧。mybytis里设置好要返回自定义的结果集合。就直接可以
java三级菜单代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java 查询三级菜单表、java三级菜单代码的信息别忘了在本站进行查找喔。








