简体   繁体   中英

How do I output to textField?

I'm making a java program to turn any text into capital capital letters. It worked fine with system.out.print but now I want to turn it into a GUI. I tried getting the text from the original textField input, turn it into a double, turn the double into a string then I used addText but I don;t think it works. Here is how it is supposed to work: I write a bunch a text into a the input box then when I click the GO button it converts every character into capital letters. The output is supposed to to inside another TextField

here is my code (using Eclipse)

    import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Frame1 {

    private JFrame frame;
    private JTextField textField;
    private JTextField textField_1;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Frame1 window = new Frame1();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Frame1() {
        initialize();
    }
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 710, 508);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JLabel lblInputTextTo = new JLabel("Input Text to Capitalise:");
        lblInputTextTo.setFont(new Font("Segoe UI", Font.BOLD, 26));
        lblInputTextTo.setBounds(210, 0, 289, 54);
        frame.getContentPane().add(lblInputTextTo);

        textField = new JTextField();
        textField.setBounds(34, 77, 624, 150);
        frame.getContentPane().add(textField);
        textField.setColumns(10);

        textField_1 = new JTextField();
        textField_1.setBounds(34, 354, 613, 90);
        frame.getContentPane().add(textField_1);
        textField_1.setColumns(10); 

        JButton btnGo = new JButton("GO!");
        btnGo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                double TextInput = Double.parseDouble(textField.getText());
                String Str = String.valueOf(TextInput);
                textField_1.setText(Str.toUpperCase());
                textField_1.setEditable(false);

            }
        });
        btnGo.setFont(new Font("Segoe UI", Font.BOLD, 18));
        btnGo.setBounds(250, 239, 180, 23);
        frame.getContentPane().add(btnGo);



        JLabel lblOutput = new JLabel("Output:");
        lblOutput.setFont(new Font("Segoe UI", Font.BOLD, 26));
        lblOutput.setBounds(304, 300, 95, 43);
        frame.getContentPane().add(lblOutput);
    }}

I'm not done yet if you have some suggestion it would be nice.

You don't need to convert the input String into double to make it uppercase. I did below change to your program and it works now.

btnGo.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
    //Remove these 2 lines
    //double TextInput = Double.parseDouble(textField.getText());
    //String Str = String.valueOf(TextInput);

    //Directly take input text, make it uppercase and set it in output field.
    textField_1.setText(textField.getText().toUpperCase());
    textField_1.setEditable(false);

  }
});

Additional tip:
Don't do setLayout(null) and use setBounds() to set components in absolute positions. Instead use layout managers. See https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM