我寫了下面這段代碼,它應該返回一個複選框和一個按鈕的面板。這個想法是,只有在複選框被選中的情況下才能啓用該按鈕。 它的工作原理,這意味着如果複選框沒有選中,我嘗試按下按鈕,什麼也沒有發生。但是,按鈕的外觀不正確,當它顯示爲啓用時,它顯示爲禁用,反之亦然。 任何想法我的代碼有什麼問題? 感謝很多提前啓用/禁用AWT按鈕
public Panel createCalibrationPanel(final ImagePlus imp) {
final Panel panel = new Panel();
panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
panel.add(Box.createHorizontalStrut(20));
final Checkbox checkbox = new Checkbox(
"Use image spatial calibration for q scale", true);
final Button button = new Button("Set scale");
useCalibration = checkbox.getState();
button.setEnabled(checkbox.getState());
panel.add(checkbox);
panel.add(button);
checkbox.addItemListener(new ItemListener() {
public void itemStateChanged(final ItemEvent e) {
boolean state = checkbox.getState();
setUseCalibration(state);
button.setEnabled(state);
}
});
button.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
imp.unlock();
IJ.run(imp, "Set Scale...", "");
imp.lock();
}
});
return panel;
}
如果您刪除了與實際問題無關的所有代碼並僅粘貼相關代碼,那麼看看發生了什麼(並自己嘗試)會容易得多。你也可能會發現,在這樣做的時候,你解決了自己的問題。 –
對不起,我會清理我的代碼。 – Sebastien