簡體   English   中英

如何輸出到textField?

[英]How do I output to textField?

我正在制作一個Java程序,將任何文本轉換為大寫字母。 它與system.out.print一起正常工作,但現在我想將其轉換為GUI。 我嘗試從原始textField輸入中獲取文本,將其轉換為雙精度型,將雙精度型轉換為字符串,然后使用addText,但我認為它不起作用。 這是應該的工作方式:我在輸入框中寫了一堆文本,然后單擊“ GO”按鈕,它將每個字符轉換為大寫字母。 輸出應該在另一個TextField內部

這是我的代碼(使用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);
    }}

如果您有任何建議,我還沒有完成,那就太好了。

您無需將輸入String轉換為double即可使其變為大寫。 我在下面對您的程序進行了更改,現在可以使用了。

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);

  }
});

附加提示:
不要執行setLayout(null)並使用setBounds()將組件設置在絕對位置。 而是使用布局管理器。 參見https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM