如何让字符串显示在 jLabel 中?
首先,请放心,我已经阅读了所有 Oracle Java 文章和文章。教程现在大约有 6 次,所以如果您能提供帮助,您必须做得比提供 Oracle 页面的链接更好。抱歉,如果这听起来很粗鲁。
显然我不知道字符串是如何工作的。我试图让 4 个 jButton 在按下时将其值发送到字符串,这样只有最后按下的按钮才会将其(名称、文本、任何有效的内容)记录在字符串或值中,以便我可以将其连接起来jLabel 消息的值。
所以这里有两个问题,我一定没有正确设置“状态”按钮,然后我需要能够获取该值并将其放入 jLabel 中。
通过查看此屏幕截图,您可以看到我陷入困境的地方: http://krisbunda.com/gui2.png
查看哪里显示“null”而不是值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 个 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);
}
这里有一段代码来说明“Hovercraft full of eels":
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();
}
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. Sorry if that sounds rude.
Apparently I don't know how Strings work. 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.
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.
You can see where I'm stuck by looking at this screenshot:
http://krisbunda.com/gui2.png
See where it says "null" instead of the value of 1 of 4 "status" buttons ("Shipped" or "Loaded", etc.)
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.
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:
public class PlanterStatusUI extends javax.swing.JFrame
{
/** Creates new form PlanterStatusUI */
public PlanterStatusUI() {
initComponents();
}
public String status = new String(); {
}
2.) change button code:
private void shippedButtonHandler(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
status = shippedButton.getText();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
上述方法中的状态变量都是在方法中声明的,因此仅在方法中可见。方法结束后,变量就会消失。最好更改在类本身中声明的 StringBuilder 变量。
例如,我会将 status 设置为类 String 字段/变量,而不是 StringBuilder,也不是局部变量,然后更改按钮处理程序方法,如下所示:
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.
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:
此示例展示了如何在每次单击按钮时更新标签。
This example shows how to update a label each time a button is clicked.