test.java

来自「点击菜单可弹出~子菜单」· Java 代码 · 共 88 行

JAVA
88
字号
import java.awt.Component;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.Insets;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;public class Test extends JFrame implements ActionListener{    public Test(){        setSize(200,400);        setLayout(null);        JPanel p = new JPanel();        p.setBounds(10,10,80,350);        add(p);        p.setLayout(new GridBagLayout());        Insets is = new Insets(1,1,1,1);        JButton b = new JButton("test 1");        p.add(b,new GridBagConstraints(0,0,1,1,1,0,GridBagConstraints.WEST,GridBagConstraints.HORIZONTAL,is,0,0));        b.addActionListener(this);        b = new JButton("sub test 11");        b.setVisible(false);        p.add(b,new GridBagConstraints(0,1,1,1,1,0,GridBagConstraints.WEST,GridBagConstraints.HORIZONTAL,is,0,0));        b = new JButton("sub test 12");        b.setVisible(false);        p.add(b,new GridBagConstraints(0,2,1,1,1,0,GridBagConstraints.WEST,GridBagConstraints.HORIZONTAL,is,0,0));        b = new JButton("sub test 13");        b.setVisible(false);        p.add(b,new GridBagConstraints(0,3,1,1,1,0,GridBagConstraints.WEST,GridBagConstraints.HORIZONTAL,is,0,0));        b = new JButton("test 2");        b.addActionListener(this);        p.add(b,new GridBagConstraints(0,4,1,1,1,0,GridBagConstraints.WEST,GridBagConstraints.HORIZONTAL,is,0,0));        b = new JButton("sub test 21");        p.add(b,new GridBagConstraints(0,5,1,1,1,0,GridBagConstraints.WEST,GridBagConstraints.HORIZONTAL,is,0,0));        b.setVisible(false);        b = new JButton("sub test 22");        p.add(b,new GridBagConstraints(0,6,1,1,1,0,GridBagConstraints.WEST,GridBagConstraints.HORIZONTAL,is,0,0));        b.setVisible(false);        b = new JButton("sub test 23");        b.setVisible(false);        p.add(b,new GridBagConstraints(0,7,1,1,1,0,GridBagConstraints.WEST,GridBagConstraints.HORIZONTAL,is,0,0));        b = new JButton("exit");        b.addActionListener(this);        p.add(b,new GridBagConstraints(0,8,1,1,1,0,GridBagConstraints.WEST,GridBagConstraints.HORIZONTAL,is,0,0));        JLabel lb = new JLabel();        p.add(lb,new GridBagConstraints(0,9,1,1,1,1,GridBagConstraints.WEST,GridBagConstraints.BOTH,is,0,0));    }    public void actionPerformed(ActionEvent e){        JButton b = (JButton)e.getSource();        if(b.getText().equals("test 1")){            Component[] objects = b.getParent().getComponents();            for(int i=0;i<objects.length;i++){                if( objects[i] instanceof JButton){                    JButton bt = (JButton)objects[i];                    if ( bt.getText().startsWith("sub test 1")){                        bt.setVisible(!bt.isVisible());                    }else if ( bt.getText().startsWith("sub test 2")){                        bt.setVisible(false);                    }                }            }        }else if(b.getText().equals("test 2")){            Component[] objects = b.getParent().getComponents();            for(int i=0;i<objects.length;i++){                if( objects[i] instanceof JButton){                    JButton bt = (JButton)objects[i];                    if ( bt.getText().startsWith("sub test 2")){                        bt.setVisible(!bt.isVisible());                    }else if ( bt.getText().startsWith("sub test 1")){                        bt.setVisible(false);                    }                }            }        }else if(b.getText().equals("exit")){            System.exit(0);        }    }    public static void main(String[] args){        Test t = new Test();        t.setVisible(true);    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?