调用另一个 JFrame 中的方法。

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

Call a method from another JFrame

问题

MainFrame.java

import frames.SelectFrame;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MainFrame extends JFrame {
    private JPanel mainPanel;
    private JComboBox cbOption;
    private JButton btConfirm;
    private JLabel lbCredit;
    private JFrame createFrame;

    public static void main(String[] args) {
        JFrame mainFrame = new JFrame("");
        mainFrame.setContentPane(new MainFrame().mainPanel);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.pack();
        mainFrame.setVisible(true);
        mainFrame.setBounds(500, 500, 400, 400);
    }

    public MainFrame() {
        btConfirm.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int option = cbOption.getSelectedIndex();
                System.out.println(option);
                if (option == 0) {
                    JFrame createFrame = new SelectFrame();
                    createFrame.setContentPane(new SelectFrame().mainPanel);
                    createFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    createFrame.pack();
                    createFrame.setVisible(true);
                    createFrame.setBounds(500, 500, 400, 400);
                    System.out.println("Frame created!");
                } else if (option == 1) {

                } else {

                }
            }
        });
    }

    public final JFrame getCreateFrame() {
        return createFrame;
    }
}

CreateFrame.java

package frames;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SelectFrame extends JFrame {
    public JPanel mainPanel;
    private JLabel lbCredit;
    private JComboBox cbxCurrency;
    private JButton btConfirm;
    private JButton btExit;

    public SelectFrame() {
        btConfirm.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });

        btExit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });
    }
}
英文:

I want to get a JFrame (createFrame) from another JFrame (mainFrame). I created a method in the mainFrame which returns the createFrame, so that I can dispose the createFrame when clicking a button in the createFrame. But I cant call this methode (getCreateFrame) from the second JFrame (createFrame).

MainFrame.java

import frames.SelectFrame;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MainFrame extends JFrame{
private JPanel mainPanel;
private JComboBox cbOption;
private JButton btConfirm;
private JLabel lbCredit;
private JFrame createFrame;
public static void main(String[] args) {
JFrame mainFrame = new JFrame("");
mainFrame.setContentPane(new MainFrame().mainPanel);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.pack();
mainFrame.setVisible(true);
mainFrame.setBounds(500, 500, 400, 400);
}
public MainFrame() {
btConfirm.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int option = cbOption.getSelectedIndex();
System.out.println(option);
if(option == 0){
JFrame createFrame = new SelectFrame();
createFrame.setContentPane(new SelectFrame().mainPanel);
createFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
createFrame.pack();
createFrame.setVisible(true);
createFrame.setBounds(500, 500, 400, 400);
System.out.println("Frame created!");
} else if(option == 1){
}  else{
}
}
});
}
public final JFrame getCreateFrame(){
return createFrame;
}
}

CreateFrame.java

package frames;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;
public class SelectFrame extends JFrame{
public JPanel mainPanel;
private JLabel lbCredit;
private JComboBox cbxCurrency;
private JButton btConfirm;
private JButton btExit;
public SelectFrame() {
btConfirm.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
btExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
}
}

答案1

得分: 1

回答你的问题,我认为你应该查看编程范围

如果你想在SelectFrame类中访问getCreateFrame()方法,你需要能够以某种方式 达到 MainFrame 类,因为这个类保存了这个方法。

createFrameMainFrame 类的一个私有变量,这意味着它只能在类内部访问。这就是为什么(如你所做的)你需要一个获取方法来检索它。不过,当你调用获取方法时,你想知道要访问哪个对象的 createFrame。问题是,SelectFrame 怎么知道要调用哪个方法?

如果你像这样编辑你的 SelectFrame 类:

public class SelectFrame extends JFrame {
    
    private MainFrame mFrame;
    
    public SelectFrame(MainFrame frame) {
        mFrame = frame;
    }

}

你现在可以在 SelectFrame 类中从你传递给 SelectFrame 构造函数的 MainFrame 中调用 getCreateFrame()。(mFrame.getCreateFrame();

注意:在 MainFrame 类的构造函数中,你在一个 if 语句中初始化了你的 createFrame,这意味着除非 option == 0,否则它会给你返回 null

if(option == 0) {
    JFrame createFrame = new SelectFrame(); 
}
英文:

To answer your question I believe you should take a look at Programming scope.

If you want to access the getCreateFrame() method in your SelectFrame-class you will need to be able to reach the MainFrame-class somehow since this holds the method.

createFrame is a private variable of the MainFrame-class which means it's only accessible within the class. Which is why (as you have done) you need a get-method to retrieve it. Although, when you call the get-method you want to know which object's createFrame you are trying to access. The question is, how will SelectFrame know which method to call?

If you edit your SelectFrame-class alike this:

public class SelectFrame extends JFrame {
private MainFrame mFrame;
public SelectFrame(MainFrame frame) {
mFrame = frame;
}
}

You can now call the getCreateFrame() from the MainFrame you pass to SelectFrame's constructor within the SelectFrame class. (mFrame.getCreateFrame();)

Note: In the MainFrame-class's constructor you initialize your createFrame in an if-statement which means it will give you null unless option == 0.

if(option == 0) {
JFrame createFrame = new SelectFrame(); 
}

答案2

得分: 0

你返回的createFrame为空。这是因为你在MainFram构造函数中引入了一个局部实例JFrame createFrame = new SelectFrame();

public final JFrame getCreateFrame(){
     return createFrame;
}
英文:

Your returned createFrame is null. This is due to the fact that you have introduced an local instance of JFrame createFrame = new SelectFrame(); in the MainFram constructor

public final JFrame getCreateFrame(){
return createFrame;
}

答案3

得分: 0

在你的构造函数中,你创建了一个局部变量,而不是赋值给你创建的实例变量。

JFrame createFrame = new SelectFrame();

而应该是:

createFrame = new SelectFrame();

不要创建一个新变量,而是赋值给已经存在的变量。

英文:

In your constructor, you are creating a local variable instead of assigning to the instance variable you created.

JFrame createFrame = new SelectFrame();

Should instead have been:

createFrame = new SelectFrame();

Don't create a new variable, assign to the existin one.

huangapple
  • 本文由 发表于 2020年9月9日 15:39:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63806861.html
匿名

发表评论

匿名网友

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

确定