【问题标题】:Drawing from another class wont take the right dimensions of the JPanel从另一个类绘图不会采用 JPanel 的正确尺寸
【发布时间】:2013-12-07 15:23:47
【问题描述】:

好的,我有一个这样的 JPanel:

 public class GUI {
 JFrame frame = new JFrame("Net");

 JPanel panel = new JPanel();
  public GUI()
{
frame.setSize(835,650);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);          
frame.add(panel);
panel.setSize(600,600);
panel.setLocation(215,5);
panel.add(new DrawPlanes(300,300,200,Color.BLACK));}

那里的表格等还有其他一些面板。我的主要是这个:

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable(){ 
@Override
public void run(){
//new GUI();
new GUI().buildTable();

 }

});

我还有另一门课:

public class DrawPlanes extends JPanel 
{    

private static int centreX, centreY, radius;
private Color colour;

public DrawPlanes()
{
centreX = 300;
centreY = 300;
radius = 200;
colour = Color.BLACK;
}

public DrawPlanes(int centreX,int centreY, int radius, Color colour)
{
this.centreX = centreX;
this.centreY = centreY;
this.radius = radius;
this.colour = colour;

}

@Override
protected void paintComponent(Graphics g)
{

super.paintComponent(g);
System.out.println("ppp");
Graphics2D g2D = (Graphics2D) g;           
g2D.setStroke(new BasicStroke(2F));  
g.setColor(Color.BLACK);
g.drawOval(centreX - radius , centreY - radius, radius * 2 , radius * 2);
......
}
}

好的,所以我已将面板的背景颜色设置为红色,以查看整个面板上发生的情况,它是红色的,但我相信图纸所在的位置有一个灰色的小方块。我尝试更改 opaque,因为可能存在不兼容问题,但没有任何改变。任何建议,我有什么遗漏的吗?

结果的链接

 panel.add(new DrawPlanes(300,300,200,Color.BLACK))

http://dc626.4shared.com/img/FeYopZC1/s7/142d22f1be0/2013-12-08_142846.png?async&rand=0.9010817544924218

当我检查 DrawPlanes 类在 DrawPlanes 本身 http://dc626.4shared.com/img/NPDkiQRJ/s7/142d22f23b0/1451491_586878858047235_191988.jpg?async&rand=0.27479583155781395 中具有主和面板等时,它会绘制什么。当我应用布局管理器时,当我使用 getPreferredSize 覆盖或不覆盖时,灰色方块只会移动到中心整个红色面板显示为灰色。

【问题讨论】:

    标签: java swing class object jpanel


    【解决方案1】:
    • frame.add(component) 函数最终将您的组件添加到框架的内容窗格中,其中 BorderLayout 作为默认布局管理器。
    • JPanel 使用 FlowLayout 作为默认布局,它尊重组件的首选大小。
    • 由于您的 'panel' 和 'frame' 默认满足上述两个条件,因此带有 setSize(Dimension)setBounds(Dimenstion) 的组件大小提示将不起作用。
    • 您应该使用 setPreferredSize(Dimenstion)to DrawPanel 上下文的实例)提供尺寸提示,如果需要指定最小/最大尺寸 setMinimumSize(Dimenstion)setMaximumSize(Dimenstion)
    • 但是,覆盖getXXXSize(Dimenstion) 被认为是一种更好的做法:xxx 始终代表Preferred/Minimmum/Maximum,它允许根据其内容调整组件的大小。
    • 与其在窗口上调用setSize(Dimension),不如在添加子组件结束时调用pack()
    • 我们应该在完成所有子组件的添加后调用setVisible(true)pack()

    请从教程开始:Laying Out Components Within a Container


    编辑:

    让我们编辑您的 GUI() 构造函数代码,看看会发生什么:

    public GUI()
    {
      frame.setSize(835,650);
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      //frame.setVisible(true);   // <<--- call it at the end of the code      
      frame.add(panel);
      //panel.setSize(600,600); <<--- removing set size
    
      panel.add(new DrawPlanes(300,300,200,Color.BLACK));
      frame.setVisible(true);
    }
    

    DrawPnales 将扩展JComponent

    public class DrawPlanes extends JComponent{
        public DrawPlanes(int centreX,int centreY, int radius, Color colour)
        {
          this.centreX = centreX;
          this.centreY = centreY; 
          this.radius = radius;
          this.colour = colour;
        }
    
        @Override
        public Dimension getPreferredSize() {
    
             return new Dimenstion(width, height);
                                   // ^ provide your required size 
        }
    
    }
    

    如果这对您仍然没有任何意义,那么请尝试进一步学习 Swing 布局管理器。否则,无论我们如何用头撞桌子,都可能一事无成。

    【讨论】:

    • 好的,我明白你在说什么,你和 JB Nizet 我刚刚仔细阅读了教程,你告诉我的很有意义,我刚刚尝试了 getPreferredSize 我已经覆盖了它使用它的约束尝试了 GridBagLayout 我尝试了 MigLayout 我唯一能做的就是将这个灰色的小方块从顶部中心移动到中心而不需要调整大小。实际上它只适合面板一秒钟完全空然后转向一个灰色的小方块。
    • @Renobatio,请在您的问题中附上一张图片(上传到图片主机并附上链接),描述您的要求。首先,您可以从FlowLayout/BorderLayout 开始。并不是说这种布局可能会实现您的目标,但在了解您的需求之前很难选择合适的布局管理器
    • 我不能使用两个以上的链接,所以我只发布了这两个,希望它对你有用。
    • 您实际上并未共享完整的布局代码。编辑了答案。检查它是否解决了您的问题。如果没有,那么请先努力理解摆动布局管理器。我们不能一口气爬山,甚至一天也爬不上
    • 我刚试过,现在整个面板没有出现我有灰色区域。似乎比我想象的要困难得多。 :/ 你有更多的建议可以尝试吗,可以说我应该尝试 FlowLayout Manager 并使用它的约束吗?!?!我应该更改整个 GUI 类并将其引入 FlowLayout (希望不会因为我相信某些东西会那样出错)。我只是担心如果 GridBag 和 Mig 没有设法完成工作,其他人都不会完成它:/
    【解决方案2】:

    您错过了使用 LayoutManager 在其容器内布置面板的事实。 JPanel 的默认布局管理器是 FlowLayout。 FlowLayout 使用其布局的组件的首选宽度和高度来决定将它们放置在容器中的哪个位置,以及它们应该具有的大小。但是您的 DrawPlanes 面板不会覆盖 getPreferredSize(),因此它的首选大小是默认值。

    每次在组件或框架上使用setSize(),您有 99.9% 的可能性做错事。学习布局管理器。如果你设计了一个像 DrawPlanes 组件这样的自定义组件,它不仅是其他组件的容器,而且有一个自定义的 paintComponent() 方法,然后覆盖 getPreferredSize()getMaximumSize()getMinimumSize() 以告诉布局管理器他们如何应该显示您的组件,并确保您的自定义组件始终具有适当的大小。你从来没有设置过 JButton 的大小,对吧?这是因为 JButton 本身会根据它包含的文本和图标来决定它应该具有的大小。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多