3

皆さん、おっしゃるようにコードにいくつか変更を加えました。私は3つのクラスを持っています:

2番目のクラス(および最初のGUI):4つJButtonの秒があります-Simulare、CazParticular、StartおよびHandSelection、いくつかJLabelの秒と3JTextField秒。HandSelectionボタンを押すと、別のフレームが別のコンテンツで作成されます。

3番目のクラス(および2番目のGUI):2つありますJButton-[OK]と[キャンセル]などです。[OK]ボタンを押すとJTextField(QuesHandText)、最初のGUIからにアクセスし、メソッドを使用しますsetText()。私はこれを理解することができません、私はこれを4-5日のように考えています、そしてそれでも答えを得ることができません。私を助けてください!

スクリーンショット

JTextFieldfrom 2nd class(first GUI)のテキストを変更できるようにするには、ifステートメントにどのコードを書き込む必要がありますか?

ファーストクラス:

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
    }
}

2番目のクラス:

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");
            }
        }
    }
}
4

5 に答える 5

3

おそらく、実際には2つのWindowsは必要ありません。必要なのは、クラスで実現できるダイアログですJOptionPane

これがデモコードです。

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();
            }
        });
    }
}
于 2012-07-15T16:18:25.823 に答える
3

最初のGUIに、最初のGUIのインスタンスを返すpublic JTextField getQuesHandText()メソッドと静的メソッドを追加します。これで、どこからでもpublic static JFrame getInstance()呼び出してインスタンスを取得できます。このメソッドでは、一度に1つのインスタンスしか持てないことに注意してください。SecondClass.getInstance().getQuesHandText()JTextFieldSecondClass

メソッド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のインスタンスを手動で作成しないように注意してください。

于 2012-07-15T14:40:41.787 に答える
3

開始されたインスタンスを使用して、その変数Classにアクセスします。publicしたがって、次のようなことを行う必要があります。

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

または、 (そうでJTextFieldはないが)コンテンツを変更するためのアクセス権をprivate持つインスタンスメソッドを使用している場合は、これが推奨されるメソッドです。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");
}
于 2012-07-15T14:41:10.713 に答える
3

構成を使用、

1.JFrameを含むクラスのインスタンスを作成します。JFrameのJTextFieldにアクセスする必要があります。

2.次に、そのインスタンスで、JTextFieldのsetterまたはgetterメソッドを呼び出します。

編集:

Mainクラスにシングルトンの原則を実装していることを確認してください。そうしないと、必要のない新しいインスタンスが取得されます......2番目のクラス。

パブリッククラスGuiMain{

Main m = new Main();

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

//その他のもの

}

于 2012-07-15T14:42:37.167 に答える
2

GuiMainのハンドラーで、GuiSelectionのコンストラクターへの引数としてそれ自体(メインJFrame)を渡します。

GuiSelection gui2 = new GuiSelection(this);

そして、GuiSelectionのコンストラクターをから変更します

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

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

また、GuiSelectionがGuiMainのサブクラスであることはかなり奇妙に思えます。おそらく、これらは両方ともJFrameの直接のサブクラスである必要があります。

また、Swingに関連するすべてのものはイベントディスパッチスレッドで実行される必要があるため、SwingUtilities.invokeLater内のmainメソッドですべてをラップする必要があります。

また、パブリックメンバー変数は絶対に使用しないでください。Javaではありません。

于 2012-07-15T14:57:21.330 に答える