gpt4 book ai didi

java - 从另一个方法更改对象值

转载 作者:行者123 更新时间:2023-11-30 02:49:36 25 4
gpt4 key购买 nike

以下问题:

我想将一个值更改为另一个值,但是使用不同的方法。完整代码(根据要求)如下:

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

public class main {

public static void about() {
JDialog aboutWindow = new JDialog();
aboutWindow.setTitle("About kingfisher a0.0.3");
aboutWindow.setSize(300, 600);
aboutWindow.setModal(true);
JMenuBar menubar = new JMenuBar();
JMenu control = new JMenu("Control");
JMenuItem quit = new JMenuItem("quit");

quit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
aboutWindow.setVisible(false);
}
});

control.add(quit);
menubar.add(control);
aboutWindow.add(menubar);
JLabel name = new JLabel("kingfisher");
aboutWindow.add(name);
}

public static void main(String args[]) {
//Defined window dimensions
JFrame controlpanel = new JFrame();
controlpanel.setTitle("kingfisher a0.0.3");
controlpanel.setSize(500, 400);
controlpanel.add(new JLabel("The angels have the blue box"));
JMenuBar menubar = new JMenuBar();
//defining menu groups
JMenu windows = new JMenu("Windows");
JCheckBoxMenuItem chat = new JCheckBoxMenuItem("Chat");
JCheckBoxMenuItem filetransfer = new JCheckBoxMenuItem("Filetransfer");
JCheckBoxMenuItem settings = new JCheckBoxMenuItem("Settings");
windows.add(chat);
windows.add(filetransfer);
windows.add(settings);
JMenu control = new JMenu("Control");
JMenuItem quit = new JMenuItem("Quit");

quit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
control.add(quit);
JMenu help = new JMenu("Help");
JMenuItem support = new JMenuItem("Support");
JMenuItem about = new JMenuItem("About");
about.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
//HERE!
aboutWindow.setVisible(true);
}
}
);
help.add(support);
help.add(about);
menubar.add(control);
menubar.add(windows);
menubar.add(help);
controlpanel.setJMenuBar(menubar);
controlpanel.setVisible(true);
};
};

aboutWindow 已在同一个类中定义,但不是在 main 方法中定义,而是在 about 方法中定义。我该如何正确解决这个问题?

我当前的解决方案(根本不起作用)位于main方法中,我已用注释对其进行了标记。

最佳答案

尝试将 aboutWindow 设为类变量(将类重命名为驼峰式 Main 也是一个好习惯):

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

public class Main
{
public static JDialog aboutWindow;

public static void about () {
Main.aboutWindow = new JDialog();
Main.aboutWindow.setTitle("About kingfisher a0.0.3");
Main.aboutWindow.setSize(300,600);
Main.aboutWindow.setModal(true);
JMenuBar menubar = new JMenuBar();
JMenu control = new JMenu("Control");
JMenuItem quit = new JMenuItem("quit");
quit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
Main.aboutWindow.setVisible(false);
}
}
);
control.add(quit);
menubar.add(control);
Main.aboutWindow.add(menubar);
JLabel name = new JLabel("kingfisher");
Main.aboutWindow.add(name);
}

public static void main (String args[]){
//Defined window dimensions
JFrame controlpanel = new JFrame();
controlpanel.setTitle("kingfisher a0.0.3");
controlpanel.setSize(500,400);
controlpanel.add(new JLabel("The angels have the blue box"));
JMenuBar menubar = new JMenuBar();
//defining menu groups
JMenu windows = new JMenu("Windows");
JCheckBoxMenuItem chat = new JCheckBoxMenuItem("Chat");
JCheckBoxMenuItem filetransfer = new JCheckBoxMenuItem("Filetransfer");
JCheckBoxMenuItem settings = new JCheckBoxMenuItem("Settings");
windows.add(chat);
windows.add(filetransfer);
windows.add(settings);
JMenu control = new JMenu("Control");
JMenuItem quit = new JMenuItem("Quit");
quit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
}
);
control.add(quit);
JMenu help = new JMenu("Help");
JMenuItem support = new JMenuItem("Support");
JMenuItem about = new JMenuItem("About");
about.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
//HERE!
Main.aboutWindow.setVisible(true);
}
}
);
help.add(support);
help.add(about);
menubar.add(control);
menubar.add(windows);
menubar.add(help);
controlpanel.setJMenuBar(menubar);
controlpanel.setVisible(true);
};
};

关于java - 从另一个方法更改对象值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39103309/

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