好的,伙计们,我已经按照你的说法对我的代码进行了一些更改。我有 3 节课:
第二类(也是第一个 GUI):我有 4 个JButton
- Simulare、CazParticular、Start 和 HandSelection,一些JLabel
s 和 3 个JTextField
;当我按下 HandSelection 按钮时,它会创建另一个具有不同内容的框架。
第三节课(和第二个 GUI):我有 2JButton
秒 - 确定和取消以及其他东西。当我按下 Ok 按钮时,我想JTextField(QuesHandText)
从第一个 Gui 访问并使用方法setText()
。我想不通,我想了 4-5 天,但仍然无法得到答案。请帮我!
我应该在 if 语句中编写什么代码才能修改JTextField
第二类(第一个 GUI)中的文本?
一级:
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");
}
}
}
}