即使這是一種有效的方法,JComboBox#setVisible
也不會隱藏我的JComboBox
。難道我做錯了什麼?如果不是,有沒有其他的選擇?JCombobox.setVisible(false);
-1
A
回答
2
既然你不顯示在發佈的SSCCE任何興趣,這裏是我證明你有問題說什麼都是假的,沒有什麼可以直到完成你發佈你的代碼。
這工作對我很好,
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class FrameTest extends JFrame implements ActionListener {
JComboBox test;
public FrameTest() {
setLayout(new FlowLayout());
setSize(550, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton hideJCombo = new JButton("Hide my JCombobox!");
JButton showJCombo = new JButton("Show my JCombobox!");
String course[] = {"This", "is", "a", "sample", "for", "StackOverflow"};
test = new JComboBox(course);
add(hideJCombo);
add(test);
add(showJCombo);
hideJCombo.setActionCommand("hide");
showJCombo.setActionCommand("show");
hideJCombo.addActionListener(this);
showJCombo.addActionListener(this);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new FrameTest().setVisible(true);
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
if ("hide".equals(e.getActionCommand())) {
test.setVisible(false);
System.out.println("hide");
} else if ("show".equals(e.getActionCommand())) {
test.setVisible(true);
}
}
}
1
不知道爲什麼我花時間創建SSCCE,但此代碼正常工作。我建議你與你的代碼進行比較和搜索的差異
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class ComboboxDemo {
private static JFrame createFrame(){
JFrame result = new JFrame("ComboboxDemo");
final JComboBox<String> combobox = createCombobox();
result.add(combobox, BorderLayout.CENTER);
JCheckBox toggleVisibility = new JCheckBox("Toggle visibility");
toggleVisibility.setSelected(combobox.isVisible());
toggleVisibility.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
combobox.setVisible(e.getStateChange() == ItemEvent.SELECTED);
}
});
result.add(toggleVisibility, BorderLayout.SOUTH);
result.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
result.pack();
return result;
}
private static JComboBox<String> createCombobox(){
return new JComboBox<>(new String[]{"foo", "bar", "StackOverflow", "Hello World"});
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
createFrame().setVisible(true);
}
});
}
}
1
,直到您發佈的代碼沒有人能夠回答你的問題。但是,對於「替代方案」的問題,我會回答。
您可以禁用它使用「的setEnabled(假)」
如果它是一個JPanel裏面,則可以使用「刪除()」刪除方法和方法的其他重載版本
您可能可以使用「setEditable(false)」將其設置爲不可編輯。我還沒有試過它
相關問題
- 1. Javascript:false && false是false?
- 2. XML false vs(string)false
- 3. false/FALSE - 有什麼區別?
- 4. False or None vs. None或False
- 5. C#How false false == true?見圖
- 6. HiddenInput(DisplayValue = false)] vs [ScaffoldColumn(false)]
- 7. 不能設法調用mojarra.ab(this,evt,'action',false,false,false);
- 8. MVC 5複選框返回「False,false」或「false」
- 9. SecureSocket.isSupported == false
- 10. ArrayIndexOutOfBoundsException False?
- 11. JavaScript !!「false」
- 12. ModelState.isValid - false
- 13. ObjXMLDoc.async = False
- 14. Application.SetCompatibleTextRenderingDefault(false);
- 15. false和FALSE有什麼區別?
- 16. 區別:[ScaffoldColumn(false)]和[Display(AutoGenerateField = false)]
- 17. ValidateInput(false)vs AllowHtml
- 18. Popup with StaysOpen = False
- 19. @ javax.persistence.Column(updatable = false)
- 20. openssl_dh_compute_key返回false
- 21. WPF和ToolboxItem(false)
- 22. PHP boolean TRUE/FALSE?
- 23. jquery:event.stopImmediatePropagation()vs return false
- 24. jQuery true和false
- 25. Request.IsAjaxRequest返回false
- 26. Golang:reflect.DeepEqual返回false
- 27. GetVolumeNameForVolumeMountPoint返回false
- 28. PHP password_verify false negative
- 29. AS3 hitTestObject == false
- 30. Req.isAuthenticated變爲false
*「我做錯了什麼?」*是的。爲了更好地(或任何)幫助,請發佈[SSCCE](http://sscce.org/)。 –
該方法按預期工作 – Robin
_Ecce vis de [sscce](http://sscce.org/)infra!_ – trashgod