如何在jlabel中出现一根字符串?
题
首先,请放心,我现在阅读了大约6次Oracle Java文章和教程,因此,如果您可以提供帮助,则必须比提供指向Oracle页面的链接更好。抱歉,这听起来不礼貌。
显然我不知道字符串是如何工作的。我正在尝试获得4个jbutton,以将其值发送到字符串,以便只有被按下的最后一个按钮才能在字符串或值中记录其(名称,文本,任何作品),以便我可以串通重视Jlabel的消息。
因此,这里有2个问题,我一定不能正确设置“状态”按钮,然后我需要能够将值拿到Jlabel中。
您可以通过查看此屏幕截图来查看我的位置:http://krisbunda.com/gui2.png
查看它说“ null”的位置,而不是4个“状态”按钮的值(“运送”或“已加载”等)。
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个Jlabels。
并像这样编码:
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();
}
解决方案
以上方法中的状态变量全部在方法中声明,因此仅在方法中可见。该方法结束后,变量将立即消失。最好更改类本身声明的StringBuilder变量。
例如,我将状态成为类字符串字段/变量,而不是字符串构造器,而不是本地变量,然后更改您的按钮处理程序方法:
private void outReadyButtonHandler(java.awt.event.ActionEvent evt) {
status = outsideReadyButton.getText();
}
其他提示
这 例子 显示如何单击按钮时更新标签。
不隶属于 StackOverflow