Created
February 19, 2017 04:57
-
-
Save tommy-mor/251644feb94f051d094f9d3e64ffc6d3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.*; // Using AWT's layouts | |
import java.awt.event.*; // Using AWT's event classes and listener interfaces | |
import java.math.BigDecimal; | |
import javax.swing.*; // Using Swing's components and container | |
import java.util.function.BiFunction; | |
// A Swing application extends from javax.swing.JFrame | |
/* Need Javadoc documentation here | |
* | |
* | |
*/ | |
@SuppressWarnings("serial") | |
public class SwingAdder extends JFrame | |
{ | |
private JTextField tfNumber1, tfNumber2; | |
private JLabel resultShow; | |
private JButton btnAdd, btnClear, btnMinus, btnMultiply, btnDevide, btnMod; | |
private int number1, number2, result; | |
// Constructor to set up UI components and event handlers | |
public SwingAdder() { | |
// Swing components should be added to the content-pane of the JFrame. | |
Container cp = getContentPane(); | |
// Set this Container to grid layout of 4 rows and 2 columns | |
cp.setLayout(new GridLayout(6, 2, 10, 3)); | |
cp.add(new JLabel("First Number ")); // at (1, 1) | |
tfNumber1 = new JTextField(10); | |
tfNumber1.setHorizontalAlignment(JTextField.RIGHT); | |
cp.add(tfNumber1); // at (1, 2) | |
cp.add(new JLabel("Second Number ")); | |
tfNumber2 = new JTextField(10); | |
tfNumber2.setHorizontalAlignment(JTextField.RIGHT); | |
cp.add(tfNumber2); | |
btnAdd = new JButton("+"); | |
cp.add(btnAdd); | |
btnAdd.addActionListener(newListener((a,b) -> a + b)); | |
btnClear = new JButton("CLEAR"); | |
cp.add(btnClear); | |
btnDevide = new JButton("/"); | |
cp.add(btnDevide); | |
btnDevide.addActionListener(newListener((a,b) -> a.doubleValue() / b.doubleValue())); | |
btnMinus = new JButton("-"); | |
cp.add(btnMinus); | |
btnMinus.addActionListener(newListener((a,b) -> a - b)); | |
btnMultiply = new JButton("*"); | |
cp.add(btnMultiply); | |
btnMultiply.addActionListener(newListener((a,b) -> a * b)); | |
btnMod = new JButton("rem"); | |
cp.add(btnMod); | |
btnMod.addActionListener(newListener((a,b) -> a % b)); | |
resultShow = new JLabel("--"); | |
btnClear.addActionListener(new ActionListener() | |
{ | |
@Override | |
public void actionPerformed(ActionEvent evt) | |
{ | |
tfNumber1.setText(""); | |
tfNumber2.setText(""); | |
} | |
}); | |
JLabel resultlabel = new JLabel("Result: "); | |
cp.add(resultlabel); | |
cp.add(resultShow); | |
// for the "window-close" button | |
setDefaultCloseOperation(EXIT_ON_CLOSE); | |
setTitle("Swing Adder"); | |
setSize(300, 170); | |
setVisible(true); | |
} | |
private int[] parseInputs() { | |
try { | |
number1 = Integer.parseInt(tfNumber1.getText()); | |
number2 = Integer.parseInt(tfNumber2.getText()); | |
} catch(NumberFormatException ex) { | |
JOptionPane.showMessageDialog(tfNumber1.getParent(), | |
"Please enter a valid integer, not" + ex.getMessage().split(":")[1]); | |
} | |
return new int[]{number1, number2}; | |
} | |
private ActionListener newListener(BiFunction<Integer,Integer,Number> op) { | |
return new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent evt) { | |
int[] inputs = parseInputs(); | |
resultShow.setText("" + op.apply(inputs[0], inputs[1])); | |
} | |
}; | |
} | |
// The entry main() method | |
public static void main(String[] args) { | |
// For thread safety, use the event-dispatching thread to construct UI | |
javax.swing.SwingUtilities.invokeLater(new Runnable() | |
{ | |
@Override | |
public void run() { | |
new SwingAdder(); // Let the constructor do the job | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment