2010-11-28 90 views
0

首先,請放心,我已閱讀每篇Oracle Java文章&教程約6次,所以如果您可以提供幫助,則必須比提供指向Oracle頁面的鏈接更好。對不起,如果這聽起來粗魯。如何獲得一個字符串以顯示在jLabel中?

顯然我不知道字符串是如何工作的。我試圖讓4個jButtons在推送時將它們的值發送到一個字符串,這樣只有最後一個按鈕被按下才會將它的(名稱,文本,任何作品)記錄在一個字符串或值中,以便我可以將對jLabel的消息的價值。

所以這裏有兩個問題,我不能把「狀態」按鈕設置正確,然後我需要能夠將值放入jLabel中。

你可以看到我通過看這個截圖貼: http://krisbunda.com/gui2.png

看到它說「空」的,而不是4個「狀態」按鈕(「已裝船」 1的值或者「加載「等)

private void shippedButtonHandler(java.awt.event.ActionEvent evt) {          
    // TODO add your handling code here: 
    StringBuilder status = new StringBuilder(loadedButton.getText()); 
}          

private void loadedButtonHandler(java.awt.event.ActionEvent evt) {          
    // TODO add your handling code here: 
    StringBuilder status = new StringBuilder(loadedButton.getText()); 
}          

private void outReadyButtonHandler(java.awt.event.ActionEvent evt) {          
    // TODO add your handling code here: 
    StringBuilder status = new StringBuilder(outsideReadyButton.getText()); 
}          

private void outNotReadyButtonHandler(java.awt.event.ActionEvent evt) {           
    // TODO add your handling code here: 
    StringBuilder status = new StringBuilder(outsideNotReadyButton.getText()); 
}           

private void enterButtonHandler(java.awt.event.ActionEvent evt) { 
    // TODO add your handling code here: 
    Confirmation.setVisible(true); 
    Confirmation.setBounds(300,200,900,400); 
    Confirmation.setModal(rootPaneCheckingEnabled); 
    confirmationLabel.setText("You are about to send this message:" 
      + "\n #" + display.getText()+ " is currently " + status); 
} 

private void confirmationNoButtonHandler(java.awt.event.ActionEvent evt) { 
    // TODO add your handling code here: 
    Confirmation.dispose(); 
} 

任何幫助,非常感謝。

編輯:

感謝您的幫助,這是我想出了「新線」問題的解決方法:我用2周的JLabel。 GUI for JLabel new line

和編碼就像這樣:

private void enterButtonHandler(java.awt.event.ActionEvent evt) {          
    // TODO add your handling code here: 
    Confirmation.setVisible(true); 
    Confirmation.setBounds(200,300,1000,400); 
    Confirmation.setModal(rootPaneCheckingEnabled); 
    confirmationLabel1.setText("You are about to send this message:"); 
    confirmationLabel2.setText("PS# " + display.getText()+ " is currently " + status); 
}         

,這裏是一個代碼片段,以說明「氣墊船充滿鰻魚的回答說:」修復:

1)添加非本地變量/串:

public class PlanterStatusUI extends javax.swing.JFrame 
    { 
    /** Creates new form PlanterStatusUI */ 
    public PlanterStatusUI() { 
     initComponents(); 
    } 

    public String status = new String(); { 
    } 

2.)改變按鈕的代碼:

private void shippedButtonHandler(java.awt.event.ActionEvent evt) {          
    // TODO add your handling code here: 
    status = shippedButton.getText(); 
}          

回答

1

上述方法中的狀態變量都在方法中聲明,因此只在方法中可見。方法結束後,變量就會消失。最好更改在類中聲明的StringBuilder變量。

舉例來說,我會做狀態的String類字段/變量,而不是StringBuilder的,而不是一個局部變量,並改變你的按鈕處理方法,像這樣:

private void outReadyButtonHandler(java.awt.event.ActionEvent evt) { 
     status = outsideReadyButton.getText(); 
    }          
+0

剛剛運行該程序,它的工作!謝謝您的幫助!還有一個問題,如果你還在那裏 - 我該如何設置一個「新線」? 「\ n」不適用於我... – Kris 2010-11-28 03:34:06

0

example說明如何更新每次按一個按鈕時都會有一個標籤。

相關問題