2013-07-05 18 views
0

寫在textarea的GUI答案我有一些問題就早了哪些這裏 解決了,但現在我的問題是,我得把這個代碼,並得到 出來的結果在GUI textarea的這個基本代碼。到目前爲止所做的一切是,第一個 設計了一個textArea和一個按鈕,但現在我卡住了。我不知道如何 將我用作基礎的代碼翻譯成GUI,並使其可能 單擊該按鈕並將答案顯示在文本顯示中。我 不知道我應該複製這個基地代碼或我需要做 在GUI內完全不同的東西?例如根據 jButtonActionPerformed,我需要做什麼...即時對不起,即時通訊新的 這個,所以失去了。我一整天都在搜索,但很難理解。如何從基本代碼

這是基礎代碼:

import java.lang.String; 
class Vara {    


    //Deklarerar variabler 

    private String name; 
    private double price; 
    private int antal; 

    //tildela konstruktorer för de deklarerade variablerna 
    public Vara (String name, int antal, double price) { 
     this.name = name; 
     this.antal = antal; 
     this.price = price; 
    } // slut constructor 

    public void setName(String name) { 
     this.name = name; } 

    public void setPrice (double price) { 
     this.price = price; } 

    public void setAntal (int antal) { 
     this.antal = antal; } 

    public String getName() {  
     return this.name;} 

    public double getPrice() { 
     return this.price; } 

    public int getAntal() {  
     return this.antal; }  
} 

//testklassen ska stå som en egen klass 
    class Test { 
     public static void main(String[] args){ 
     Vara var = new Vara("Banan",5, 12.5); 


System.out.println("Namnet är " +var.getName() +" och priset är " +var.getPrice() +" och antalet är "+var.getAntal());// här slutar system.out 


} 
} 

這裏是我的GUI代碼,我還沒有做任何事情迄今除了創造一個文本區域和一個按鈕開始。

public class NewJFrame extends javax.swing.JFrame { 

    /** 
    * Creates new form NewJFrame 
    */ 
    public NewJFrame() { 
     initComponents(); 
    } 

    /** 
    * This method is called from within the constructor to initialize the form. 
    * WARNING: Do NOT modify this code. The content of this method is always 
    * regenerated by the Form Editor. 
    */ 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code"> 
    private void initComponents() { 

     jScrollPane1 = new javax.swing.JScrollPane(); 
     jTextArea1 = new javax.swing.JTextArea(); 
     jButton1 = new javax.swing.JButton(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     jTextArea1.setColumns(20); 
     jTextArea1.setRows(5); 
     jScrollPane1.setViewportView(jTextArea1); 

     jButton1.setText("jButton1"); 
     jButton1.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jButton1ActionPerformed(evt); 
      } 
     }); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addContainerGap(29, Short.MAX_VALUE) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
         .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE) 
         .addGap(69, 69, 69)) 
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
         .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 361, javax.swing.GroupLayout.PREFERRED_SIZE) 
         .addContainerGap()))) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 231, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
       .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addContainerGap(17, Short.MAX_VALUE)) 
     ); 

     pack(); 
    }// </editor-fold> 

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 
     txtDisplay.setText(null); 
     txtDisplay.append(String.valueOf(String)+"\n"); 
     // TODO add your handling code here: 
    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) { 
     /* 
     * Set the Nimbus look and feel 
     */ 
     //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
     /* 
     * If Nimbus (introduced in Java SE 6) is not available, stay with the 
     * default look and feel. For details see 
     * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */ 
     try { 
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
       if ("Nimbus".equals(info.getName())) { 
        javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
        break; 
       } 
      } 
     } catch (ClassNotFoundException ex) { 
      java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 
     //</editor-fold> 

     /* 
     * Create and display the form 
     */ 
     java.awt.EventQueue.invokeLater(new Runnable() { 

      public void run() { 
       new NewJFrame().setVisible(true); 
      } 
     }); 
    } 
    // Variables declaration - do not modify 
    private javax.swing.JButton jButton1; 
    private javax.swing.JScrollPane jScrollPane1; 
    private javax.swing.JTextArea jTextArea1; 
    // End of variables declaration 
} 

回答

0

在GUI類中設置修飾符爲「Public Static」,以便可以從其他類訪問它。

public static javax.swing.JTextArea jTextArea1; 

然後你可以在其他類中的jTextArea1中輸入Value。通過直接訪問這樣

NewJFrame.jTextArea1.append("text to append"); 
0

從你的問題,如果你希望把數據導入到JTextArea或閱讀在JTextArea輸入內容,以存儲到其他地方並不明顯。我會回答這兩種情況。

連接數據和GUI的優雅方式是將數據模型對象(在您的案例中是類Vara的一個實例)插入到GUI對象中。例如。這可以在創建GUI對象時完成,如下面的代碼所示。我在代碼中用「// CHANGED:...」評論了每一行。

public class NewJFrame extends javax.swing.JFrame { 

     // CHANGED: store the model for this GUI 
     private Vara vara; 

     /** 
     * Creates new form NewJFrame 
     */ 
     public NewJFrame(Vara vara) { // CHANGED: provide the model for this GUI 
     this.vara = vara; // CHANGED: store the model for this GUI 
     initComponents(); 

     // CHANGED: set TextArea content according to the data in the 
     // provided model object (if this is what you want) 
     jTextArea1.setText(vara.getName()); 
    } 

    ... 
    ... 
    ... 

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 
     txtDisplay.setText(null); 
     txtDisplay.append(String.valueOf(String)+"\n"); 

     // CHANGED: read the entered text from the TextArea and store it as the 
     // new name of the Vara object (if this is what you want to happen when 
     // the user clicks on the button) 
     vara.setName(jTextArea1.getText()); 
    } 

    ... 
    ... 
    ... 

     // Variables declaration - do not modify 
    private javax.swing.JButton jButton1; 
    private javax.swing.JScrollPane jScrollPane1; 
    private javax.swing.JTextArea jTextArea1; 
     // End of variables declaration 
} 

一個更好的程序員的生活的一些技巧:

  • 使用英語無處不在,也是在評論

  • 始終命名GUI元素合理的,例如您jButton1可能是btnSave(「BTN」代表按鈕)和你的jTextArea1可能是taVaraName(「TA」代表文本區) - 這大大增加你的GUI代碼的可讀性,使調試和維護更容易

  • 使用的Eclipse用於Java開發,尤其是Java開發。如果你想構建GUI。它帶有一個名爲Eclipse WindowBuilder的插件。這是「一個功能強大且易於使用的雙向Java GUI設計器」,意味着您可以:(1)輕鬆拖放GUI元素並獲取爲您自動生成的代碼; (2)修改代碼的任何部分並查看自動更改的拖放視圖。它會生成比您發佈的GUI代碼更乾淨,更短的GUI代碼,並允許您修改所有內容(不包含代碼中的「不修改」部分)