如何将参数从适配器类传递到另一个类中的方法

时间:2016-07-21 10:32:24

标签: java

我的测试项目如下:

  public class Skills {
private String c;
private String cPlusPlus;
private String java;
private String web;

public String getC() {
    return c;
}

public void setC(String c) {
    this.c = c;
}

public String getcPlusPlus() {
    return cPlusPlus;
}

public void setcPlusPlus(String cPlusPlus) {
    this.cPlusPlus = cPlusPlus;
}

public String getJava() {
    return java;
}

public void setJava(String java) {
    this.java = java;
}

public String getWeb() {
    return web;
}

public void setWeb(String web) {
    this.web = web;
}
}

在另一个文件中创建 MyMouseListener 类:

import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Test {
    public static void main(String[] args) {
        EventQueue.invokeLater(()->{
            JFrame frame = new MyFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        });
    }
}

class MyFrame extends JFrame {

    private JButton imgBtn = new JButton("Open");           // Create a button named “Open”.

    public MyFrame() {
        setPreferredSize(new Dimension(960, 540));
        setSize(getPreferredSize());

        initBtnListener();
//      deliverParamTest(s);
    }
    // Set listener for imgBtn to respond to a click event.
    public void initBtnListener() {
        imgBtn.addMouseListener(new MyMouseListener(imgBtn));
    }

    // A method to receive a parameter from imgBtn's adapter and print it out.
    public void passParamTest(String s) {
        System.out.println(s);
    }
}

现在问题是如何将 testString 作为MyFrame类中 passParamTest()方法的参数传递给测试文件?

2 个答案:

答案 0 :(得分:1)

一种解决方案是将JFrame实例传递给您的侦听器:

import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Test {
    public static void main(String[] args) {
        EventQueue.invokeLater(()->{
            JFrame frame = new MyFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        });
    }
}

class MyFrame extends JFrame {

    private JButton imgBtn = new JButton("Open");           

    public MyFrame() {
        setPreferredSize(new Dimension(960, 540));
        setSize(getPreferredSize());

        initBtnListener();
    }    

    // Set listener for imgBtn to respond to a click event.
    public void initBtnListener() {
        imgBtn.addMouseListener(new MyMouseListener(imgBtn, this));
    }

    // A method to receive a parameter from imgBtn's adapter and print it out.
    public void passParamTest(String s) {
        System.out.println(s);
    }
}

的MouseListener:

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;

public class MyMouseListener extends MouseAdapter {

    private JButton btn;
    private String testString = "A String to be delivered to another class as a parameter";
    private MyFrame myFrame;

    public MyMouseListener(JButton btn, MyFrame myFrame) {
        this.btn = btn;
        this.myFrame = myFrame;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        super.mouseClicked(e);
        switch (btn.getText()) {
        // Deal with the pass logic here.
        case "Open":
            myFrame.passParamTest(testString);
            // Pass the String testString to Test class
            break;

        default:
            break;
        } 
    }
}

答案 1 :(得分:0)

您可以将JFrame的实例作为构造函数参数传递,并从clicked事件中调用特定方法。

public class MyMouseListener extends MouseAdapter {

private JButton btn;
private JFrame myFrame;
private String testString = "A String to be delivered to another class as a parameter";

public MyMouseListener(JButton btn, JFrame myFrame) {
    this.btn = btn;
    this.myFrame = myFrame; 
}

@Override
public void mouseClicked(MouseEvent e) {
    super.mouseClicked(e);
    switch (btn.getText()) {
     // Deal with the pass logic here.
    case "Open":
        // Pass the String testString to Test class
        myFrame.passParamTest(this.testString);
        break;

    default:
        break;
    }
}

}