import java.awt.Dimension; import javax.swing.JButton; public class MyButton extends JButton { public MyButton(String label) { super(label); setMaximumSize(new Dimension(100, 50)); } }
import java.awt.Dimension; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class MyFrame extends JFrame { public MyFrame() { JPanel panel = new JPanel(); JButton button = new JButton("Click me!"); button.setMaximumSize(new Dimension(150, 75)); panel.add(button); add(panel); pack(); setVisible(true); } }In this example, a JFrame is created with a JPanel added to it. A JButton component is created with the label "Click me!" and its maximum size is set to 150 pixels wide and 75 pixels high using the setMaximumSize method. The button is then added to the JPanel and the JFrame is packed and made visible. Package library: java.awt.