开发者

Automatic entry in text field

I have this GUI:

Automatic entry in text field

I would like after I enter a number in the Length of hot tub text box for that number to be auto开发者_运维技巧matically entered into the Width of hot tub text box, but only if the Round Tub radio button is selected.

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!!");   
            }   
        }   
    });


Add a focus listener to your length text field when it loses focus and the round tub is selected, compute and set the the width.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜