首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ActionListener java swing

ActionListener java swing
EN

Stack Overflow用户
提问于 2015-04-26 01:07:33
回答 2查看 78关注 0票数 0

我一直在尝试使用swing广告创建一个窗口,我必须把按钮放在右边,这就是为什么我使用boxlayout,但我找不到一种方法来在我已经有的按钮上使用ActionListener。这就是我正在做的项目:

代码语言:javascript
复制
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();
    }
}
EN

回答 2

Stack Overflow用户

发布于 2015-04-26 01:14:22

您不应该直接将按钮添加到面板中,而应该实例化它们,然后向它们添加一个ActionListener或您想要对它们执行的任何其他操作。示例:

代码语言:javascript
复制
JButton ajouterButton = new JButton("ajouter");
ajouterButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent event) {
        // code goes here
    }
});

然后,您可以将按钮添加到数组中:

代码语言:javascript
复制
boxes[1].add(ajouterButton);

然后对你所有的按钮做同样的事情。

票数 1
EN

Stack Overflow用户

发布于 2015-04-26 01:15:01

您要查找的按钮实例存储在boxes1中,因此您可以简单地执行boxes[1].addActionListener(...);

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29868211

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档