gpt4 book ai didi

java - 如何从另一种方法访问 JLabel?

转载 作者:行者123 更新时间:2023-12-01 12:27:08 24 4
gpt4 key购买 nike

我正在开发一个 GUI 应用程序,我希望我的代码做的是,单击您在基于 swing 的 GUI 中可以感知到的唯一按钮后,其中存在的 2 个 jLabels 使字符串“Working”可感知给用户。这是我的代码:

public class DiligentGUI extends javax.swing.JFrame {

public DiligentGUI() {
initComponents();
}
void a()
{
fahrenheitLabel.setText("Working");
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

tempTextField = new javax.swing.JTextField();
celsiusLabel = new javax.swing.JLabel();
convertButton = new javax.swing.JButton();
fahrenheitLabel = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Celsius Converter");

tempTextField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
tempTextFieldActionPerformed(evt);
}
});

celsiusLabel.setText("Celsius");

convertButton.setText("Convert");
convertButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
convertButtonActionPerformed(evt);
}
});

fahrenheitLabel.setText("Fahrenheit");

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

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(tempTextField,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(12, 12, 12)
.addComponent(celsiusLabel))
.addGroup(layout.createSequentialGroup()
.addComponent(convertButton)


.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(fahrenheitLabel)))
.addContainerGap(59, Short.MAX_VALUE))
);

layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {convertButton, tempTextField});

layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(tempTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(celsiusLabel))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(convertButton)
.addComponent(fahrenheitLabel))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);

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

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

private void convertButtonActionPerformed(java.awt.event.ActionEvent evt) {

DiligentGUI b = new DiligentGUI();
b.a();
celsiusLabel.setText("Working");
}

/**
* @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(DiligentGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(DiligentGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(DiligentGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(DiligentGUI.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 DiligentGUI().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JLabel celsiusLabel;
private javax.swing.JButton convertButton;
private javax.swing.JLabel fahrenheitLabel;
private javax.swing.JTextField tempTextField;
// End of variables declaration

}

如您所见,其中有 2 个 jLabel:fahrenheitLabel 和 celsiusLabel。我创建了一个名为“a”的辅助方法,它将 fahrenheitLabel 的状态更改为“正在工作”。我无法实现所需的输出。有什么建议吗?

最佳答案

这不会发生,因为您正在 convertButtonActionPerformed 方法中创建 DiligentGUI 类的另一个对象。这将创建另一个 JFrame。您看不到它,因为您没有通过 d.setVisible(true) 将其设置为可见。

在您的

中进行此更改
private void convertButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              

a(); // This will call the a() method in the current running instance.
celsiusLabel.setText("Working");
}

关于java - 如何从另一种方法访问 JLabel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26241615/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com