[英]while disabling the tabs in Jtabbedpane, there is no change in the look and feel of that tab
我正在使用JTabbedPane。 在这种情况下,我必须禁用一些选项卡。 我使用setEnabledAt(index,false)方法禁用该选项卡。 该选项卡实际上已被禁用,但我没有外观[禁用的选项卡文本的颜色变为浅色]。 我该如何纠正?
首先,我使用Java的默认外观...现在,我将外观更改为nimbus。。我发现颜色发生了变化,但是禁用的选项卡看起来不像禁用的复选框,单选按钮或其他组件。
我假设您真正想要的是禁用“禁用”选项卡上的所有组件?
Swing无法以父组件的setEnabled()对子组件产生任何影响的方式工作-您需要自己对每个子组件调用setEnabled(false)(毕竟,您可能希望启用其中的一些功能,因此,Swing不做任何假设,并将其完全留给您)。
我没有意识到应该对LookAndFeel进行了更改,但是使用UIManager.put,您可以更改非常特殊部分的LookAndFeel,我敢打赌,通过手动更改残疾人的外观/感觉标签,您也许可以得到想要的东西。
您可以在此页面上寻求帮助,以及指向所有外观选项的链接http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/color.html
JTabbedPan
e的大多数方法在API中受保护,并且不能从Swing methods
必须寻找Custom XxxTabbedPaneUI
,重写这些方法,并且可以从外部访问
正确的方法是仅实现Custom Look&Feel ,其中一部分也使用protected methods
覆盖JTabbedPane
您可以很容易地为禁用的选项卡更改Foreground
,但仅通过使用HTML语法即可正常工作
码
tabbedPane.setTitleAt(2, "<html><font color="
+ (tabbedPane.isEnabledAt(int tab) ? "black" : "red") + ">"
+ tabbedPane.getTitleAt(int tab) + "</font></html>");
Look and Feel
或简单地通过使用HTML
更改Foreground
也许您有自己的Tab组件?
然后,您必须将Enable-State委派给该组件。
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
public class Display extends JFrame {
public Display() {
super("Enable");
this.setDefaultCloseOperation( EXIT_ON_CLOSE );
this.setSize( new Dimension(300,300) );
this.setLocationRelativeTo( null );
JTabbedPane tabs = new JTabbedPane() {
// delegating to tabComponent
@Override
public void setEnabledAt( int index, boolean enabled ) {
super.setEnabledAt( index, enabled );
Component tabComponent = getTabComponentAt( index );
if(tabComponent != null) {
getTabComponentAt( index ).setEnabled( enabled );
}
}
};
JComponent component = new JLabel("Lorem");
tabs.addTab( "", component );
JLabel custom = new JLabel("disabled");
tabs.setTabComponentAt( tabs.indexOfComponent( component ), custom );
tabs.addTab( "enabled", new JLabel("Ipsum") );
tabs.addTab( "disabled", new JLabel("Dolor") );
tabs.addTab( "enabled", new JLabel("Sit") );
tabs.addTab( "enabled", new JLabel("Amet") );
tabs.setEnabledAt( 0, false );
tabs.setEnabledAt( 2, false );
tabs.setSelectedIndex( 1 );
this.getContentPane().add( tabs );
}
public static void main( String[] args ) throws Exception {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Windows".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
SwingUtilities.invokeLater( new Runnable() {
@Override
public void run() {
new Display().setVisible( true );
}
} );
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.