How do I receive input from a textbox in a JFrame?

天涯浪子 提交于 2019-12-13 17:26:59

问题


The following method extends a JFrame, but I need a swing textbox inside it to receive double values and store them in a variable. the textbox must be added to the container.

    public class myClass extends JFrame{
        Container container;
        container = getContentPane();

        //Item label
        JLabel labelText = new JLabel();        
       ...
        labelText.setText ("Item:");        
        container.add( labelText );

        //Item name: Textbox
        //code to make a textbox, add it to the container, and store the value in a                      variable goes here

回答1:


Use a JTextField:

 JTextField field = new JTextField(10);
 container.add(field, BorderLayout.SOUTH);

To get the value of the field, do Double.parseDouble(field.getText()).



来源:https://stackoverflow.com/questions/15538127/how-do-i-receive-input-from-a-textbox-in-a-jframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!