![](/img/trans.png)
[英]How to change button text color and background color while button is pressed
[英]how to change the text of a button once that button is pressed
我按下连接按钮后有一个名为 Connect 的按钮,我希望该按钮的文本断开连接。 因此,当用户启动应用程序时,它将被连接,一旦单击按钮,当用户想要关闭应用程序时,它将断开连接。
有可能做到这一点吗?
嗨,下面的代码片段对你有用
connectBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
connectBtn.setText("Dissconnect");
}
});
connectBtn
是您的按钮组件的实例
Netbeans 非常直观且易于使用...试试这个:
private javax.swing.JButton jButton2;
jButton2 = new javax.swing.JButton();
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
jButton2.setText("Hello");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.