以下问题:
我想将一个值更改为另一个值,但是使用不同的方法。完整代码(根据要求)如下:
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
方法,我已将其标记为评论。
答案 0 :(得分:1)
我假设aboutWindow
是一个局部变量,它在名为about
的方法中声明。使aboutWindow
对其他方法(如问题中显示的方法)的代码可见的简单方法是将其声明为字段。为此,请将声明移出about
方法。
答案 1 :(得分:1)
尝试将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);
};
};