Swing
Panels, buttons and events
Add buttons to a panel
A frame with two buttons
Common constructors of the JButton class
Constructor | Description |
---|---|
JButton() | Creates a button with no text. |
JButton(String) | Creates a button with the text specified by the string. |
Common methods of the JButton class
Method | Description |
---|---|
setText(String) | Sets the text of the button to the specified string. |
getText() | Returns a String object for the text of this button. |
A JPanel class with two buttons
class FutureValuePanel extends JPanel { private JButton calculateButton; private JButton exitButton; public FutureValuePanel() { calculateButton = new JButton("Calculate"); this.add(calculateButton); exitButton = new JButton("Exit"); this.add(exitButton); } }
A frame constructor that adds the panel to the frame
class FutureValueFrame()
{
setTitle("Future Value Calculator");
setSize(267, 200);
centerWindow(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new FutureValuePanel();
this.add(panel);
}