【问题标题】:Use if with radio button and text filed both?如果与单选按钮和文本一起使用?
【发布时间】:2014-01-26 04:58:52
【问题描述】:

我有一个绝对布局的面板、一个显示图像的标签和三个单选按钮,棕色、蓝色和红色。

当我在文本字段中输入像奥迪 A4 这样的汽车名称时,会显示默认图像并选择棕色单选按钮。但是当我选择蓝色和红色单选按钮时,会显示 A4 蓝色或A4 红色,但没有反应。

private void fmod1FocusLost(java.awt.event.FocusEvent evt) {
    String mod;
    mod=fmod1.getText();

    if(mod.equals("A4")) {
    rbrown.setSelected(true);   
    imglabel.setIcon(new ImageIcon("D:\\Photo\\a4brown.jpg"));
    fcolour1.setText("Brown");

    if (rblue.isSelected()==true) {
    imglabel.setIcon(new ImageIcon("D:\\Photo\\a4blue.jpg"));
    fcolour1.setText("Blue");
  } 
    else if (rred.isSelected() == true) {
    imglabel.setIcon(new ImageIcon("D:\\Photo\\a4red.jpg"));
    fcolour1.setText("red");
  }
}
 else if(mod.equals("R8"))
    {

        rbrown.setSelected(true);
        imglabel.setIcon(new ImageIcon("D:\\Photo\\r8brown.jpg"));
      fcolour1.setText("Brown");

    if (rblue.isSelected()==true)
           {
                imglabel.setIcon(new ImageIcon("D:\\Photo\\r8blue.jpg"));
  fcolour1.setText("Blue");
           }
           else if(rred.isSelected()==true)
           {
                imglabel.setIcon(new ImageIcon("D:\\Photo\\r8red.jpg"));
  fcolour1.setText("red");
           }
           }

R8、RS5 以此类推。

我需要一个没有摇摆的解决方案。

【问题讨论】:

  • 这个问题我只明白一件事:什么都没有。
  • 你能显示更多的代码吗?上面的代码并没有过多地说明听众等。对事件处理很重要的东西。
  • 另外你需要解释你到底希望发生什么。
  • 两点: * 为什么没有 Swing? * 您的紧急不是我们的紧急,没有人会要求“请在下个月左右提供帮助”,所以最好不要在问题中使用“尽快”。

标签: java if-statement netbeans radio-button textfield


【解决方案1】:

您向我们展示的代码不会捕获用户选择不同单选按钮的事件。由于您选择的是棕色单选按钮,因此会显示该单选按钮。

您需要为单选按钮添加一个事件处理程序。

// This should be the code that responds to entering a different model
rbrown.setSelected(true);   


// Something like this should be the code that responds to 
// a radio button being selected
mod = fmod1.getText();

if(mod.equals("A4"))
{
    if (rbrown.isSelected())
    {
        imglabel.setIcon(new ImageIcon("D:\\Photo\\a4brown.jpg"));
        fcolour1.setText("Brown");
    }
    else if (rblue.isSelected())
    {
        imglabel.setIcon(new ImageIcon("D:\\Photo\\a4blue.jpg"));
        fcolour1.setText("Blue");
    }
    else if(rred.isSelected())
    {
        imglabel.setIcon(new ImageIcon("D:\\Photo\\a4red.jpg"));
        fcolour1.setText("red");
    }
} 

添加一个监听器并将它们放在一起作为练习留给读者你。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-07
    • 2012-12-25
    • 2017-03-09
    相关资源
    最近更新 更多