gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:40:26 24 4
gpt4 key购买 nike

我的测试项目如下:

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);
}
}

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

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";

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

@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
break;

default:
break;
}
}
}

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

最佳答案

一种解决方案是将 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);
}
}

鼠标监听器:

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;
}
}
}

关于java - 如何将参数从适配器类传递到另一个类中的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38501844/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com