2014-10-01 35 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(); 
} 
相關問題