Jump to content

How to use jbuttons to input to a text field in java?

Too difficult to read without indentation. Use the code button (the "<>" thing near the quote button and choose the C option) to write the java in like this:

 

Public class Example(){
  private example; 
}

 

If you want to reply back to me or someone else USE THE QUOTE BUTTON!                                                      
Pascal laptops guide

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, Castdeath97 said:

Too difficult to read without indentation. Use the code button (the "<>" thing near the quote button and choose the C option) to write the java in like this:

 


Public class Example(){
  private example; 
}

 

ok

Link to comment
Share on other sites

Link to post
Share on other sites

 textArea.insert("2",0); will insert to place 0 or the start of the string you need to insert to the end of the string. You will need to find the length of the teat area then  textArea.insert("2",length -1);

 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, Shammikit said:

ok

Add spaces between the buttons as well, still a bit hard to read.

If you want to reply back to me or someone else USE THE QUOTE BUTTON!                                                      
Pascal laptops guide

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, vorticalbox said:

 textArea.insert("2",0); will insert to place 0 or the start of the string you need to insert to the end of the string. You will need to find the length of the teat area then  textArea.insert("2",length -1);

 

since this is a calculator the user should be able to enter numbers in any order.if i made it this way i can only enter numbers from 1 to 9 in ascending order right

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, Castdeath97 said:

Add spaces between the buttons as well, still a bit hard to read.

done 

 

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, Shammikit said:

since this is a calculator the user should be able to enter numbers in any order.if i made it this way i can only enter numbers from 1 to 9 in ascending order right

no to will add what they pressed to the end of the string.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

 

10 minutes ago, vorticalbox said:

 textArea.insert("2",0); will insert to place 0 or the start of the string you need to insert to the end of the string. You will need to find the length of the teat area then  textArea.insert("2",length -1);

 

doing this way im able to type in12 but im not able to type 21

Link to comment
Share on other sites

Link to post
Share on other sites

Why not use Getters and Setters in a data-holding class?

 

This is what I wrote for "my first calculator" (so it might not be perfect, but it works how you want it to):

 


public class Calculator{

private String textFieldContents = "0";

 

public String getTextFieldContents() {
        return textFieldContents;
    }


    public void setTextFieldContents(String textFieldContents) {
        this.textFieldContents = textFieldContents;
    }

 

This would be the "1" button and the text field:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import Calculator;

public class CalculatorUI extends Calculator implements ActionListener {

    protected Calculator calc = new Calculator();

    private JButton numberButton1;

    private JTextArea resultTextArea;
    private JPanel basePanel;

    private String textFieldContents = calc.getTextFieldContents();

 

    // putting it in the constructor cause I'm a lazy fuck 

    public CalculatorUI() {

 

        resultTextArea = new JTextArea();
        resultTextArea.setBounds(20, 50, 340, 40);
        resultTextArea.setBackground(Color.WHITE);
        resultTextArea.setEnabled(true);
        resultTextArea.setEditable(false);
        resultTextArea.setSelectedTextColor(Color.BLACK);

 

        numberButton1 = new JButton("1");
        numberButton1.setText("1");
        numberButton1.setBounds(20, 100, 50, 50);
        numberButton1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (calc.getTextFieldContents().equals("0")) {
                    textFieldContents = "1";
                    resultTextArea.setText(textFieldContents);
                    entryTrackerLabel.setText(textFieldContents);
                    calc.setTextFieldContents(resultTextArea.getText());
                } else {
                    textFieldContents = (calc.getTextFieldContents() + "1");
                    resultTextArea.setText(textFieldContents);
                    entryTrackerLabel.setText(entryTrackerLabel.getText()+"1");
                    calc.setTextFieldContents(resultTextArea.getText());
                }
            }
        });

 

        // add elements to panel
        basePanel = new JPanel();
        basePanel.setLayout(null);
        basePanel.setSize(360, 400);

        basePanel.add(resultTextArea);
        basePanel.add(numberButton1);

}

 

And then convert the text field into a double or float type to do your maths.

 

 

 

Intel i7 5820K (4.5 GHz) | MSI X99A MPower | 32 GB Kingston HyperX Fury 2666MHz | Asus RoG STRIX GTX 1080ti OC | Samsung 951 m.2 nVME 512GB | Crucial MX200 1000GB | Western Digital Caviar Black 2000GB | Noctua NH-D15 | Fractal Define R5 | Seasonic 860 Platinum | Logitech G910 | Sennheiser 599 | Blue Yeti | Logitech G502

 

Nikon D500 | Nikon 300mm f/4 PF  | Nikon 200-500 f/5.6 | Nikon 50mm f/1.8 | Tamron 70-210 f/4 VCII | Sigma 10-20 f/3.5 | Nikon 17-55 f/2.8 | Tamron 90mm F2.8 SP Di VC USD Macro | Neewer 750II

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×