2014-10-01 176 views
1

Noob here。我一直在浏览几个小时,但我仍然无法弄清楚如何让用户输入从我的Jframe中的文本字段保存为字符串。任何帮助,将不胜感激。我想将用户的文本保存到变量userWords中。JAVA:将用户输入保存为Jframe GUI中的字符串

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

    } 
} 
+0

一个好方法是首先添加一个文本字段(http://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html)。 – Tom 2014-10-01 23:02:36

+1

你有什么麻烦?该领域从现场获得价值,从现场节省价值?你想坚持价值到磁盘? – MadProgrammer 2014-10-01 23:33:52

+0

嗯,我想保持窗口的大小,只在中间有一个文本窗口 - 有点像谷歌的主页面。然后,这个想法是对文本进行“加密”,然后通过另一条消息(选择加密类型后)将其输出。 – user1765804 2014-10-02 01:35:26

回答

4

你必须使用一个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); 
    } 
} 
+0

嗯,我想保持窗口的大小,只是在中心有一个文本窗口 - 有点像谷歌的主页面。然后,这个想法是对文本进行“加密”,然后通过另一条消息(选择加密类型后)将其输出。 – user1765804 2014-10-02 01:34:58

+0

对不起,我改变你的布局^^。我会为你修改 – BilalDja 2014-10-02 01:36:24

+0

谢谢,我正在学习......慢慢地,但我到了那里。我想我不知道用什么来创建该文本框,因为我不希望它在新窗口中,我希望它相当长 – user1765804 2014-10-02 01:39:04

0

这是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(); 
} 
相关问题