简体   繁体   English

将字符串变量从一个窗口显示到另一个窗口Java Swing

[英]Displaying a string variable from one window into another window Java Swing

Apologies if the question is a little ambiguous. 如果问题有点模棱两可,我深表歉意。 Essentially, I have two separate windows. 本质上,我有两个单独的窗口。 One is the main window which takes in a String of numbers into a JTextField . 一个是主窗口,它将JTextField数字输入到JTextField I then assign those numbers to a variable (take as a given that all variables are already declared) eg 然后,我将这些数字分配给变量(假设所有变量都已声明)

xxxx = textField.getText(); 

In my second window, which opens up after a button is pushed, I need the number that was entered in the first window's textField to be automatically displayed in a textField in the second window. 在第二个窗口(按下按钮后打开)中,我需要在第一个窗口的textField中输入的数字自动显示在第二个窗口的textField中。

for this I use: 为此,我使用:

textField_1 = new JFormattedTextField();
textField_1.setEditable(false);
panel_4.add(textField_1);

textField_1.setText(dbMain.xxxx);
System.out.println(dbMain.xxxx);

textField_1.setFont(productFont);
textField_1.setColumns(10);

I can't seem to get this to work as everything I have tried just gives me a blank textField or if I try print the variable in the console, I get null . 我似乎无法使它正常工作,因为我尝试的所有操作都给了我一个空白的textField,或者如果我尝试在控制台中打印变量,则会得到null

dbMain is the main class in the program from which I'm calling xxxx from dbMain是程序中的主类,我从该程序中调用xxxx

If you call the getText() method and assign the return value to an attribute at the very beginning, in this case xxxx, it must be null before user entered any text. 如果调用getText()方法并在最开始将返回值分配给属性(在本例中为xxxx),则在用户输入任何文本之前,该值必须为null。

I recommend you provide a method to dbMain and return the getText() value at the moment the 2nd window appears, like: 我建议您为dbMain提供一个方法,并在第二个窗口出现时返回getText()值,例如:

(inside Main class) (在主舱内)

public String getTextFromTextField() {
  return this.textField.getText();
}

Then in 2nd window call dbMain.getTextFromTextField() should return the most current value entered by user. 然后在第二个窗口中调用dbMain.getTextFromTextField()应该返回用户输入的最新值。

Putting aside whether having 2 frames are good. 抛开是否有2帧效果不错。 What you want to achieve can be done in a few ways: 您想要实现的目标可以通过以下几种方式完成:

  1. MVC (Model View Controller) where you keep all your data in the model. MVC(模型视图控制器),您可以在其中保存所有数据。 So both your frames will be accessing the same model (data). 因此,您的两个框架都将访问同一模型(数据)。 Update from one side will be reflected on the other. 一方的更新将反映在另一方。

  2. Observer pattern. 观察者模式。 Create an observer and an observable interface. 创建一个观察者和一个可观察的界面。 Let your frames implement these interfaces. 让您的框架实现这些接口。 When one made any changes, it will "automatically" notify the other. 当一个进行任何更改时,它将“自动”通知另一个。

  3. Pass a reference of current frame to the other frame. 将当前帧的参考传递到另一帧。 (This is the simplest to implement) (这是最简单的实现)

Example

class WindowOne extends JFrame{
    private JFrame windowTwo;
}
class WindowTwo extends JFrame{
    private JFrame windowOne;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM