【问题标题】:Passing variable set in one Jframe from one to another将一个 Jframe 中的变量集从一个传递到另一个
【发布时间】:2016-05-20 06:34:55
【问题描述】:

我有一个名为 User 的 JFrame,我在其中声明了一个名为 id 的变量,并根据某些条件将其设置为某个值。

我需要在名为 output 的第二个 JFrame 中使用这个变量。

这是我的代码

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

我在框架输出中使用了一个构造函数,但它似乎不起作用。

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

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

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

更新代码 框架 - 输入

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

表格 - 输出

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

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

 }

【问题讨论】:

标签: java swing class jframe


【解决方案1】:

所有原始数据类型都将通过值传递。使用对象包装器通过引用传递值。例如原子整数

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();
}
}

【讨论】:

  • 我试过这个,但我似乎只得到 null 作为输出框架中的值。
  • 如果没有看到您的完整代码,我无法帮助您。
  • 我现在已经更新了代码。请看看你是否帮我解决这个问题。
  • 这不是完整的代码。我认为,您需要从一本书开始学习 Java。所以你可以提出我们可以理解的问题。
【解决方案2】:

参数传递 最简单的方法是将 Output 作为参数传递给 InputScreen。然后,您可以在 InputScreen.submitButtonActionPerformed() 逻辑中调用方法 Output.setId(id)。然而,当应用程序增长时,这种方式有点问题。

观察者模式 更好的方法是将 IdChangedListener 实现为 Observer 模式的一部分,作为您已经实现的 ActionListener 的代理。当您的 ActionListener 触发时,您将所有已注册为观察者的 IdChangeListeners 调用到 InputScreen。在 Output 中,您提供了一个方法 setId(id) 来提供访问权限。在您实例化两个 JFrame 的地方,您实现了一个 IdChangeListener,将它添加到 InputScreen 并在它触发时调用 Output.setId()

无论如何,您会看到一个事件的大量工作(和代码)。此外,这种方法在应用程序大小和动态方面也有其局限性。

内存事件总线 最新的方法是使用内存中的 EventBus。这消除了 UI 中组件的硬连线并减少了代码大小。但是,它限制了组件的重用,因为它们会自动对某些事件做出反应。如果你不止一次拥有它们怎么办?收听不同的事件?

您需要考虑要在哪里使用哪种方法。我建议使用 EventBus 在应用程序的具体组件之间传播事件。在中等大小的组件上使用观察者模式,而对小尺寸或非常静态的组件使用参数传递。

【讨论】:

    【解决方案3】:

    您可以使用 getter 和 setter。在您想要传递 id 变量的 Frame 中定义一个 getter 方法。例如

    public int getID(){
          return id;
    }
    

    在您的其他框架中,您可以使用此方法返回结果。 int id = getID();

    解决此问题的另一种解决方法是使变量 globe 和 static 以便可以在其他框架中导入并使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-14
      • 1970-01-01
      • 2018-10-02
      • 2018-01-14
      • 2018-11-04
      • 2013-12-17
      相关资源
      最近更新 更多