簡體   English   中英

將一個Jframe中設置的變量從一個傳遞到另一個

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

我有一個名為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);
    }

我在Output框架中使用了一個構造函數,但它似乎不起作用。

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

 }

該值將傳遞所有原始數據類型。 使用對象包裝器通過引用傳遞值。 例如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();
}
}

參數傳遞最簡單的方法是將Output作為參數傳遞給InputScreen 然后,您可以只在InputScreen.submitButtonActionPerformed()邏輯中調用方法Output.setId(id) 但是,當應用程序增長時,這種方式會出現問題。

觀察者模式更好的方法是將IdChangedListener作為觀察者模式的一部分實現為已實現的ActionListener的代理。 當ActionListener觸發時,您會將所有已注冊為Observers的IdChangeListener調用到InputScreen 輸出中 ,提供方法setId(id)提供訪問權限。 在實例化兩個JFrame的地方 ,實現一個IdChangeListener ,將其添加到InputScreen,並在激發時調用Output.setId()

無論如何,對於一個事件,您會看到很多工作(和代碼)。 同樣,這種方法在應用程序大小和動態性方面也有其局限性。

內存中的EventBus最新方法是使用內存中的EventBus。 這消除了UI中組件的繁瑣連接,並減少了代碼大小。 但是,它限制了組件的重用,因為它們將對某些事件自動做出反應。 如果您不止一次擁有它們怎么辦? 聽不同的事件?

您需要考慮在哪里使用哪種方法。 我建議使用EventBus在應用程序的具體組件之間進行事件傳播。 在中等大小的組件上使用觀察者模式,而對於小尺寸或非常靜態的組件使用參數傳遞。

您可以使用getter和setter。 在您要傳遞id變量的位置的Frame中定義一個getter方法。 例如

public int getID(){
      return id;
}

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

解決此問題的另一種方法是使變量globe和static可變,以便可以將其導入其他框架並使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM