JTextField等于另一个JTextField

时间:2011-03-30 18:58:10

标签: java swing

我在下面的代码中提到了我要找的内容:从底部开始大约17行。

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())  
                {  
        /* THIS IS WHERE THE WIDTH FIELD NEEDS TO EQUAL THE LENGTH FIELD */ 
                    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!!");  
            }  
        }  
    }); 

After a user enters a number for the length it automatically enters it into the width field.

3 个答案:

答案 0 :(得分:0)

如果您想要比较内容:

textField1.getText().equals(textField2.getText());

其他比较只需查看javadoc和getValueWhichYouAreInterestedIn()

答案 1 :(得分:0)

从你的变量名称判断我想你想要从某些用户那里得到宽度和高度,在这种情况下我会这样做:

double width = Double.parseDouble(textField1.getText());
double height= Double.parseDouble(textField2.getText());

if(width == height){
  Bla bla ...
}

答案 2 :(得分:0)

我希望您使用过Formatted text fields?在这种情况下,您可以lengthField.setValue(widthField.getValue())

但是如果用户然后在长度框中输入了什么呢?哪个是'主'字段?我建议改为禁用宽度框,然后你不必设置任何东西。