JAVA:在 Jframe GUI 中将用户输入保存为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26151920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JAVA: Save user input as a string in a Jframe GUI
提问by user1765804
Noob here. I have been browsing for hours, and I still cannot figure out the proper way to get user input to be saved as a string from a text field in my Jframe. Any help would be appreciated. I want to save the user's text into the variable userWords.
菜鸟在这里。我已经浏览了几个小时,但仍然无法找出将用户输入保存为 Jframe 中文本字段中的字符串的正确方法。任何帮助,将不胜感激。我想将用户的文本保存到变量 userWords 中。
package cipher;
import java.util.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
public class cipherApp extends JFrame {
private static final int WIDTH = 700;
private static final int HEIGHT = 700;
private String userWords; // stores user input into a string
public cipherApp(){
setTitle("Camo Cipher");
setSize(WIDTH, HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args){
cipherApp newInstance = new cipherApp();
}
}
回答by BilalDja
You have to use a JTextField and a JButton to submit the use input:
您必须使用 JTextField 和 JButton 来提交使用输入:
public class Test extends JFrame {
String userWord = "";
JTextField userInput = new JTextField(10);
JButton submit = new JButton("Submit");
public Test() {
super("Camo Cipher");
JPanel centerPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 15, 15));
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null); // This center the window on the screen
submit.addActionListener( (e)-> {
submitAction();
});
centerPanel.add(userInput);
JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 15, 15));
southPanel.add(submit);
Box theBox = Box.createVerticalBox();
theBox.add(Box.createVerticalStrut(100));
theBox.add(centerPanel);
theBox.add(Box.createVerticalStrut(200));
theBox.add(southPanel);
add(theBox);
}
private void submitAction() {
// You can do some validation here before assign the text to the variable
userWord = userInput.getText();
}
public static void main(String[] args) {
new Test().setVisible(true);
}
}
回答by user1765804
This is a variation of BilaDja's code (I mainly changed the graphics). This will make the window size at around half the screen, in the middle of the screen. If you would like to change the size, change the field 'jf.setSize(x, y);'.
这是 BilaDja 代码的变体(我主要更改了图形)。这将使窗口大小约为屏幕的一半,在屏幕的中间。如果您想更改大小,请更改字段'jf.setSize(x, y);' .
package test;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Test extends JFrame {
private static final long serialVersionUID = -5624404136485946868L;
String userWord = "";
JTextField userInput;
public Test() {
JFrame jf = new JFrame();
JPanel panel = new JPanel();
JLabel jl = new JLabel("Test");
JButton jButton = new JButton("Click");
userInput = new JTextField("", 30);
jButton.addActionListener( (e) -> {
submitAction();
});
jf.setSize(500, 500);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add(panel);
panel.add(jl);
panel.add(userInput);
panel.add(jButton);
}
private void submitAction() {
userWord = userInput.getText();
//do something with the variabe userWord here (print it to the console, etc.)
}
public static void main(String[] args) {
new Test();
}