即使這是一種有效的方法,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. jquery驗證false set false false
- 2. Javascript:false && false是false?
- 3. XML false vs(string)false
- 4. false && nil and nil && false in ruby
- 5. false/FALSE - 有什麼區別?
- 6. HiddenInput(DisplayValue = false)] vs [ScaffoldColumn(false)]
- 7. False or None vs. None或False
- 8. b = false與if(b)b = false
- 9. C#How false false == true?見圖
- 10. MVC 5複選框返回「False,false」或「false」
- 11. 不能設法調用mojarra.ab(this,evt,'action',false,false,false);
- 12. CURLOPT_SSL_VERIFYPEER = false?
- 13. ArrayIndexOutOfBoundsException False?
- 14. ModelState.isValid - false
- 15. Application.SetCompatibleTextRenderingDefault(false);
- 16. ObjXMLDoc.async = False
- 17. SecureSocket.isSupported == false
- 18. JavaScript !!「false」
- 19. 在佈局上:false false client_side_validation屬性
- 20. Clojure koans :(如果false [])vs(如果false [:a:b:c])
- 21. false或true!= true或false!= true ||假
- 22. 在beforeDelete回調中返回false false killAll
- 23. !false和!= false有什麼區別
- 24. 區別:[ScaffoldColumn(false)]和[Display(AutoGenerateField = false)]
- 25. false和FALSE有什麼區別?
- 26. AutoCompleteTextView.isPopupShowing()總是FALSE
- 27. Couchbase rereduce always false
- 28. startAccessingSecurityScopedResource()return always false
- 29. File.delete()返回false
- 30. openssl_x509_parse返回false
*「我做錯了什麼?」*是的。爲了更好地(或任何)幫助,請發佈[SSCCE](http://sscce.org/)。 –
該方法按預期工作 – Robin
_Ecce vis de [sscce](http://sscce.org/)infra!_ – trashgod