[英]How do I output to textField?
I'm making a java program to turn any text into capital capital letters. 我正在制作一个Java程序,将任何文本转换为大写字母。 It worked fine with system.out.print but now I want to turn it into a GUI.
它与system.out.print一起正常工作,但现在我想将其转换为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.
我尝试从原始textField输入中获取文本,将其转换为双精度型,将双精度型转换为字符串,然后使用addText,但我认为它不起作用。 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.
这是应该的工作方式:我在输入框中写了一堆文本,然后单击“ GO”按钮,它将每个字符转换为大写字母。 The output is supposed to to inside another TextField
输出应该在另一个TextField内部
here is my code (using Eclipse) 这是我的代码(使用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. 您无需将输入
String
转换为double
即可使其变为大写。 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. 不要执行
setLayout(null)
并使用setBounds()
将组件设置在绝对位置。 Instead use layout managers. 而是使用布局管理器。 See https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
参见https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.