2016-04-27 247 views
1

我是java GUI编程的新手。 我创建了一个JFrame。 在这个JFrame里面,我创建了一个JPanel。 在这JPanel里面我创建了另一个JPanel(让我们称之为“A”)。JFrame中看不到JPanel中的子JPanel

我在“A”中绘制了一个矩形,并且想要使用图形创建按钮。 我的gui中没有矩形。我可以看到“A”中的paintComponent()方法未被调用。

代码: 的JPanels:(孩子JPanel是内部类)

public class MemoryPanel extends JPanel { 

    public MemoryPanel(){ 
     setPreferredSize(new Dimension(350,448)); 
    } 

    @Override 
    public void paintComponent(Graphics g) {  
     //POSITIONING 
     setLayout(new BorderLayout()); 

     //CREATE MEMORY BUTTONS 
     MemButton a=new MemButton(); 

     //Drawing Rectangles for Memory 
     add(a,BorderLayout.CENTER); 

    } 



    private class MemoryButton extends JPanel{ 
     public MemoryButton(){ 
      setLayout(null); 
      setPreferredSize(new Dimension(87,40)); 
     } 

     @Override 
     public void paintComponent(Graphics g){ 
      Graphics2D td= (Graphics2D)g; 
      td.drawRect(0, 0, 20, 20); 
     } 
    } 
} 

编辑: 感谢所有, 我不得不引起与它相同的类名的问题,另一个包。它现在看起来很有效。

+1

1)'paintComponent'方法应该用于绘制图形,而不是用于创建和添加组件。在构造函数中这样做。 2)如果您将按钮添加到“BorderLayout.CENTER”,则“setPreferredSize”不会达到您所期望的。该按钮将占用整个可用空间。 3)不要忘记在'paintComponent'方法的开头调用'super.paintComponent()'。此外,MemButton与MemoryButton相同吗? - 请考虑发布重现问题的[mcve]。 –

回答

1

我做了一个简单的GUI来测试你的代码,矩形显示正确。 我没有在您发布的代码中进行相关更改。

import javax.swing.JFrame; 
import javax.swing.SwingUtilities; 

public class SimpleJFrameProgram extends JFrame { 
    private static final long serialVersionUID = 1L; 

    public SimpleJFrameProgram() { 
     super("TEST"); 

     initComponents(); 

     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.pack(); 
     this.setLocationRelativeTo(null); 
     this.setVisible(true); 
    } 


    private void initComponents() { 
     MemoryPanel memoryPanel = new MemoryPanel(); 

     this.add(memoryPanel); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        new SimpleJFrameProgram(); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 
} 

我applyed小的改动你的MemoryPanel:你MemoryButton取代MemButton和填充矩形红色,以提高其对测试的知名度。没有这个最后的改变,矩形也会出现。

import java.awt.BorderLayout; 
    import java.awt.Color; 
    import java.awt.Dimension; 
    import java.awt.Graphics; 
    import java.awt.Graphics2D; 

    import javax.swing.JPanel; 

    public class MemoryPanel extends JPanel { 

    public MemoryPanel(){ 
     setPreferredSize(new Dimension(350,448)); 
    } 

    @Override 
    public void paintComponent(Graphics g) { 
     // POSITIONING 
     setLayout(new BorderLayout()); 

     // CREATE MEMORY BUTTONS 
     MemoryButton a = new MemoryButton(); 

     // Drawing Rectangles for Memory 
     add(a,BorderLayout.CENTER); 

    } 

    private class MemoryButton extends JPanel{ 
     public MemoryButton(){ 
      setLayout(null); 
      setPreferredSize(new Dimension(87,40)); 
     } 

     @Override 
     public void paintComponent(Graphics g) { 

      Graphics2D td = (Graphics2D) g; 
      td.setColor(Color.red); 
      td.fillRect(0, 0, 20, 20); 
     } 
    } 
} 

这是得到的结果:

enter image description here

也许你的问题是位于初始化父JFrame

+1

谢谢,我发现了这个问题。我在同一个项目中使用了另一个名称为“MemButton和从那里初始化的对象”的包。 – ALUFTW

2

您应该先编程JButtons,以使您的图形能够像按钮一样工作。我相信这个帖子会帮你:

Creating a custom button in Java

我想要一个矩形成为你的按钮的背景,你可以把它收回去你的主面板,并添加它的按钮。尝试使用不同的布局来维持一些顺序。