如何知道在Java中哪个引用是静态的

huangapple go评论76阅读模式
英文:

How to know which reference is static in Java

问题

以下是您提供的代码的翻译部分:

这是我的第一个窗口

public class MainFrame extends JFrame {
	
	private Manager manager = new Manager();
	private JPanel titlepane;
	private JLabel title;
	
	MainFrame(String name){
		setTitle(name);
	}
	
	public void content() {
		Font titlefont = new Font("Times New Roman", Font.PLAIN, 22);
		setLayout(new BorderLayout());
		
		titlepane = new JPanel();
		title = new JLabel("欢迎来到POS!");
		title.setFont(titlefont);
		titlepane.add(title);
		manager.LoginGUI();
		
		add(titlepane,BorderLayout.NORTH);
		add(manager,BorderLayout.CENTER);
	}
	
	public void runGUI() {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				content();
				setSize(700,700);
				setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				setVisible(true);
				setLocationRelativeTo(null);
			}
		});
	}
}

这是另一个类,其中包含按钮

public class Manager extends JPanel implements ActionListener {

	private ArrayList<AccountInfo> manager = new ArrayList<AccountInfo>();
	private GridBagConstraints gbc = new GridBagConstraints();

	private JLabel id;
	private JLabel pw;

	private JTextField idfill;
	private JTextField pwfill;

	private JButton login;

	private int isManager = 0;

	private String idinput, pwinput;
	private int temp = -1;

	Manager() {
		this.manager.add(new AccountInfo("admin", "1234"));
	}

	public void addManager(AccountInfo newManager) {
		this.manager.add(newManager);
	}

	public void LoginGUI() {
		Font standard = new Font("Times New Roman", Font.PLAIN, 18);
		setLayout(new GridBagLayout());

		id = new JLabel("ID");
		id.setFont(standard);
		// 对齐
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.ipadx = 10;
		gbc.ipady = 10;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.fill = GridBagConstraints.VERTICAL;
		gbc.anchor = GridBagConstraints.FIRST_LINE_START;
		add(id, gbc);

		idfill = new JTextField(10);
		idfill.setFont(standard);
		// 对齐
		gbc.gridx = 1;
		gbc.gridy = 0;
		gbc.anchor = GridBagConstraints.FIRST_LINE_START;
		add(idfill, gbc);

		pw = new JLabel("密码");
		pw.setFont(standard);
		// 对齐
		gbc.gridx = 0;
		gbc.gridy = 1;
		gbc.anchor = GridBagConstraints.FIRST_LINE_START;
		add(pw, gbc);

		pwfill = new JTextField(10);
		pwfill.setFont(standard);
		// 对齐
		gbc.gridx = 1;
		gbc.gridy = 1;
		gbc.anchor = GridBagConstraints.FIRST_LINE_START;
		add(pwfill, gbc);

		login = new JButton("登录");
		login.setFont(standard);
		login.addActionListener(this);
		// 对齐
		gbc.gridx = 1;
		gbc.gridy = 2;
		gbc.insets = new Insets(5, 5, 5, 5);
		gbc.anchor = GridBagConstraints.FIRST_LINE_START;
		add(login, gbc);
	}

	public void actionPerformed(ActionEvent e) {
		verify();
		if(isManager == 1) {
			MenuFrame menu = new MenuFrame("菜单");
			menu.runGUI();
			MainFrame.setVisible(false);    // 这是问题所在
		}
	}

	private void verify() {
		idinput = idfill.getText().trim();
		pwinput = pwfill.getText();
		for (int i = 0; i < manager.size(); i++) {
			if (idinput.equals(manager.get(i).id)) {
				temp = i;
			}
		}
		
		if(temp == -1) {
			JOptionPane.showMessageDialog(null, "用户名或密码不正确,请重试");
		} else if(pwinput.equals(manager.get(temp).password)) {
			isManager = 1;
		} else 
			JOptionPane.showMessageDialog(null, "用户名或密码不正确,请重试");
	}
}

您遇到的问题是:

无法从类型Window对非静态方法setVisible(boolean)进行静态引用

这个问题发生在您尝试在Manager类的actionPerformed方法中使用setVisible(false)来关闭MainFrame。要解决这个问题,您可以使用Mainframe.this.setVisible(false)来访问外部类MainFrame的实例并关闭它,如下所示:

public void actionPerformed(ActionEvent e) {
    verify();
    if (isManager == 1) {
        MenuFrame menu = new MenuFrame("菜单");
        menu.runGUI();
        MainFrame.this.setVisible(false); // 关闭外部类MainFrame
    }
}

这样,您就可以正确地关闭MainFrame窗口。希望这可以帮助您解决问题!

英文:

I am currently working on GUI of simple food ordering system. I created a button that whenever user clicks it it will go to another frame, however I am facing problem when I want to close the first frame (setVisible(false)).

This is my first frame

public class MainFrame extends JFrame {
private Manager manager = new Manager();
private JPanel titlepane;
private JLabel title;
MainFrame(String name){
setTitle(name);
}
public void content() {
Font titlefont = new Font(&quot;Times New Roman&quot;, Font.PLAIN, 22);
setLayout(new BorderLayout());
titlepane = new JPanel();
title = new JLabel(&quot;Welcome to POS!&quot;);
title.setFont(titlefont);
titlepane.add(title);
manager.LoginGUI();
add(titlepane,BorderLayout.NORTH);
add(manager,BorderLayout.CENTER);
}
public void runGUI() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
content();
setSize(700,700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setLocationRelativeTo(null);
}
});
}

This is another class where the button is

public class Manager extends JPanel implements ActionListener {
private ArrayList&lt;AccountInfo&gt; manager = new ArrayList&lt;AccountInfo&gt;();
private GridBagConstraints gbc = new GridBagConstraints();
private JLabel id;
private JLabel pw;
private JTextField idfill;
private JTextField pwfill;
private JButton login;
private int isManager = 0;
private String idinput, pwinput;
private int temp = -1;
Manager() {
this.manager.add(new AccountInfo(&quot;admin&quot;, &quot;1234&quot;));
}
public void addManager(AccountInfo newManager) {
this.manager.add(newManager);
}
public void LoginGUI() {
Font standard = new Font(&quot;Times New Roman&quot;, Font.PLAIN, 18);
setLayout(new GridBagLayout());
id = new JLabel(&quot;ID&quot;);
id.setFont(standard);
// Alignment
gbc.gridx = 0;
gbc.gridy = 0;
gbc.ipadx = 10;
gbc.ipady = 10;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.fill = GridBagConstraints.VERTICAL;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
add(id, gbc);
idfill = new JTextField(10);
idfill.setFont(standard);
// Alignment
gbc.gridx = 1;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
add(idfill, gbc);
pw = new JLabel(&quot;Password&quot;);
pw.setFont(standard);
// Alignment
gbc.gridx = 0;
gbc.gridy = 1;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
add(pw, gbc);
pwfill = new JTextField(10);
pwfill.setFont(standard);
// Alignment
gbc.gridx = 1;
gbc.gridy = 1;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
add(pwfill, gbc);
login = new JButton(&quot;Login&quot;);
login.setFont(standard);
login.addActionListener(this);
// Alignment
gbc.gridx = 1;
gbc.gridy = 2;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
add(login, gbc);
}
public void actionPerformed(ActionEvent e) {
verify();
if(isManager == 1) {
MenuFrame menu = new MenuFrame(&quot;Menu&quot;);
menu.runGUI();
MainFrame.setVisible(false);    // This is the problem
}
}
private void verify() {
idinput = idfill.getText().trim();
pwinput = pwfill.getText();
for (int i = 0; i &lt; manager.size(); i++) {
if (idinput.equals(manager.get(i).id)) {
temp = i;
}
}
if(temp == -1) {
JOptionPane.showMessageDialog(null, &quot;Id or password incorrect, try again&quot;);
} else if(pwinput.equals(manager.get(temp).password)) {
isManager = 1;
} else 
JOptionPane.showMessageDialog(null, &quot;Id or password incorrect, try again&quot;);
}
}

(The codes are a bit lengthy as I am not confident that the other part was correct. All I know this has nothing to do with MenuFrame)

I get this error:

> Cannot make a static reference to the non-static method setVisible(boolean) from the type Window

It might be my fault where it is not obvious enough for me to know which part of Manager or MainFrame is static. I also came across other posts regarding the same issue but none relates with mine. (Other post was having obvious static method)

Also tried the create an MainFrame object in Manager but it made it worse, please help, thank you!

答案1

得分: 1

确实需要将MainFrame 对象 保留在可访问的地方,保持对它的引用。对于这个 MVC,模型-视图-控制器 是一个很好的范例。

  1. 使用MVC

我个人将Swing的main方法放在一个Controller类中(所以Controller是应用程序类)。它创建主窗口(View),并将控制器传递给它。

public void actionPerformed(ActionEvent e) {
    verify();
    if(isManager == 1) {
        MenuFrame menu = new MenuFrame("Menu");
        menu.runGUI();
        controller.setMainFrameVisible(false);
    }
}

Controller:

private MainFrame mainFrame;

public setMainFrameVisible(boolean visible) {
    mainFrame.setVisible(visible);
}
  1. 传递MainFrame实例。

但是你也可以传递MainFrame:

private final MainFrame mainFrame;

Manager(MainFrame mainFrame) {
    this.mainFrame = mainFrame;
}

public void actionPerformed(ActionEvent e) {
    verify();
    if(isManager == 1) {
        MenuFrame menu = new MenuFrame("Menu");
        menu.runGUI();
        mainFrame.setVisible(false);
    }
}
  1. 如果面板在MainFrame内部
((JFrame) getTopLevelAncestor()).setVisible(false);

提示:

如果应用程序退出(EXIT_ON_CLOSE),改变默认的关闭操作。

MainFrame(String name){
    setTitle(name);
    setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
英文:

You indeed need to keep the MainFrame object somewhere accessible, keep a reference to it. For this MVC, Model-View-Controller, is a nice paradigm.

  1. Use MVC

I personally have my main method for swing in a Controller class (so the controller is the application class). It creates the main frame (View) and the controller is passed.

public void actionPerformed(ActionEvent e) {
verify();
if(isManager == 1) {
MenuFrame menu = new MenuFrame(&quot;Menu&quot;);
menu.runGUI();
controller.setMainFrameVisible(false);
}
}

Controller:

private MainFrame mainFrame;
public setMainFrameVisible(boolean visible) {
MainFrame.setVisible(visible);
}
  1. Pass the MainFrame instance.

However you may also pass the MainFrame:

private final MainFrame mainFrame;
Manager(MainFrame mainFrame) {
this.mainFrame = mainFrame;
}
public void actionPerformed(ActionEvent e) {
verify();
if(isManager == 1) {
MenuFrame menu = new MenuFrame(&quot;Menu&quot;);
menu.runGUI();
mainFrame.setVisible(false);
}
}
  1. If the panel is inside the MainFrame

    ((JFrame) getTopLevelAncestor()).setVisible(false);


Tip:

Should the application exit (EXIT_ON_CLOSE), change the default close operation.

MainFrame(String name){
setTitle(name);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}

huangapple
  • 本文由 发表于 2020年8月4日 13:59:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63241006.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定