代码之家  ›  专栏  ›  技术社区  ›  rainhider

如何从JFrame中的文本框接收输入?

  •  1
  • rainhider  · 技术社区  · 13 年前

    下面的方法扩展了JFrame,但我需要在它里面有一个swing文本框来接收双值并将它们存储在一个变量中。必须将文本框添加到容器中。

        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 回复  |  直到 13 年前
        1
  •  6
  •   syb0rg    13 年前

    使用 JTextField :

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

    要获取字段的值,请执行 Double.parseDouble(field.getText()) .