通过另一个类添加对按钮执行的操作

时间:2016-05-03 21:54:37

标签: java class jbutton actionlistener class-method

我正在尝试学习方法和课程 我创建了一个小工具,其中类Main运行它,类SWing创建框架,类Verb创建一个按钮添加到Swing以及执行动作,我的问题是,我怎么能做到动作执行真的通过动词类。 假设我想从textfield获取文本并将其与textfiled1连接并在textfield2上显示答案。 这是我的代码

1-我的主要班级

package Abo;

public class Main {

    public static void main (String[]args){
            Swing runFrame = new Swing(); // creating a variable for class Swing 
            runFrame.Run(); // to run the Swing
    }
}

2-我的秋千

package Abo;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextField;

public class Swing extends JFrame {
    public JTextField textField;
    public JTextField textField_1;
    public JTextField textField_2;

    public Swing() { 
        // creating the frame
            getContentPane().setLayout(new GridLayout(2,2,5,5));

            textField = new JTextField();
            getContentPane().add(textField);
            textField.setColumns(10);

            textField_1 = new JTextField();
            getContentPane().add(textField_1);
            textField_1.setColumns(10);

            textField_2 = new JTextField();
            getContentPane().add(textField_2);
            textField_2.setColumns(10);
            textField_2.setEditable(false);

            // adding the btn from another class
            Verb addBTN = new Verb();
            getContentPane().add(addBTN.BTN());


    }

    public void Run(){
        // setting the frame
            Swing frame = new Swing();
            frame.setVisible(true);
            frame.setSize(300,400);
            frame.setLocationRelativeTo(null);
            frame.setResizable(false);
            frame.setDefaultCloseOperation(EXIT_ON_CLOSE);     
    }
}

3-动词类

package Abo;

import javax.swing.JButton;

public class Verb extends JButton {


// creating the btn constructor
    public JButton BTN(){
        JButton btn1 = new JButton("First Button");
        return btn1;
    }
}   

提前谢谢

2 个答案:

答案 0 :(得分:0)

在您的动词类中,只需将其添加到btn1

即可
 btn1.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e)
                {
                    //Execute when button is pressed



                }
            }); 

答案 1 :(得分:0)

我注意到Verb正在扩展JButton,但您选择编写一个在其中返回JButton的方法。

如果您希望Verb成为始终称为JButton的{​​{1}},那么我会修改"First Button"的构造函数。

这样做的一种方法是:

Verb

现在,由于您要延长package Abo; import javax.swing.JButton; public class Verb extends JButton { public Verb() { super("First Button"); //super calls the constructor of the parent of this class, in this case that is JButton } } ,每当您制作JButton时,它也会是Verb,文字为JButton

现在,您可以直接将"First Button"添加到ActionListener

通过做:

Verb

但是,为了访问actionlistener中的文本字段,您需要将它们设为最终字段。

Verb addBTN = new Verb(); addBTN.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField_2.setText(textField.getText() + textField_1.getText()); } });

此外,除非您计划向final textField = new JTextField(); // do this for all text fields添加比默认构造函数更多的功能,否则我只想使用Verb而不是自己创建类。