java gui 打开另一个,java – 从另一个JFrame Gui访问vars

这篇博客讨论了在Java Swing中如何在不同的GUI类之间进行交互,特别是从一个窗口(类)访问另一个窗口(类)的JTextField。作者遇到的问题是在按下某个按钮后,尝试修改第一个GUI类中的文本字段,但遇到了困难。博客详细展示了代码结构,并请求帮助解决这个问题。
摘要由CSDN通过智能技术生成

好的,我已根据您的说法对我的代码进行了一些更改.我有3个班:

第二类(和第一个GUI):我有4个JButton – Simulare,CazParticular,Start和HandSelection,一些JLabel和3个JTextField;当我按下HandSelection按钮时,另一个框架会创建不同的内容.

第三类(和第二个GUI):我有2个JButton – 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);

}

}

}}

第3课

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

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值