卡片布局和切换按钮问题
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);}
}
只显示红色背景,不显示绿色背景,所以不能选择togglebutton。拥有 CardLayout 有何不同?
我无法在这里解释 CardLayout
的问题。事实上,我也无法使用简单的 JFrame
来实现它。
但是如果你想改变按钮的背景颜色,你需要初始化它:
togglebtn.setContentAreaFilled(false);
togglebtn.setOpaque(true);
使其不透明并移除内容区域的填充可确保您的 ui 外观和感觉不会过度绘制您定义的背景颜色。
我在使用卡片布局和切换按钮时遇到问题。 我想在按下时更改背景按钮,例如:
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);}
}
只显示红色背景,不显示绿色背景,所以不能选择togglebutton。拥有 CardLayout 有何不同?
我无法在这里解释 CardLayout
的问题。事实上,我也无法使用简单的 JFrame
来实现它。
但是如果你想改变按钮的背景颜色,你需要初始化它:
togglebtn.setContentAreaFilled(false);
togglebtn.setOpaque(true);
使其不透明并移除内容区域的填充可确保您的 ui 外观和感觉不会过度绘制您定义的背景颜色。