简体   繁体   English

如何访问一个JFrame到另一个类JFrame

[英]how to access one JFrame to another class JFrame

I hava a problem,i am creating a two JFrame in diffrent class in same packege in eclips. 我有一个问题,我在Eclipse的同一包中的不同类中创建了两个JFrame。 in first Jframe class i hava diffrent Jbuttons for different use. 在第一个Jframe类中,我有不同的Jbutton用于不同的用途。 in JButtons one button there name is "View user profile" after clicking this button some event is perform. 在JButtons中,一个按钮的名称为“查看用户个人资料”,单击此按钮后,将执行某些事件。 the event accour when button is pressed that is another Jframe visible and this jframe show all user information which user is login. 当按下另一个Jframe可见的按钮时,事件就会发生,并且此Jframe显示该用户正在登录的所有用户信息。 but this jframe not show all the user details present in database. 但是此jframe不会显示数据库中存在的所有用户详细信息。 becouse this showing an error for accessing another class(JFrame) varialble like jbutton,jlabel etc.please help me how can i accesss different class varible in another class. 因为这显示访问另一个类(JFrame)变量(如jbutton,jlabel等)时出错,请帮助我如何访问另一个类中的不同类变量。

"please help me how can i accesss different class varible in another class." “请帮助我如何在另一个类中访问不同的类变量。”

First See The Use of Multiple JFrames, Good/Bad Practice? 首先查看使用多个JFrame,良好/不良做法?

I would instead use a modal JDialog . 我将改为使用模式JDialog See How to make Dialogs . 请参阅如何制作对话框

To access the components in the GUI class, you can just pass it as reference to the JDialog class, with getters for the components you want to access. 要访问GUI类中的组件,您可以将其作为对JDialog类的引用进行传递,并带有要访问的组件的吸气剂。

Here's an example of what I mean. 这是我的意思的例子。 You can see the JLabel from the GUI class is accessed through the getJLabel method from the GUI class. 你可以看到JLabelGUI类是通过访问getJLabel从方法GUI类。

public class GUI {
    private JLabel label;
    private MyDialog dialog;
    private JFrame frame;

    public GUI() {
        JButtton button = new JButton("Button");
        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                dialog = new JDialog(frame, true, GUI.this);
            }
        });
    }

    @Override
    public JLabel getJLabel() {
        return label;
    }
}

public class MyDialog extends JDialog {
    private GUI gui;

    public MyDialog(final JFrame frame, boolean modal, GUI gui) {
        super(frame, modal);
        this.gui = gui; 

        JButton button = new JButton("Button");
        button.addActionListener(MyListener());
    }

    private MyListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            JLabel label = gui.getJLabel();
            label.setText("Hello");
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM