[英]Method to say what jpanel is shown at the moment in a jtabbedpane
我找不到说当前显示的jpanel以及当前在屏幕上看到的内容的方法。 我尝试过isShowing(),hasFocus(),isEnabled(),但从未奏效。 谢谢你
您可以使用getSelectedComponent()
或getSelectedIndex()
获取jtabbed窗格上的当前活动面板。 考虑以下示例,这将在单击按钮时将当前活动面板的颜色设置为yello
public class JTabbedPaneDemo extends JFrame {
public JTabbedPaneDemo() {
JButton button = new JButton("color");
JPanel mainpanel = new JPanel();
JTabbedPane jtbExample = new JTabbedPane();
JPanel jplInnerPanel1 = new JPanel();
jtbExample.addTab("t1", jplInnerPanel1);
jtbExample.setSelectedIndex(0);
JPanel jplInnerPanel2 = new JPanel();
jtbExample.addTab("t2", jplInnerPanel2);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jtbExample.getSelectedComponent().setBackground(Color.yellow);
}
});
setLayout(new GridLayout(1, 1));
mainpanel.setLayout(new BorderLayout());
mainpanel.add(jtbExample, BorderLayout.CENTER);
mainpanel.add(button,BorderLayout.NORTH);
this.setContentPane(mainpanel);
this.setVisible(true);
}
public static void main(String[] args) {
new JTabbedPaneDemo();
}
}
和你的意见
myPane.getSelectedComponent()
将返回活动组件,而getSelectedComponent()
返回component
对象。因此, component
类中没有方法getComponents()
。如果要获取jpnel中的所有组件,则必须将返回的组件转换为jpanel
调用getComponents()
之前的示例
Component[] c=((JPanel)jtbExample.getSelectedComponent()).getComponents();
供您评论2
如果要验证活动对象是否为jpanel1,则将其jpanel
为jpanel
并进行如下检查。请确保已将jPanel12声明为field
变量。
if((JPanel)jTabbedPane2.getSelectedComponent()==jPanel12){
System.out.println("jPanel12 is active");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.