循环使用时,添加动作一个JButton [英] Adding action to a JButton when using a for loop

查看:261
本文介绍了循环使用时,添加动作一个JButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想动态添加按钮(Jbutton中),其中每次改变名称。我有这样做是为了循环,是不是真的有问题。但加入一个动作侦听或识别哪个按钮了pressed的时候,那就是当事情不工作那么好。

MyFrame.java

 进口的javax.swing *。
java.awt.event中导入*。
进口java.awt.GridLayout中;公共类MyFrame扩展JFrame中实现的ActionListener
{
    私人的JPanel面板;
    私有静态的JButton []按钮=新的JButton [18];
    //设置所有静态计算Jbutton将
    私有静态的JButton equalsBtn,addBtn,SUBBTN,multiBtn,divBtn,clearBtn,plusMinusBtn,decimalBtn;
    //设置所有静态数JBtuttons
    私有静态的JButton zeroBtn,oneBtn,twoBtn,threeBtn,fourBtn,fiveBtn,sixBtn,sevenBtn,eightBtn,nineBtn;
    私有静态的JTextField resultField;
    // numOne是数字的第一行连接的第二numSecond是第二行
    私有静态双numOne,numSecond,结果;
    私有静态双plusMinus;
    私有静态诠释添加点击= 0,subClick = 0,multiClick = 0,divClick = 0;
    私有静态诠释克利尔;    公共MyFrame(){
        //配置的JFrame
        超(玛珥Rekennen!);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        的setResizable(假);
        的setSize(230,370);
        setLocationRelativeTo(NULL);        // confugure在JPanel
        面板=新JPanel();
        panel.setSize(230,370);
        panel.setLayout(新的GridLayout(5,0));        //字符串数组丝毫所有的按钮名称
        串a_btnNames [] = {clearBtn,plusMinusBtn,divBtn,multiBtn,sevenBtn,eightBtn,nineBtn,SUBBTN,fourBtn,fiveBtn,sixBtn, addBtn,oneBtn,twoBtn,threeBtn,equalsBtn,zeroBtn,decimalBtn};
        //数组字符串白衣所有按键字符
        串a_btnCharts [] = {C,+/-,/,*,7,8,9, - ,4,5,6 ,+,1,2,3,=,0,}。        的for(int i = 0; I< buttons.length;我++)
        {
            //作出新的按钮名称
            按钮[i] =的新的JButton(a_btnNames [I]);
            //添加按钮面板
            panel.add(的新的JButton(a_btnCharts [I]));
            //System.out.println(buttons[i]);
        }        //添加面板时,他充满
        加(面板);
        调用setVisible(真);
    }    公共无效的actionPerformed(ActionEvent的五){
        // preSS按钮
        如果(e.getSource()== ......){
            的System.out.println(123);
        }
    }
}

Main.java

 公共类主要{
    公共静态无效的主要(字串[] ARG){
        MyFrame MF =新MyFrame();
    }
}


解决方案

有什么监听当按钮pressed。当您创建每个按钮,你必须给他们动作监听器。

此外,你应该添加您创建相同的按钮。

 按钮[i] =的新的JButton(+ a_btnNames [I]);
//按钮由[i]现在应添加到面板

新的for循环应该是...

 的for(int i = 0; I< buttons.length;我++){
    按钮[i] =的新的JButton(+ a_btnNames [I]); //创建按钮和放大器;添加到阵列
    按钮[I] .addActionListener(本); //添加一个动作监听当前按钮
    panel.add(按钮[I]); //添加相同的按钮到面板}

I'm trying to dynamically add buttons (JButtons), which changes names everytime. I doing it with a for loop and is not really problematic. but when adding an action listener or identifying which button got pressed, that is when things don't work so good.

MyFrame.java

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

public class MyFrame extends JFrame implements ActionListener 
{
    private JPanel panel;
    private static JButton[] buttons  = new JButton[18];
    // set all static calculate JButtons
    private static JButton equalsBtn, addBtn, subBtn, multiBtn, divBtn, clearBtn, plusMinusBtn, decimalBtn;
    // set all static number JBtuttons
    private static JButton zeroBtn, oneBtn, twoBtn, threeBtn, fourBtn, fiveBtn, sixBtn, sevenBtn, eightBtn, nineBtn;
    private static JTextField resultField;
    // numOne is the first row of figures en the second numSecond is the second row
    private static double numOne, numSecond, result;
    private static double plusMinus;
    private static int addClick = 0, subClick = 0, multiClick = 0, divClick = 0;
    private static int clearField;

    public MyFrame() {
        // configure the JFrame
        super("Rekennen maar!");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setSize(230, 370);
        setLocationRelativeTo(null);

        // confugure the JPanel
        panel = new JPanel();
        panel.setSize(230, 370);
        panel.setLayout(new GridLayout(5, 0));

        // array string whit all the button names
        String a_btnNames[] = {"clearBtn", "plusMinusBtn", "divBtn", "multiBtn", "sevenBtn", "eightBtn", "nineBtn", "subBtn", "fourBtn", "fiveBtn", "sixBtn",                               "addBtn", "oneBtn", "twoBtn", "threeBtn", "equalsBtn", "zeroBtn", "decimalBtn"};
        // array String whit all button characters
        String a_btnCharts[] = {"C", "+/-", "/", "*", "7", "8", "9", "-", "4", "5", "6", "+", "1", "2", "3", "=", "0", "."};

        for(int i = 0; i < buttons.length; i++)
        {
            // make new button name 
            buttons[i]  = new JButton(a_btnNames[i]);
            // add button to panel
            panel.add(new JButton(a_btnCharts[i]));
            //System.out.println(buttons[i]);
        }

        // add panel when he's filled 
        add(panel);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        // press the button
        if ( e.getSource() == ...... ) {
            System.out.println("123");  
        }   
    } 
}

Main.java

public class Main {
    public static void main(String[] arg) {
        MyFrame mf = new MyFrame();
    }
}

解决方案

There is nothing listening for when the buttons are pressed. When you create each button, you must give them action listeners.

Also, you should be adding the same button that you create.

buttons[i] = new JButton("" + a_btnNames[i]);
//buttons[i] should now be added to the panel

New for loop should be...

for(int i = 0; i < buttons.length; i++){
    buttons[i] = new JButton("" + a_btnNames[i]); //create button & add to array
    buttons[i].addActionListener(this); //add an action listener to the current button
    panel.add(buttons[i]); //add that same button to the panel

}

这篇关于循环使用时,添加动作一个JButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆