Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 文本字段中的自动输入_Java_Swing_User Interface_Event Handling - Fatal编程技术网

Java 文本字段中的自动输入

Java 文本字段中的自动输入,java,swing,user-interface,event-handling,Java,Swing,User Interface,Event Handling,我有一个GUI: 我希望在“热水浴缸长度”文本框中输入一个数字后,该数字将自动输入到“热水浴缸宽度”文本框中,但前提是选择了“圆形浴缸”单选按钮 public void createHotTubs() { hotTubs = new JPanel(); hotTubs.setLayout(null); labelTubStatus = new JTextArea(6, 30); hotTubs.add(labelTubStatus)

我有一个GUI:

我希望在“热水浴缸长度”文本框中输入一个数字后,该数字将自动输入到“热水浴缸宽度”文本框中,但前提是选择了“圆形浴缸”单选按钮

public void createHotTubs()   
{   
    hotTubs = new JPanel();   
    hotTubs.setLayout(null);   
    labelTubStatus = new JTextArea(6, 30);   
    hotTubs.add(labelTubStatus);   
    JLabel lengthLabel = new JLabel(   
            "Length of hot tub(ft):");   
    lengthLabel.setBounds(10, 15, 260, 20);   
    hotTubs.add(lengthLabel);   
    hotTubLengthText = new JTextField();   
    hotTubLengthText.setBounds(180, 15, 150, 20);   
    hotTubs.add(hotTubLengthText);   
    JLabel widthLabel = new JLabel(   
            "Width of hot tub(ft):");   
    widthLabel.setBounds(10, 40, 260, 20);   
    hotTubs.add(widthLabel);   
    hotTubWidthText = new JTextField();   
    hotTubWidthText.setBounds(180, 40, 150, 20);   
    hotTubs.add(hotTubWidthText);   
    JLabel depthLabel = new JLabel(   
            "Average depth the hot tub(ft):");   
    depthLabel.setBounds(10, 65, 260, 20);   
    hotTubs.add(depthLabel);   
    hotTubDepthText = new JTextField();   
    hotTubDepthText.setBounds(180, 65, 150, 20);   
    hotTubs.add(hotTubDepthText);   
    JLabel volumeLabel = new JLabel("The hot tub volume is:(ft ^3");   
    volumeLabel.setBounds(10, 110, 260, 20);   
    hotTubs.add(volumeLabel);   
    hotTubVolumeText = new JTextField();   
    hotTubVolumeText.setBounds(180, 110, 150, 20);   
    hotTubVolumeText.setEditable(false);   
    hotTubs.add(hotTubVolumeText);   
    final JRadioButton rdbtnRoundTub = new JRadioButton("Round Tub");   
    rdbtnRoundTub.addActionListener(new ActionListener()   
    {   
        public void actionPerformed(ActionEvent arg0)   
        {   
            hotTubWidthText.setEditable(false);   
        }   
    });   
    rdbtnRoundTub.setSelected(true);   
    rdbtnRoundTub.setBounds(79, 150, 109, 23);   
    hotTubs.add(rdbtnRoundTub);   
    JRadioButton rdbtnOvalTub = new JRadioButton("Oval Tub");   
    rdbtnOvalTub.addActionListener(new ActionListener()   
    {   
        public void actionPerformed(ActionEvent arg0)   
        {   
            hotTubWidthText.setEditable(true);   
        }   
    });   
    rdbtnOvalTub.setBounds(201, 150, 109, 23);   
    hotTubs.add(rdbtnOvalTub);   
    ButtonGroup radioBtnGroup = new ButtonGroup();   
    radioBtnGroup.add(rdbtnRoundTub);   
    radioBtnGroup.add(rdbtnOvalTub);   
    JButton btnCalculateVlmn = new JButton("Calculate Volume");   
    btnCalculateVlmn.addActionListener(new ActionListener()   
    {   
        public void actionPerformed(ActionEvent arg0)   
        {   
            double width = 0, length = 0, depth = 0, volume = 0;    
            String lengthString, widthString, depthString;    
            lengthString = hotTubLengthText.getText();    
            widthString = hotTubWidthText.getText();    
            depthString = hotTubDepthText.getText();    
            depth = Double.valueOf(depthString);  
            length = Double.valueOf(lengthString);  
            width = Double.valueOf(widthString); 

            try  
            {   
                if (rdbtnRoundTub.isSelected())   
                {   
                   volume = length * width * depth;   
                }   
                else  
                {   
                    volume = Math.PI * length * width / 4 * depth;   
                }   
                DecimalFormat formatter = new DecimalFormat("#,###,###.###");   
                hotTubVolumeText.setText("" + formatter.format(volume));   
            }   
            catch (NumberFormatException e)   
            {   
                labelTubStatus   
                        .setText("Enter all three numbers!!");   
            }   
        }   
    });

当长度文本字段失去焦点且选中圆形桶时,向其添加焦点侦听器,计算并设置宽度。

当长度文本字段失去焦点且选中圆形桶时,向其添加焦点侦听器,计算并设置宽度。

您需要一些if语句和文本字段的侦听器,我认为你可以自己做这件事:)与你眼前的问题无关——但如果你坚持的话,这将是未来的问题:做吧。使用。布局经理。(而不是手动调整组合的大小/位置)您需要一些if语句和textfield的侦听器,我认为您可以自己完成:)这与您当前的问题无关,但如果您坚持:do,这在将来将是一个问题。使用。布局经理。(而不是手动调整/定位组件)