gpt4 book ai didi

java - 我怎样才能到达处理程序内的框架标题

转载 作者:行者123 更新时间:2023-12-02 08:19:50 25 4
gpt4 key购买 nike

我无法在 Handler 中访问我的标题。

我的代码示例:

public Frame extends JFrame {

public Frame() {
super("Frame 1");
.
.
.

我的 GUI 中需要有一个按钮,每当您单击它时,它都需要将标题从第 1 帧更改为第 2 帧,反之亦然。

ButtonHandler handler = new ButtonHandler();
.
.
.
}
class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
JButton but = (JButton) event.getSource();
if (??.getTitle(equals("Frame 1"))) {
setTitle("Frame 2");
} else {
setTitle("Frame 1");
}

所以每次我单击按钮时,它所做的都是 else 部分。我无法在 if 中使用 getTitle :/

最佳答案

首先应该读取

if (??.getTitle().equals("Frame 1"))

否则,您可以在 ButtonHandler 实例上调用 equals 并进行测试,看看该实例是否等于字符串“Frame 1”。 .

然后,我将一个构造函数添加到 ButtonHandler 中,该构造函数采用框架实例并将其存储在内部。我的建议总共:

class ButtonHandler implements ActionListener {
private JFrame frame;
public ButtonHandler(JFrame frame) {
this.frame = frame;
}

public void actionPerformed(ActionEvent event) {
JButton but = (JButton) event.getSource();
if (frame.getTitle().equals("Frame 1")) {
setTitle("Frame 2");
} else {
setTitle("Frame 1");
}
}
}

关于java - 我怎样才能到达处理程序内的框架标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5687979/

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