Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Application extends JFrame {
- private static final long serialVersionUID = 1L;
- private static final String TITLE = "Average Machine";
- private static final int WIDTH = 320;
- private static final int HEIGHT = 125;
- private JPanel panel;
- private JTextField field, infoField;
- private JButton btnAdd, btnClear, btnAvg;
- private JLabel label;
- private int counter;
- private int index = 0;
- private double total = 0;
- private double ans = 0;
- public double temps[] = new double[10000];
- public Application() {
- initComponents();
- initLayout();
- }
- private void initComponents() {
- //Fonts
- Font font0 = new Font("Arial", Font.PLAIN, 11);
- Font font1 = new Font("Arial", Font.PLAIN, 9);
- panel = new JPanel();
- // JButtons
- btnAdd = new JButton("Add");
- btnAdd.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- onClick();
- }
- });
- btnAvg = new JButton("Average");
- btnAvg.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- average();
- }
- });
- btnClear = new JButton("Clear");
- btnClear.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- clear();
- }
- });
- btnAdd.setFont(font0);
- btnAvg.setFont(font0);
- btnClear.setFont(font0);
- //Text Field
- field = new JTextField();
- field.setHorizontalAlignment(SwingConstants.CENTER);
- field.setColumns(38);
- field.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- onClick();
- }
- });
- //Info field
- infoField = new JTextField();
- infoField.setEditable(false);
- infoField.setText("Start completed");
- infoField.setHorizontalAlignment(SwingConstants.CENTER);
- infoField.setColumns(38);
- // Label
- label = new JLabel("All Rights Reserverd - v0.00");
- label.setFont(font1);
- // Adding panel
- add(panel, BorderLayout.CENTER);
- }
- private void onClick() {
- try {
- temps[index] = Double.parseDouble(field.getText());
- counter++;
- total += temps[index];
- infoField.setText(temps[index] + " added. " + "Amount: " + counter + ". Total: " + total);
- System.out.println("Total: " + total);
- System.out.println("Added: " + temps[index]);
- System.out.println("Counter: " + counter);
- System.out.println("Temp[" + index + "]: " + temps[index]);
- System.out.println("Index: " + index + "\n");
- field.setText("");
- field.requestFocusInWindow();
- } catch (NumberFormatException e) {
- infoField.setText("Error: Wrong character format");
- return;
- }
- index++;
- }
- private void clear() {
- field.setText("");
- infoField.setText("Clear completed");
- counter = 0;
- total = 0;
- index = 0;
- System.out.println("Counter: " + counter);
- System.out.println("Index: " + index);
- System.out.println("Total: " + total + "\n");
- }
- private void average() {
- double ans = total / index;
- infoField.setText("Average: " + ans + " | " + total + " / " + index + "");
- counter = 0;
- total = 0;
- index = 0;
- System.out.println("Counter: " + counter);
- System.out.println("Index: " + index);
- System.out.println("Total: " + total + "\n");
- }
- private void initLayout() {
- //Adding all the components to the panel
- panel.add(infoField);
- panel.add(field);
- panel.add(btnAdd);
- panel.add(btnAvg);
- panel.add(btnClear);
- panel.add(label);
- //setting the rules of the JFrame
- setTitle(Application.TITLE);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setResizable(false);
- setSize(WIDTH, HEIGHT);
- setLocationRelativeTo(null);
- setVisible(true);
- field.requestFocusInWindow();
- }
- public static void main(String[] args) {
- try {
- UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
- new Application();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement