[英]Card Layout and toggle button problems
我在使用卡片布局和切換按鈕時遇到問題。 例如,我想更改背景按鈕:
private void togglebtnActionPerformed(java.awt.event.ActionEvent evt){
if(togglebtn.isSelected()){
togglebtn.setBackground(Color.green);}
else{
togglebtn.setBackground(Color.red);}
}
並將此togglebtn放在普通的JFrame上,就可以工作。 如果我使用帶有CardLayout的面板並將此togglebtn放在帶有CardLayout的面板上,則它不起作用。
private void cbItemStateChanged(java.awt.event.ItemEvent evt){ //cb is the combobox i use to switch the two panels
CardLayout cl = (CardLayout) (displaypane.getLayout()); //displaypane is the panel in which i used the CardLayout
if (cb.getSelectedIndex() == 0){
cl.show(displaypane, "card1"); //card1 is the first panel in displaypane
} else {
cl.show(displaypane, "card2"); //card2 is the second panel in displaypane
}
}
現在,如果我使用之前無法使用的相同代碼:
private void togglebtnActionPerformed(java.awt.event.ActionEvent evt){ //the toggle button is in card1
if(togglebtn.isSelected()){
togglebtn.setBackground(Color.green);}
else{
togglebtn.setBackground(Color.red);}
}
它僅顯示紅色背景,而不顯示綠色背景,因此無法選擇切換按鈕。 使用CardLayout有什么區別?
我無法在這里解釋CardLayout
的問題。 實際上,我也無法使其與簡單的JFrame
使用。
但是,如果要更改按鈕的背景色,則需要使用以下命令對其進行初始化:
togglebtn.setContentAreaFilled(false);
togglebtn.setOpaque(true);
使它不透明,並刪除內容區域的填充,以確保您的ui外觀不會覆蓋您定義的背景色。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.