2016-02-19 17 views
0

我一直无法让绘图组件在第三个屏幕上绘制任何东西。我用这个简单的代码测试过它,发现了一件事。如果我将矩形的坐标设置为0,0,那么一个小的小点出现在屏幕的中间顶部,大小应该是它的一小部分。JPanel在使用cardLayout时不会显示paintComponent

import java.awt.CardLayout; 
import java.awt.Color; 
import java.awt.Component; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Toolkit; 
import java.awt.event.*; 
import java.util.Random; 
import javax.swing.*; 

public class hello extends JPanel { 
    public int[][] room = new int[20][20];// Whether the room is solid or hollow 

    public static Random r = new Random(); 
    boolean toggle, GameEnable; 
    int i, j, px, py, temp, endx, endy; 
    private static final long serialVersionUID = 1L; 
    JFrame f = new JFrame("Maze Game"); 

    JPanel p0 = new JPanel(); 
    JPanel p1 = new JPanel(); 
    JPanel p2 = new JPanel(); 
    JPanel p3 = new JPanel(); 
    JPanel p4 = new JPanel(); 

    JButton b1 = new JButton("Screen 2"); 
    JButton b2 = new JButton("Give Visuals"); 

    CardLayout cl = new CardLayout(); 
    public void Game() { 
     p0.setLayout(cl); 

     p1.add(b1); 
     p2.add(b2); 
     p3.add(new GameVisual()); 
     p1.setBackground(Color.RED); 
     p2.setBackground(Color.BLACK); 
     p4.setBackground(Color.PINK); 

     p0.add(p1, "1"); 
     p0.add(b2, "2"); 
     p0.add(p3, "3"); 
     p0.add(p4, "4"); 
     cl.show(p0, "1"); 

     final Timer timer = new Timer(10 * 60 * 1000, new ActionListener() { 
      public void actionPerformed(final ActionEvent e) { 
       cl.show(p0, "4"); 
      } 
     }); 

     b1.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       cl.show(p0, "2"); 
      } 
     }); 
     b2.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       cl.show(p0, "3"); 
       timer.start(); 
      } 
     }); 

     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.setSize(816, 816); 
     f.setVisible(true); 
     f.setLocationRelativeTo(null); 
     f.add(p0); 
     timer.start(); 
    } 

    class GameVisual extends JPanel { //This is a sub class not a separate class file 
     private static final long serialVersionUID = 2L; 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      g.setColor(Color.BLUE); 
      g.fillRect(10,10, 400, 400); 
     } 
    } 

    public static void main(String[] args) { 
     new hello().Game(); 
    } 
} 

所以我的问题是,为什么不apearing我的paintComponent的屏幕(或大小的位置不正确的分数显示,我怎么可能会解决这个问题。 PS如果可能的话我想保留一切在同一班上,而不是把它们放到不同的班级,因为我使用数组来做大部分我的绘画在这个代码的较大版本。

回答

1

首先,我想你的意思是将p2添加到p0(而不是b2) :

p0.add(p2, "2"); 

二,如果你想看到完整的矩形,你可以这样做:

GameVisual gv = new GameVisual(); 
gv.setPreferredSize(new Dimension(500,500)); 
p3.add(gv); 

然而,当你添加组件GameVisual,它会调整以适应这些组件。所以也许你不应该设置首选大小。

+0

好的,所以当它到达第三个屏幕时,它会调整屏幕大小以适应视觉?我的理解正确吗? – MrFAYAZ666

+0

是的。作为一个测试,不要使用上面显示的setPreferredSize()方法。相反,只需将组件添加到GameVisual。您将看到蓝色矩形增长,因为JPanel必须增长以适应组件。我不知道你最终的GameVisual面板应该是什么样子,但它会增长以适合你添加到它的组件。 –