简体   繁体   English

将一个Jframe中设置的变量从一个传递到另一个

[英]Passing variable set in one Jframe from one to another

I have one JFrame called User where I declare a variable called id and set it to a certain value depending on some conditions. 我有一个名为User的JFrame,在其中声明了一个名为id的变量,并根据某些条件将其设置为某个值。

I need to use this variable in a second JFrame called output. 我需要在另一个名为output的JFrame中使用此变量。

This is the code I have 这是我的代码

class InputScreen extends javax.swing.JFrame {
public int id = 0;

private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
    if (condition){
        id = 1;
        System.out.println(id);
    }
    elseif (condition){
             id = 2;
             System.out.println(id);
    }
    elseif (condition){
             id = 3;
             System.out.println(id);
    }
    else{
    System.exit(0);
    }

I used a constructor in the frame Output but it doesn't seem to work. 我在Output框架中使用了一个构造函数,但它似乎不起作用。

public class Output extends javax.swing.JFrame {
int rule;

public Output(int Id){
    rule = Id;
    initComponents();
}

public Output() {
    initComponents();
    conn = MySqlConnect.ConnectDB();
}

Updated Code Frame - Input 更新的代码框架-输入

class InputScreen extends javax.swing.JFrame {
public int id = 0;

public int getID(){
    return input_rule_id;
}

private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
    if (condition){
        id = 1;
        System.out.println(id);
    }
    elseif (condition){
             id = 2;
             System.out.println(id);
    }
    elseif (condition){
             id = 3;
             System.out.println(id);
    }

Form - Output 表格-输出

private void formWindowActivated(java.awt.event.WindowEvent evt)       {                                     

    Input InSc = new Input();
    InSc.getId();

 }

All primitive data-types will be passed by the value. 该值将传递所有原始数据类型。 Use an object wrapper to pass the value by the reference. 使用对象包装器通过引用传递值。 For example AtomicInteger 例如AtomicInteger

class InputScreen extends javax.swing.JFrame {
    private AtomicInteger id = new AtomicInteger(0);

private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
    if (condition){
        id.set(1);
        System.out.println(id);
    }
    else if (condition){
             id.set(2);
             System.out.println(id);
    }
    else if (condition){
             id.set(3);
             System.out.println(id);
    }
      else{
      System.exit(0);
    }
}

public class Output extends javax.swing.JFrame {
AtomicInteger rule;

public Output(AtomicInteger Id){
    rule = Id;
    initComponents();
}

public Output() {
    initComponents();
    conn = MySqlConnect.ConnectDB();
}
}

Parameter passing The most simple way would be passing Output as a parameter to InputScreen . 参数传递最简单的方法是将Output作为参数传递给InputScreen Then you might just call a method Output.setId(id) within your InputScreen.submitButtonActionPerformed() logic. 然后,您可以只在InputScreen.submitButtonActionPerformed()逻辑中调用方法Output.setId(id) This way however is a bit problematic, when the application grows. 但是,当应用程序增长时,这种方式会出现问题。

Observer Pattern A better way would be realizing a IdChangedListener as part of an Observer pattern as a proxy to the ActionListener you already have implemented. 观察者模式更好的方法是将IdChangedListener作为观察者模式的一部分实现为已实现的ActionListener的代理。 When your ActionListener fires, you call all IdChangeListeners that have registered as Observers to InputScreen . 当ActionListener触发时,您会将所有已注册为Observers的IdChangeListener调用到InputScreen In Output you provide a method setId(id) to provide access. 输出中 ,提供方法setId(id)提供访问权限。 In the place you instantiate the two JFrames you implement an IdChangeListener , add it to InputScreen and when it fires you call Output.setId() . 在实例化两个JFrame的地方 ,实现一个IdChangeListener ,将其添加到InputScreen,并在激发时调用Output.setId()

Anyway, you see a lot of work (and code) for just one event. 无论如何,对于一个事件,您会看到很多工作(和代码)。 Also this approach has its limitations concerning application size and dynamic. 同样,这种方法在应用程序大小和动态性方面也有其局限性。

In Memory EventBus The up-to-date approach would be using a in memory EventBus. 内存中的EventBus最新方法是使用内存中的EventBus。 This eliminates the hard-wiring of the components within your UI and reduces the code size. 这消除了UI中组件的繁琐连接,并减少了代码大小。 However, it limits the reuse of components because they will be automatically react to some events. 但是,它限制了组件的重用,因为它们将对某些事件自动做出反应。 What if you have them more than once? 如果您不止一次拥有它们怎么办? Listening to different Events? 听不同的事件?

You need to think about where you want to use which approach. 您需要考虑在哪里使用哪种方法。 I would suggest using an EventBus for event propagation between the concrete components of your application. 我建议使用EventBus在应用程序的具体组件之间进行事件传播。 Use Observer pattern on medium size components while use parameter passing for small size or very static ones. 在中等大小的组件上使用观察者模式,而对于小尺寸或非常静态的组件使用参数传递。

You can use getters and setter. 您可以使用getter和setter。 Define a getter method in your Frame from where you want to pass the id variable. 在您要传递id变量的位置的Frame中定义一个getter方法。 eg 例如

public int getID(){
      return id;
}

In your other frame you can just use this method to return the result. 在其他框架中,您可以仅使用此方法返回结果。 int id = getID();

Another work around for this problem is making the variable globe and static so it can be imported in the other frame and be used. 解决此问题的另一种方法是使变量globe和static可变,以便可以将其导入其他框架并使用。

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

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