【问题标题】:JAVA: Save user input as a string in a Jframe GUIJAVA:将用户输入保存为 Jframe GUI 中的字符串
【发布时间】:2014-11-26 22:00:00
【问题描述】:

这里是菜鸟。我已经浏览了几个小时,但我仍然无法找出正确的方法来让用户输入从我的 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();

    }
}

【问题讨论】:

  • 一个好方法是先添加一个文本字段 (docs.oracle.com/javase/tutorial/uiswing/components/…)。
  • 您遇到了哪些问题?字段,从字段中获取值,从字段中保存值?是否要将值保存到磁盘?
  • 嗯,我想让窗口保持原样,并且在中间有一个文本窗口——有点像谷歌的主页。因此,这个想法是“加密”文本,然后通过另一条消息(在选择加密类型之后)进行加密后输出。

标签: java string swing


【解决方案1】:

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

【讨论】:

  • 嗯,我想让窗口保持原样,并且在中间有一个文本窗口——有点像谷歌的主页。因此,这个想法是“加密”文本,然后通过另一条消息(在选择加密类型之后)进行加密后输出。
  • 对不起,我改变了你的布局^^。我帮你修改
  • 谢谢你,我正在学习..慢慢地,但我到了那里。我想我不确定要使用什么来创建该文本框,因为我不希望它在新窗口中并且我希望它相当长
  • @user1765804 你所说的“新窗口”是什么意思,你希望它出现在对话框中吗
  • 我不知道如何描述它。谷歌主页看起来像在中心有一个长文本框的地方
【解决方案2】:

这是 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();
}

【讨论】:

    猜你喜欢
    • 2015-07-21
    • 1970-01-01
    • 2016-11-26
    • 2018-10-13
    • 2022-01-19
    • 1970-01-01
    • 2012-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多