-7

我对java很陌生,所以请放轻松!

我创建了一个窗口、窗格、标签、文本字段等。

什么是查看用户在文本字段中输入的内容(作为字符串)的最简单代码,正如我所知道的可以使用的浮点数Float.parseFloat(txtName.getText());

4

2 回答 2

1

为了我对这个问题的理解,这是一个简单的示例,输出在 a 中输入的内容JTextField并将其附加到 a JTextArea

在此处输入图像描述

public class Test {
    private static String ENTER = "Enter";
    static JButton enterButton;
    public static JTextArea output;
    public static JTextField input;
    static JFrame frame;
    static JPanel panel;

    public static void main(String... args)
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex)
        {
            ex.printStackTrace();
        }
        createFrame();
    }

    public static void createFrame()
    {
        frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.setOpaque(true);
        ButtonListener buttonListener = new ButtonListener();
        output = new JTextArea(15, 50);
        output.setWrapStyleWord(true);
        output.setEditable(false);
        JScrollPane scroller = new JScrollPane(output);
        scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        JPanel inputpanel = new JPanel();
        inputpanel.setLayout(new FlowLayout());
        input = new JTextField(20);
        enterButton = new JButton("Enter");
        enterButton.setActionCommand(ENTER);
        enterButton.addActionListener(buttonListener);
        input.setActionCommand(ENTER);
        input.addActionListener(buttonListener);
        DefaultCaret caret = (DefaultCaret) output.getCaret();
        caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
        panel.add(scroller);
        inputpanel.add(input);
        inputpanel.add(enterButton);
        panel.add(inputpanel);
        frame.getContentPane().add(BorderLayout.CENTER, panel);
        frame.pack();
        frame.setLocationByPlatform(true);
        // Center of screen
        // frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setResizable(false);
        input.requestFocus();
    }

    public static class ButtonListener implements ActionListener
    {

        public void actionPerformed(final ActionEvent ev)
        {
            Thread thread = new Thread()
            {

                public void run()
                {
                    if (!input.getText().trim().equals(""))
                    {
                        String cmd = ev.getActionCommand();
                        if (ENTER.equals(cmd))
                        {
                            output.append(input.getText() + "\n");
                        }
                    }
                    input.setText("");
                    input.requestFocus();
                }
            };
            thread.start();
        }
    }
}
于 2013-04-20T16:27:01.413 回答
1

注意:如果你想真正做到正确,你可以实现一个 JTextField 的子类,它甚至不允许用户输入无效浮点数的字符:

public class FloatField extends JTextField {

    public FloatField(int cols) {
        super(cols)
    } 

    protected Document createDefaultModel() { 
        return new FloatDocument();
    } 

    static class FloatDocument extends PlainDocument {

        public void insertString(int offs, String str, AttributeSet a)
                          throws BadLocationException { 
            if( str == null ) 
                return;

            // Reject any string with invalid float characters.
            // TODO: this could be more sophisticated
            int len = str.length();
            for (int i = 0; i < len; ++i) {
                char c = str.charAt(i);
                if (c != '.' && c != '-' && !Character.isDigit(c))
                    return;
            }
            super.insertString(offs, str, a);
        } 
    } 
} 

注意:这是一个不完美的实现,仅用于说明目的,仍然有许多无效的浮点数会允许泄漏。

于 2013-04-20T16:53:09.783 回答