简体   繁体   English

如何使字符串显示在jLabel中?

[英]How do I get a String to show up in a jLabel?

First off, rest assured that I've read every Oracle Java article & tutorial about 6 times now, so if you can help, you'll have to do better than providing a link to an Oracle page. 首先,请放心,我现在已经阅读了每本Oracle Java文章和教程大约6次,因此,如果您可以提供帮助,那么比提供指向Oracle页面的链接要好得多。 Sorry if that sounds rude. 抱歉,这听起来很粗鲁。

Apparently I don't know how Strings work. 显然我不知道Strings是如何工作的。 I'm trying to get 4 jButtons to send their value to a string when pushed, so that only the last button that was pushed will record its (name, text, whatever works) in a string or value, so that I can concatenate that value to a jLabel's message. 我试图让4个jButton在按下时将它们的值发送到字符串,以便只有最后按下的按钮才能将其(名称,文本或其他有用的东西)记录在字符串或值中,以便我可以将其串联起来jLabel消息的值。

So there's 2 problems here, I must not have the "Status" buttons set up right, and then I need to be able to take the value and put it in the jLabel. 所以这里有2个问题,我必须没有正确设置“状态”按钮,然后我需要能够获取该值并将其放在jLabel中。

You can see where I'm stuck by looking at this screenshot: http://krisbunda.com/gui2.png 通过查看以下屏幕快照,您可以了解我的位置: http : //krisbunda.com/gui2.png

See where it says "null" instead of the value of 1 of 4 "status" buttons ("Shipped" or "Loaded", etc.) 查看显示“ null”而不是4个“ status”按钮中的1个值的位置(“ Shipped”或“ Loaded”等)

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

Any help is greatly appreciated. 任何帮助是极大的赞赏。

EDIT: 编辑:

Thanks for the help, here's the workaround I figured out for the "new line" issue: I used 2 JLabels. 感谢您的帮助,这是我为“新行”问题找到的解决方法:我使用了2个JLabel。 JLabel新行的GUI

and coded like so: 并像这样编码:

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

and here's a snippet of code to illustrate the fix answered by "Hovercraft full of eels": 这是一段代码,以说明“充满鳗鱼的气垫船”所解决的问题:

1.) add non-local variable/string: 1.)添加非局部变量/字符串:

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

    public String status = new String(); {
    }

2.) change button code: 2.)更改按钮代码:

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

The status variable in the methods above are all declared in the method and thus visible only in the method. 上面方法中的状态变量都在方法中声明,因此仅在方法中可见。 The variables will disappear as soon as the method is over. 方法结束后,变量将消失。 Better to change a StringBuilder variable that is declared in the class itself. 最好更改在类本身中声明的StringBuilder变量。

For instance, I'd make status a class String field/variable, not StringBuilder, and not a local variable, and change your button handler method like so: 例如,我将状态设为String字段/变量类,而不是StringBuilder,而不是局部变量,并按如下方式更改按钮处理程序方法:

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

示例说明了每次单击按钮时如何更新标签。

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

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