简体   繁体   English

ActionListener Java摇摆

[英]ActionListener java swing

i have being trying to create a window using swing ad i have to put the buttons on the right side that's why i used boxlayout but i can't find a way to use ActionListener on the button that i have. 我一直在尝试使用摇摆广告创建一个窗口,我不得不将按钮放在右侧,这就是为什么我使用boxlayout的原因,但是我找不到在按钮上使用ActionListener的方法。 that's the program i am working on: 那就是我正在研究的程序:

public class Fenetre2 extends JFrame {

private JSplitPane splitPan=null;

    public Fenetre2 (){
        JPanel pan = new JPanel ();

        // CARACTERISTIQUE FENETRE 
        this.setTitle("Gestion Employe");
        this.setSize(800, 400);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pan.setBackground(Color.white);
        this.setContentPane(pan);
        // ADD BUTTON  
        Container c = getContentPane();
        c.setLayout( new BorderLayout( 30, 30 ) );
        Box boxes[] = new Box[ 4 ];
        boxes[ 0 ] = Box.createHorizontalBox();
        boxes[ 1 ] = Box.createVerticalBox();
        boxes[ 2 ] = Box.createHorizontalBox();
        boxes[ 3 ] = Box.createVerticalBox();
        // create strut and add buttons to boxes[ 1 ]
        boxes[ 1 ].add( new JButton( "ajouter" ) );
        boxes[ 1 ].add( new JButton( "suprimer" ) );
        boxes[ 1 ].add( new JButton( "afficher" ) );
        c.add( boxes[ 1 ], BorderLayout.EAST );
        //TREE
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("STRUCTURE EMPLOYE");
        //create the child nodes
        DefaultMutableTreeNode PDGNode = new DefaultMutableTreeNode("PDG");
        DefaultMutableTreeNode departement1Node = new DefaultMutableTreeNode("departement 1");
        departement1Node.add(new DefaultMutableTreeNode("CHEF DEPARTEMENT"));
        departement1Node.add(new DefaultMutableTreeNode("EMPLOYEE1"));
        departement1Node.add(new DefaultMutableTreeNode("EMPLOYEE2"));
        departement1Node.add(new DefaultMutableTreeNode("EMPLOYEE3"));


        //add the child nodes to the root node
        root.add(PDGNode);
        PDGNode.add(departement1Node);
        JTree tree = new JTree(root);
        this.add(tree);
        JScrollPane scroll=new JScrollPane(tree);
        splitPan=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,scroll,new JLabel("aaaaa"));
        splitPan.setSize(this.getMaximumSize());
        add(splitPan);

        this.setVisible(true);          
    }
    public static void main (String args []){

        Fenetre2 fen = new Fenetre2();
    }
}

You should not add the buttons directly into the panel but instead instantiate them and then add an ActionListener to them or whatever else you want to do with them. 您不应该将按钮直接添加到面板中,而应实例化它们,然后向其添加ActionListener或您要对其进行任何其他操作。 Example: 例:

JButton ajouterButton = new JButton("ajouter");
ajouterButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent event) {
        // code goes here
    }
});

Then you can add the button to your array: 然后可以将按钮添加到数组中:

boxes[1].add(ajouterButton);

Then do the same thing to all your buttons. 然后对所有按钮执行相同的操作。

The button instance you are looking for is stored in boxes[1], so you can simply do boxes[1].addActionListener(...); 您要查找的按钮实例存储在box [1]中,因此您只需执行boxes[1].addActionListener(...); .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM