gpt4 book ai didi

java - Jframe 到 Jframe 变量

转载 作者:行者123 更新时间:2023-12-02 04:40:55 26 4
gpt4 key购买 nike

我将 nameS 保存到字符串中并写入文件,但在下一个窗口中会弹出一条成功消息,我希望能够使用变量 nameS 中给出的名称到成功的窗口。不知道如何通过它。

public void setPanels()
{
mainPanel = new JPanel(new GridLayout(0, 2));
JPanel containerPanel = new JPanel(new GridLayout(0, 1));
JPanel lowerPanel = new JPanel(new FlowLayout());
JPanel errorPanel = new JPanel(new FlowLayout());
JButton apply = new JButton("PAY");
JButton cancel = new JButton("Cancel");
cancel.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dispose();
}
});
apply.addActionListener(new ActionListener()
{

public void actionPerformed(ActionEvent e)
{
errorMessage= "";
safetyMessage= "";

String timestamp = new java.text.SimpleDateFormat("MM/dd/yyyy h:mm:ss a").format(new Date());
String ammountS = ammountField.getText();
String nameS = nameField.getText();
String billingAddressS = billingAddressField.getText();
String cardNameS = cardNameField.getText();
String cardNumberS = cardNumberField.getText();
String expiryDateS = expiryDateField.getText();

if(ammountS.matches("[a-zA-Z]+")){
safetyMessage += "Top Ammount invalid<br/>";
}else{errorMessage += "";}
if(ammountS == null || ammountS.isEmpty()){
safetyMessage += "Please Enter an Ammount!<br/>";
}else{errorMessage += "";}
if(nameS == null || nameS.isEmpty()){
safetyMessage += "Please Enter an Name!<br/>";
}else{errorMessage += "";}
if(billingAddressS == null || billingAddressS.isEmpty()){
safetyMessage += "Please Enter a Billing Address!<br/>";
}else{errorMessage += "";}
if(cardNameS == null || cardNameS.isEmpty()){
safetyMessage += "Please Enter a Card Name!<br/>";
}else{errorMessage += "";}
if(cardNumberS == null || cardNumberS.isEmpty()){
safetyMessage += "Please Enter a Card Number!<br/>";
}else{errorMessage += "";}
if(expiryDateS == null || expiryDateS.isEmpty()){
safetyMessage += "Please Enter a Expiry Date!<br/>";
}else{errorMessage += "";}

if (safetyMessage == ""){
errorMessage += "Successfully Topped Up With: $"+ ammountS;
balanceAmmount(nameS);
errorField.setText(errorMessage);
errormsg = true;
}else{
errorMessage += "<html>";
errorMessage += safetyMessage;
errorMessage += "</html>";
errorField.setText(errorMessage);
}

if (errormsg == true){
WriterFile("######## START OF TRANSACTION ########\n","TopUp.txt");
WriterFile("Name: "+ nameS+"\n","TopUp.txt");
WriterFile("DATE: "+ timestamp+"\n","TopUp.txt");
WriterFile("Ammount: "+ ammountS+"\n","TopUp.txt");
WriterFile("CardName: "+ cardNameS+"\n","TopUp.txt");
WriterFile("Address: "+ billingAddressS+"\n","TopUp.txt");
WriterFile("Card Number: "+ cardNumberS+"\n","TopUp.txt");
WriterFile("ExpiryDate: "+ expiryDateS+"\n","TopUp.txt");
WriterFile("######## END OF TRANSACTION ########\n","TopUp.txt");

topUpAlertWindow TopUpAlertWindow = new topUpAlertWindow();
//balanceAmmount(nameS);
//i am trying to pass in nameS into here.
//dispose();
}
}
});

我想将 nameS 变量发送到下一个 java 文件

import javax.swing.*;
import java.awt.Dialog.ModalityType;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.util.*;

class topUpAlertWindow extends JDialog
{
public topUpAlertWindow(){
setPanels();

setModalityType(ModalityType.APPLICATION_MODAL);
setSize(500, 250);
setVisible(true);
}

private JPanel mainPanel;

public void setPanels()
{
mainPanel = new JPanel(new GridLayout(0, 2));
JPanel containerPanel = new JPanel(new GridLayout(0, 1));
JPanel lowerPanel = new JPanel(new FlowLayout());
JPanel errorPanel = new JPanel(new FlowLayout());
JLabel successfulMessage = new JLabel("Successfully Toped UP");
JButton apply = new JButton("Okay");
JButton cancel = new JButton("Cancel");
cancel.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dispose();
}
});

apply.addActionListener(new ActionListener()
{

public void actionPerformed(ActionEvent e)
{
dispose();


}
});

mainPanel.add(successfulMessage);

lowerPanel.add(apply);
//lowerPanel.add(cancel);


containerPanel.add(mainPanel);
containerPanel.add(lowerPanel);
containerPanel.add(errorPanel);

add(containerPanel);
}
}

最佳答案

第一个类:

topUpAlertWindow TopUpAlertWindow = new topUpAlertWindow(nameS);

在第二个类中,更改构造函数:

public topUpAlertWindow(String nameS){
setPanels();

setModalityType(ModalityType.APPLICATION_MODAL);
setSize(500, 250);
setVisible(true);
}

在构造函数中,您可以使用名称来显示标签或其他内容。

关于java - Jframe 到 Jframe 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30213767/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com