简体   繁体   English

如何在运行时更改JTabbedpane组件的属性?

[英]How can I change properties of a JTabbedpane component at run time?

I am creating a tabbedpane 我正在创建一个选项卡式窗格

tabs= new JTabbedPane();
tabs.addChangeListener(new ChangeListener() {
    @Override
    public void stateChanged(ChangeEvent e) {
          JTabbedPane sourceTabbedPane = (JTabbedPane) e.getSource();
          int index = sourceTabbedPane.getSelectedIndex();
          sourceTabbedPane.getTabComponentAt(index).getComponentAt(0).setForeground(Color.RED);

});

tabs.setTabComponentAt(0, new JLabel("tab1"));
tabs.setTabComponentAt(0, new JLabel("tab2"));

I get this error: 我收到此错误:

The method getComponentAt(Point) in the type Component is not applicable for the arguments (int) Component类型的getComponentAt(Point)方法不适用于参数(int)

How do I get hold of the LABEL I put on the selectedTab? 如何获得放置在selectedTab上的标签? I want to set the foreground on the label. 我想在标签上设置前景。

EDIT 1

I also get a null pointer error at runtime. 在运行时,我还会收到一个空指针错误。

Component.getComponentAt accepts a Point object or two integers as int x, int y , that it looks for child components at that location. Component.getComponentAt接受一个Point对象或两个整数,如int x, int y ,它在该位置查找子组件。

http://docs.oracle.com/javase/6/docs/api/java/awt/Component.html http://docs.oracle.com/javase/6/docs/api/java/awt/Component.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM