javaswinguser-interface

Text not appearing on TextField for GUI Calculator?


I've been making a GUI calculator, but I've run into a ditch. I know my code is only restricted to the '2' button, but that's because I've cut out all the numbers so the code can be shorter to be put on here. When I press two, I know the Actionlistener is activated because the System.out.println will print out 2. But it won't appear on my Text Field, where did I go wrong?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.Random;
import java.awt.FlowLayout;

public class CalculatorY {
    public static  JPanel panel = new JPanel(new GridLayout(5, 5));
    public static JButton one, two, three, four, five, six, seven, eight, nine, zero, equal;
    public static JTextField result;
    public static boolean add, sub, mult, div;

    public static void main(String[] args) {
        JFrame frame = new JFrame("Rohini and Sonika's Calculator");
        result = new JTextField(null,90);
        two = new JButton("2");
        equal = new JButton("=");
        two.addActionListener(new button());
        panel.setLayout(new GridLayout(5, 5, 5, 25));
        panel.setLayout(new FlowLayout());
        panel.add(result, BorderLayout.NORTH);
        panel.add(two);
        frame.setVisible(true);
        frame.setSize(200, 400);
        frame.setResizable(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(panel);
    }

    public static class button implements ActionListener {
        public void actionPerformed(ActionEvent event) {

            String command = event.getActionCommand();
            String text = result.getText();
            int textLength = text.length();
            String letterValueOne = null;
            String letterValueTwo = null;
            boolean operation = false; 
                    if (operation == false) {
                        if (letterValueOne == null) {
                            letterValueOne = "2";   
                            System.out.println(letterValueOne);
                            result.setText(letterValueOne);
                        } else {
                            letterValueOne = letterValueOne + "2";
                            result.setText(letterValueOne);
                        }
                    } else {
                        if (letterValueTwo == null) {
                            letterValueTwo = "2";
                        } else {
                            letterValueTwo = letterValueTwo + "2";
                        }
                    }
        }
    }
}

Solution

  • It does put a 2 in the text field! You can see this by clicking in the text-field, pressing Ctrl-A, then Ctrl-C, then pasting somewhere else.

    new JTextField(null, 90) creates a text field that is wide enough to hold 90 characters, but this is wider than the frame (at least on my system) so it gets cut off at the sides. If you maximize the frame, you should see the 2.

    Some possible solutions are: