简体   繁体   English

如何在Java中添加组件并将其放在JFrame上?

[英]How To Add A Component And Bring It To Front On A JFrame In Java?

I am making a Java Class that's constructor will get a JForm from its parameter and then will add a JTable on that form programmatically as shown in below code... 我正在构造一个Java类,其构造JForm将从其参数中获取JForm ,然后以编程方式在该表单上添加JTable ,如以下代码所示...

public class DEMOCLASS
{
private JTable myJTable = new JTable();
public DEMOCLASS(JFrame incomingForm)
    {  
        incomingForm.add(myJTable);
        myJTable.setSize(coloumWidth,rowHeight);
        myJTable.setLocation(xpos, ypos);
        myJTable.setVisible(true);  
        //incomingForm.setComponentZOrder(myJTable,0);    // Its Bringing The Component To From But Spreading It All Over The Form From Top To Bottom And Left To Right Fully :(      
    }
}

Now the main problem is that where this class is called, there are many component already added by some other coding and I want to bring myJTable component on the front that is now hiding behind them. 现在的主要问题是,在调用该类的地方,其他一些编码已经添加了许多组件,而我想将myJTable组件放在前面,现在隐藏在它们的后面。 In short, I cant control others already added component. 简而言之,我无法控制其他已经添加的组件。 I also heard about MyGlassPane and JLayeredPane but cant make them work with my concept here. 我也听说过MyGlassPaneJLayeredPane但在这里无法使它们与我的概念一起使用。

So my Main question is how to bring this myJTable at the front of all previously added components on the incomingForm ? 因此,我的主要问题是如何将myJTable放在incomingForm myJTable上所有先前添加的组件的最前面?

The default painting logic of Swing is that the last component added to a panel is painted first. Swing的默认绘制逻辑是首先绘制添加到面板的最后一个组件。

You can change this by playing with the ZOrder as you attempted to do with your commented out code. 您可以通过尝试使用ZOrder注释代码来更改此设置。

However Swing is not really designed to have components painted on top of one another. 但是,Swing并不是真正设计成将组件彼此绘制在一起。 Components are typically positioned in a 2D area, not 3D. 组件通常位于2D区域,而不是3D区域。 Your reason for doing this is unclear. 您这样做的原因尚不清楚。

I would suggest you could: 我建议您可以:

  1. Use a child JDialog to display the table and its data (you can make the dialog undecorated to remove the Borders). 使用子JDialog来显示表及其数据(您可以使对话框未修饰以删除边框)。
  2. Maybe use a GlassPane to display the table. 也许使用GlassPane来显示表格。

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

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