[英]Call a method from another JFrame
I want to get a JFrame (createFrame) from another JFrame (mainFrame).我想从另一个 JFrame (mainFrame) 获取 JFrame (createFrame)。 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.
我在 mainFrame 中创建了一个返回 createFrame 的方法,这样我就可以在单击 createFrame 中的按钮时处理 createFrame。 But I cant call this methode (getCreateFrame) from the second JFrame (createFrame).
但是我不能从第二个 JFrame (createFrame) 调用这个方法 (getCreateFrame)。
MainFrame.java主框架.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创建框架.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) {
}
});
}
}
To answer your question I believe you should take a look at Programming scope .要回答你的问题,我相信你应该看看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.如果您想访问
SelectFrame
类中的getCreateFrame()
方法,您将需要能够以某种方式访问MainFrame
类,因为它包含该方法。
createFrame
is a private variable of the MainFrame
-class which means it's only accessible within the class. createFrame
是MainFrame
类的私有变量,这意味着它只能在类中访问。 Which is why (as you have done) you need a get-method to retrieve it.这就是为什么(正如您所做的那样)您需要一个 get 方法来检索它。 Although, when you call the get-method you want to know which object's
createFrame
you are trying to access.虽然,当您调用 get-method 时,您想知道您尝试访问哪个对象的
createFrame
。 The question is, how will SelectFrame
know which method to call?问题是,
SelectFrame
如何知道调用哪个方法?
If you edit your SelectFrame
-class alike this:如果您像这样编辑
SelectFrame
类:
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.现在,您可以拨打
getCreateFrame()
从MainFrame
传递给SelectFrame
的构造函数中SelectFrame
类。 ( mFrame.getCreateFrame();
) (
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
.注意:在
MainFrame
类的构造函数中,您在if
语句中初始化createFrame
,这意味着除非option == 0
否则它将为您提供null
。
if(option == 0) {
JFrame createFrame = new SelectFrame();
}
Your returned createFrame is null.您返回的 createFrame 为空。 This is due to the fact that you have introduced an local instance of JFrame createFrame = new SelectFrame();
这是因为您引入了 JFrame createFrame = new SelectFrame(); 的本地实例。 in the MainFram constructor
在 MainFram 构造函数中
public final JFrame getCreateFrame(){
return createFrame;
}
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.不要创建一个新变量,分配给现有变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.