JButton btn = new JButton("Click me"); Dimension preferredSize = btn.getPreferredSize(); System.out.println("Preferred size: " + preferredSize);
JButton btn1 = new JButton("OK"); JButton btn2 = new JButton("Cancel"); Dimension maxSize = new Dimension(100, 50); btn1.setMaximumSize(maxSize); btn2.setMaximumSize(maxSize);In this example, we create two JButtons with the text "OK" and "Cancel". We then create a new Dimension object with a maximum size of 100 by 50 pixels. We set this maximum size for both buttons using the setMaximumSize() method. This ensures that both buttons will have the same size and will not exceed the maximum size. Package library: java.awt.