package org.kodejava.swing;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import java.awt.FlowLayout;
public class ButtonImageExample extends JFrame {
public ButtonImageExample() {
initComponents();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new ButtonImageExample().setVisible(true));
}
private void initComponents() {
setTitle("My Buttons");
setSize(500, 500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER));
// Creates two JButton object with an images to display. The image can be
// a gif, jpeg, png and some other type supported. And we also set the
// mnemonic character of the button for short-cut key.
JButton okButton = new JButton("OK", new ImageIcon(
this.getClass().getResource("/images/ok.png")));
okButton.setMnemonic('O');
JButton cancelButton = new JButton("Cancel", new ImageIcon(
this.getClass().getResource("/images/cancel.png")));
cancelButton.setMnemonic('C');
getContentPane().add(okButton);
getContentPane().add(cancelButton);
}
}
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024