Changethe Label of a JButton Component in Java
Description
The following code shows how to changethe Label of a JButton Component.
Example
import javax.swing.JButton;
import javax.swing.JFrame;
/*w w w. j av a2 s.co m*/
public class Main extends JFrame {
public Main() {
setSize(300, 150);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JButton button = new JButton("java2s.com");
button.setText("New Label");
// Remove the label; this is useful for a button with only an icon
//button.setText(null);
add(button);
setVisible(true);
}
public static void main(String[] args) {
new Main();
}
}
The code above generates the following result.
Home »
Java Tutorial »
Swing »
Java Tutorial »
Swing »