This is just an example of a very basic Java Converter using GUI.
/* * Simple Converter suitable for anything * demonstration very basic GUI Java */ /** * * @author bbq */ import java.awt.*; import java.awt.event.*; import javax.swing.*; public class BasicConverter extends JFrame implements ActionListener { // Constructor // public BasicConverter(String windowTitle) { // begin setting up properties of our main // // JFrame // super(windowTitle); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setLayout( new BorderLayout() ); // user card layout so we have more options this.setSize(300,110); // call our function to init the components // this.initComponents(); } // function to initialise all our componenets // // to be used on JFrame // private void initComponents() { // firstly instantiate our components // // Panels Firstly - set their layouts also // this.inputPanel = new JPanel(); this.inputPanel.setLayout(new GridLayout(2,2,5,5)); this.buttonPanel = new JPanel(); this.buttonPanel.setLayout( new FlowLayout()); // labels this.inputLabel = new JLabel("Input"); this.outputLabel = new JLabel("Output"); //fields this.inputField = new JTextField(); this.outputField = new JTextField(); // button - also add ActionListener to 'this' button this.convert = new JButton("Convert"); this.convert.addActionListener(this); /* * now we have instantiated components and set some basic properties * we can now begin adding our components to their panels and then the * panels to our JFrame * **/ // Add components to the JPanel inputPanel // // order is important because we are using GridLayout // this.inputPanel.add( this.inputLabel ); this.inputPanel.add( this.inputField ); this.inputPanel.add( this.outputLabel ); this.inputPanel.add( this.outputField ); // Add components to the JPanel buttonPanel // // add it so it aligns in the center // this.buttonPanel.add( this.convert ); // Now we can add our two panels to the main JFrame // this.add( this.inputPanel, BorderLayout.NORTH ); // places inputPanel at top this.add( this.buttonPanel, BorderLayout.SOUTH ); // places buttonPanel at bottom } private void convertInputValue() { // we will simply square the current input value // // you could do any calculation you wish // double input = 0; try { // simply get the input from the input field and parse // it to something we want. In this case we want a double input = Double.parseDouble( this.inputField.getText() ); } catch (Exception e) { this.inputField.setText( "Error " + e.getMessage() ); return; } // if exception caught tell user what happened and return // // square the input we received input = input * input; // update the output field accurate to 2 decimal places // this.outputField.setText( String.format("%.2f", input) ); } // override functoin for our ActionListener public void actionPerformed(ActionEvent e) { // this function will be called for all objects we // // addActionListener to, in this example we have // // only done that for the button convert. so we do // // as follows. // if (e.getSource() == this.convert) { // we call our convertInputValue here // // alternatively we could do our code in here, if // // it is as small as this. // this.convertInputValue(); } } // Main Function public static void main(String[] args) { // this will call constructor and begin building the application frame // BasicConverter myConverter = new BasicConverter("Basic Converter - bbq"); myConverter.setVisible(true); // frame is ready to show once constructor is complete // show it } // Components needed private JPanel inputPanel; private JPanel buttonPanel; private JLabel inputLabel; private JLabel outputLabel; private JTextField inputField; private JTextField outputField; private JButton convert; }
