2013-01-19 48 views
1

我有两个类mainpanel.java和subpanel.java。 subpanel.class包含一个复选框和一些标签。当我点击mainpanel.java中的某些按钮时,我想更改这些组件的setSelected()和setText()。访问其他类的swing组件

我在subpanel.java中创建了一个方法,我从mainpanel.java调用并传递布尔值。

public void schedulerchange(boolean check){ 
     System.out.println("checked"+check); 
     scheduleenabler.setEnabled(check); 
     scheduleenabler.setSelected(check); 
     scheduleinfo.setText("Scheduler in On"); 
     //subpanel21.updateUI(); 
    } 

当我打电话从mainpanel.java函数被调用此功能,但值不会改变,除非我做JCheckBox的和静态的JLabel。但从我学到的东西我们不应该使用静态组件,除非非常必要。 有没有其他的方法来改变组件?

+0

我想在设置启用调用** scheduleenabler.revalidate()**将做的伎俩。 – Amarnath

+0

*“我有两个类mainpanel.java和subpanel.java”*不要扩展任何一个。只需保留一个参考。保持对文本组件的引用,并解决问题。还请学习类的常用[Java命名约定](http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#73307)(具体用于名称的情况)方法和属性名称并一致使用它。 –

+0

@che revalidate()不起作用。它仍然更新复选框,如果我使用静态 –

回答

3

如果我明白你的问题,然后我想你想编写一个单独ActionListener类,并没有执行操作,这将使还是在UI级禁用JCheckBox。以下代码显示。将您的复选框引用传递给PerformAction类,并通过单击该按钮启用或禁用它。

import java.awt.EventQueue; 
import java.awt.FlowLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JCheckBox; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

public class MainClass { 

MainClass() { 
    JFrame jfrm = new JFrame("JTable Demo"); 
    jfrm.setLayout(new FlowLayout()); 
    jfrm.setSize(460, 180); 
    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    JCheckBox check = null; 
    // Get the Panel from the subclass; 
    JPanel panel = new CheckBox().getCheckBoxPanel(); 

    // From the compoenents present in the panel get the CheckBox compoenent. 
    for(int i = 0; i < panel.getComponentCount(); i++) { 
     if(panel.getComponent(i) instanceof JCheckBox) { 
     check = (JCheckBox) panel.getComponent(i); 
     } 
    } 

    JButton button = new JButton("Click"); 

    // Pass the CheckBox Compoenent to the ActionListener. 
button.addActionListener(new PerformAction(check)); 

    jfrm.add(button); 
    jfrm.add(panel); 
    jfrm.setVisible(true); 
} 

    public static void main(String args[]) { 
    EventQueue.invokeLater(new Runnable() { 

     @Override 
     public void run() { 
      new MainClass(); 
     } 
    }); 
} 
} 

class PerformAction implements ActionListener { 

JCheckBox check = null; 
public PerformAction(JCheckBox checkBox) { 
    check = checkBox; 
} 
@Override 
public void actionPerformed(ActionEvent e) { 
    boolean checkStatus = check.isSelected(); 
    if(checkStatus == true) { 
     check.setEnabled(false); 
     check.setSelected(false); 
    } else { 
     check.setEnabled(true); 
     check.setSelected(true); 
     } 
} 
} 

class CheckBox { 
public JPanel getCheckBoxPanel() { 
    JPanel checkPanel = new JPanel(); 
    JCheckBox check = new JCheckBox(); 
    checkPanel.add(new JLabel("CheckBox")); 
    checkPanel.add(check); 

    return checkPanel; 
} 
} 
+0

我使用两个不同的类。子面板类将面板返回到mainpanel类。这个面板包含一个复选框和标签,当我点击mainpanel类中的一个按钮时,我想改变它的属性。 –

+0

看看我的更新代码。这将获得Panel并从面板获取组件,然后将其传递给ActionPerformed类。希望这会有所帮助.. ;-) – Amarnath

+0

@RohanKandwal:不要忽视你发现有帮助的答案。 – trashgod

3

这不是updateUI()的适当使用,它将“将UI属性重置为当前外观的值”。正如注释中所建议的,使用revalidate()仅当将组件添加到封闭的Container或从其中删除时才会有帮助。相反,直接在子面板实例上调用repaint()。为获得更大的灵活性,请使用观察者pettern建议here

附录:本示例使用Action来封装按钮的行为。由于复选框的选定状态是绑定属性,因此组件会自动重新绘制,但如果需要,您可以明确调用repaint()

附录:更新以传递参考作为参数。

附录:在此变体中,该参数是对导出的Action的引用。

import java.awt.EventQueue; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import javax.swing.AbstractAction; 
import javax.swing.Action; 
import javax.swing.JButton; 
import javax.swing.JCheckBox; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

/** @see https://stackoverflow.com/a/14412516/230513 */ 
public class Example { 

    private void display() { 
     JFrame f = new JFrame("Example"); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.setLayout(new GridLayout(0, 1)); 
     JPanel panel = new JPanel(); 
     final JCheckBox check = new JCheckBox("Check"); 
     Action checkAction = new AbstractAction("Update") { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       check.setSelected(!check.isSelected()); 
      } 
     }; 
     panel.add(check); 
     f.add(panel); 
     f.add(new SubPanel(checkAction)); 
     f.pack(); 
     f.setLocationRelativeTo(null); 
     f.setVisible(true); 
    } 

    private static class SubPanel extends JPanel { 

     public SubPanel(final Action action) { 
      this.add(new JButton(action)); 
     } 
    } 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       new Example().display(); 
      } 
     }); 
    } 
} 
+0

我几乎是一个noob,所以我不明白深层编码。一个简单的例子,两个类将是很好的 –

+0

我已经阐述了上面。 – trashgod

+0

感谢您的示例,但您的示例使用了一个在actionPerformed上进行更新的类。我想在从其他课程执行操作时选择/取消选中该复选框。 –