【问题标题】:access vars from another JFrame Gui从另一个 JFrame Gui 访问变量
【发布时间】:2012-07-14 14:49:16
【问题描述】:

好的,伙计们,我已经按照你们所说的对我的代码进行了一些更改。我有 3 节课:

第二个类(和第一个 GUI):我有 4 个JButtons - Simulare、CazParticular、Start 和 HandSelection,一些 JLabels 和 3 个JTextFields;当我按下 HandSelection 按钮时,它会创建另一个具有不同内容的框架。

第 3 类(和第二个 GUI):我有 2 个 JButtons - Ok 和 Cancel 和其他东西。当我按下 Ok 按钮时,我想从第一个 Gui 访问 JTextField(QuesHandText) 并使用方法 setText()。我想不通,我想了 4-5 天,但仍然无法得到答案。请帮帮我!

我应该在 if 语句中编写什么代码才能从第二类(第一个 GUI)修改 JTextField 中的文本?

头等舱:

import javax.swing.JFrame;
public class Main {

    public static void main(String[] args){

        //other stuff
        GuiMain gui = new GuiMain();

        gui.frame1.setLocation(150,150);
        gui.frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        gui.frame1.setSize(400,250);
        gui.frame1.setResizable(false);
        gui.frame1.setVisible(true);

        //other stuff
    }
}

二等:

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import javax.swing.*;

public class GuiMain {

    public static GuiMain instance;
public static GuiMain getInstance(){
    if(GuiMain.instance == null){GuiMain.instance = new GuiMain();}
    return GuiMain.instance;
}
    public JFrame frame1 = new JFrame();

    public JTextField QuesHandText, FlopTurnRiverText, RezultatText; 
    public JButton Simulare, CazParticular, Start, HandSelection;
    public int w1,h1;
    public JLabel someText;
    static int u=0;
    public int j=0;

    public GuiMain(){

        frame1.setTitle("HoldemTool");
        frame1.setLayout(null);
        QuesHandText = new JTextField(4);

        Simulare = new JButton("Simulare");
        CazParticular = new JButton("Caz particular");
        Start = new JButton("Start");
        HandSelection = new JButton(new ImageIcon(getClass().getResource("GuiPic.png")));
        Handler handler1 = new Handler();

        CazParticular.addActionListener(handler1);
        Simulare.addActionListener(handler1);

        HandSelection.addActionListener(handler1);
        Start.addActionListener(handler1);

        QuesHandText.setEditable(false);
        FlopTurnRiverText.setEditable(false);
        RezultatText.setEditable(false);

        frame1.add(Welcome1);
        frame1.add(Welcome2);
        frame1.add(QuesHand);
        frame1.add(FlopTurnRiver);
        frame1.add(Rezultat);
        frame1.add(QuesHandText);
        frame1.add(FlopTurnRiverText);
        frame1.add(RezultatText);
        frame1.add(Simulare);
        frame1.add(CazParticular);
        frame1.add(Start);

    }

    public JTextField getQuesHandText(){
    return QuesHandText;
}
    public class Handler implements ActionListener{
        public void actionPerformed(ActionEvent e){

            if(e.getSource()==Simulare)
            {

            }
            if(e.getSource()==CazParticular){
                QuesHandText.setEditable(true);
                FlopTurnRiverText.setEditable(true);

                QuesHandText.setText("");
                FlopTurnRiverText.setText("");
                RezultatText.setText("");

                frame1.setSize(470, 250);
                Start.setBounds(3*FlopTurnRiverText.getX(), QuesHand.getY(), 65, h1);
                HandSelection.setBounds(3*FlopTurnRiverText.getX(), FlopTurnRiverText.getY(), 65, h1);

                frame1.add(HandSelection);
                frame1.add(Start);
            }
            if(e.getSource()==Start){
                QuesHandText.setText("Text");
            }
            if(e.getSource()==HandSelection){
                GuiSelection gui2 = new GuiSelection();  
                gui2.frame2.setVisible(true);
            }
        }

    }}

第三课

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import javax.swing.*;

public class GuiSelection extends GuiMain {

    JFrame frame2 = new JFrame();
    GuiMain guiMain;
    public JButton Ok,Cancel;

    //other stuff

    public GuiSelection(){
        guiMain = new GuiMain();
        frame2.setTitle("Hand selection");
        frame2.setSize(1135,535);
        frame2.setLayout(null);
        frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame2.setVisible(true);
        frame2.setResizable(false);

        //other stuff

        Handler2 handler2 = new Handler2();

        Ok.addActionListener(handler2);
        Cancel.addActionListener(handler2);

        frame2.add(Ok); frame2.add(Cancel); 

    }

    public class Handler2 implements ActionListener{
        public void actionPerformed(ActionEvent e){
            if(e.getSource()==Cancel){
                frame2.hide();
            }
            if(e.getSource()==Ok)
            {
            GuiMain.getInstance().getQuesHandText().setText("From Ok");

                //When I prees this button "Ok" I want to get access to the JTextField(QuesHandText) in the         GuiMain class, and .setText();
                //somothing like QuesHandtText.setText("someText");
            }
        }
    }
}

【问题讨论】:

  • 1) 为了尽快获得更好的帮助,请发帖 SSCCE。 2) 见The Use of Multiple JFrames, Good/Bad Practice? 3) 如前所述,更喜欢组合而不是继承。
  • 嘿,你能解释一下为什么GuiSelection扩展GuiMain吗?我还可以看到GuiSelection中有一个GuiMain的对象
  • 我以为我将来会帮助我,但它确实t ... thats 不是问题。

标签: java swing user-interface methods jframe


【解决方案1】:

public JTextField getQuesHandText() 方法和返回第一个GUI 实例的静态方法public static JFrame getInstance() 添加到您的第一个GUI。现在您可以从任何地方调用SecondClass.getInstance().getQuesHandText() 来获取JTextField 实例。请注意,使用此方法,您在任何时候都只能拥有一个 SecondClass 实例。

您的getInstance() 方法如下所示:

public class SecondClass extends JFrame {

    private static SecondClass instance;

    public static SecondClass getInstance() {
        if(SecondClass.instance == null)
            SecondClass.instance = new SecondClass();

        return SecondClass.instance
    }

}

请注意,您不应手动创建SecondClass 的实例。

【讨论】:

  • 和 getQuesHandText(){ return QuesHandText;} 的代码可以吗?我也在主代码中编辑过
  • 看看第三课,最后几行 (GuiMain.getInstance().getQuesHandText().setText("From Ok");)
  • 是的,这是正确的。或者,如果您想限制对实际 JTextField 的访问,您可以将函数 getQuesHandText() 替换为返回 JTextField 内容的方法和设置 JTextField 内容的方法。
  • 但是使用 GuiMain.getInstance() 而不是 new GuiMain()
  • 我做了你告诉我的一切,但仍然没有工作: GuiMain.getInstance().getQuesHandText().setText("From Ok");
【解决方案2】:

使用已启动的Class 的实例来访问其public 变量。所以你应该这样做:

GuiMain main=new GuiMain();
...
main.QuesHandtText.setText("someText");

或者,如果您的 JTextFieldprivate(尽管不是),请使用具有 public 访问权限的实例方法来更改其内容 - 这是首选方法:

A类:

class A {
private JTextField tf;
public void setFieldText(String text) {
tf.setText(text);
}
}

B类:

class B {
A a = new A();
a.setText("hello");
}

【讨论】:

  • 我相信他需要 A之后创建B。
  • 你没有 setFieldText 的返回类型
【解决方案3】:

使用作曲

1.创建包含JFrame的类的实例,您需要访问其JTextField。

2. 然后在该实例上调用 JTextField 的 setter 或 getter 方法。

已编辑:

确保你已经在 Main 类上实现了 Singleton 原则,否则你会得到 一个你不想要的新实例...... 二等舱。

公共类 GuiMain{

主 m = new Main();

m.getText(); m.setText();

// 其他东西

}

【讨论】:

  • 我认为他在如何从另一个 JFrame 访问该实例时遇到了麻烦,您没有讨论。
【解决方案4】:

在 GuiMain 的处理程序中,将自身(主 JFrame)作为参数传递给 GuiSelection 的构造函数:

GuiSelection gui2 = new GuiSelection(this);

然后将 GuiSelection 的构造函数从

public GuiSelection(){
    guiMain = new GuiMain();
    ...

public GuiSelection(GuiMain guiMain){
    this.guiMain = guiMain;
    ...

此外,GuiSelection 是 GuiMain 的子类似乎很奇怪。可能这两个都应该是 JFrame 的直接子类。

此外,您应该将所有内容都包含在 SwingUtilities.invokeLater 的 main 方法中,因为与 Swing 相关的所有内容都应该在事件调度线程上运行。

此外,您永远不应该使用公共成员变量:它非常不符合 Java。

【讨论】:

    【解决方案5】:

    也许您并不真的需要两个 Windows。你需要的是一个可以通过JOptionPane类实现的Dialog。

    这是一个演示代码。

    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    
    public class YourGui implements ActionListener {
        private JFrame frame;
        private JTextField text;
        private JButton takeInput;
    
        public YourGui() {
            frame = new JFrame();
            frame.setLayout(new GridLayout(2, 1));
    
            text = new JTextField();
            takeInput = new JButton("Take Input!");
    
            frame.add(text);
            frame.add(takeInput);
    
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(400, 100);
    
            takeInput.addActionListener(this);
        }
    
        public void show() {
            frame.setVisible(true);
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            int selection =
                JOptionPane.showConfirmDialog(frame, "Select Hand", "Select",
                    JOptionPane.OK_CANCEL_OPTION);
    
            if (selection == JOptionPane.OK_OPTION) {
                text.setText("You selected ok");
            }
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    YourGui gui = new YourGui();
                    gui.show();
                }
            });
        }
    }
    

    【讨论】:

    • +1 这个想法很好,但是,您需要更改JOptionPane 的组件以显示图像的Jlabels。这是Adding a JPanel to JOptionPane 的一个示例
    猜你喜欢
    • 1970-01-01
    • 2014-10-07
    • 1970-01-01
    • 1970-01-01
    • 2011-10-15
    • 1970-01-01
    • 2019-06-11
    • 1970-01-01
    相关资源
    最近更新 更多