import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class NumberModifier {
public static void main(String[] args) {
MyFrame frame = new MyFrame();
}
}
class MyFrame extends JFrame {
public MyFrame() {
applySettings();
addComponents();
setListeners();
...
}
private void applySettings() {
...
}
private void addComponents() {
...
}
private void setListeners() {
...
}
private JButton incButton;
private JButton decButton;
private JTextField step;
private JLabel valueView;
private int value = 0;
private int increment = 1;
}
Description and Objective
- This app modifies a numeric value by adding or subtracting the amount set in a text field (see image above).
Things to Do
- Use the above code as a template.
- Changing settings:
- set the window size
- set the window location
- choose a window layout that would hold the components side by side as seen in the image.
- Adding Components:
- instantiate the increase and decrease buttons, the text field and the label that shows the the value.
- add the components to the frame
- Adding Listeners:
- set a listener for incButton, which adds the increment to the value and updates the valueView.
- set a listener for decButton, which subtracts the increment to the value and updates the valueView.
- set a listener for the step, which should check if the value entered is numeric and assign it to increment if it is. If the input is not numeric set the increment to 1. You should listen for events after the user hits the ENTER key, do not listen to every key stroke.