Java-Swing / AWT面板移位

时间:2014-08-04 23:25:31

标签: java swing

我有一个添加了两个JPanel的Jframe(请参阅BoardGUI类)。第一个JPanel(emptyPanel)是空的,因为我想要考虑板的空间(由DoDrawing -BoardGUI绘制)。在第二个JPanel(RightPanel)上,我想添加按钮和其他组件。当我开始添加按钮时,我注意到它们移动了几个像素。创建了板并且按钮在一个位置创建,然后它们将移动(不按按钮或与UI交互)。我为背景添加了一种颜色,并删除了下面代码中的按钮。通过运行此代码,我确认RightPanel正在移动,有时是水平移动,有时垂直移动。我试图设置一个边框布局,在EAST上添加RightPanel,同样的事情发生了。我已经尝试修复JPanes的大小(通过设置Preferred,Maximum和Minimum)和相同的东西。我不知道还能做什么。我花了好几个小时试图找到解决方案。

关于代码:从main调用SelectionScreen类,其中包含板的平方数。因此,例如,具有3行和2列的板将调用SelectionScreen(3,2)。该类创建Jframe并添加由BoardGUI创建的pJPanel和board。

public class SelectionScreen extends JFrame{

public static final int Maxwidth = Toolkit.getDefaultToolkit().getScreenSize().width - 40;
public static final int Maxheight = Toolkit.getDefaultToolkit().getScreenSize().height -40;
public static int fwidth;
public static int fheight;

public SelectionScreen (int h, int w){
 Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
 JFrame f=new JFrame("Movement & Target");

 System.out.println( "heigth " + h + " and w " + w  );

 if (h <=4 && w <=4){
     fwidth = (Maxwidth*2)/3;
     fheight = (Maxheight*2)/3;

    f.setSize (fwidth ,fheight);
 }

 else{ 

     f.setSize (Maxwidth, Maxheight);
     fwidth = Maxwidth;
     fheight = Maxheight;
}


f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocation(dim.width/2-f.getSize().width/2, dim.height/2-f.getSize().height/2);

BoardGUI board = new BoardGUI(fheight, fwidth, h, w);

f.add(board);

f.setVisible(true);


}

}

`

public class Board GUI extends JPanel {

private final int frameheight;
private final int framewidth;
private final int squaresheight;
private final int squareswidth;
public static int squaresize;
public final int panelwidth ;
public final int panelheight;
private int horizontalgap;
private int verticalgap;

private BufferedImage dark;
private BufferedImage light;
private TexturePaint darktp;
private TexturePaint lighttp;


    public BoardGUI( int fh, int fw, int sh, int sw) {   

        frameheight = fh;
        framewidth = fw;
        squaresheight = sh;
        squareswidth  = sw; 
        panelwidth = (3*framewidth)/5;
        panelheight = frameheight-70; 
        sizepanel2= framewidth-panelwidth;

        this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
        JPanel emptyPanel = new JPanel();
        emptyPanel.setPreferredSize(new Dimension(panelwidth, frameheight));
        emptyPanel.setMaximumSize(new Dimension(panelwidth, frameheight));
        emptyPanel.setMinimumSize(new Dimension(panelwidth, frameheight));
        emptyPanel.setBackground(new Color(0,0,0,65));


        JPanel RightPanel = new JPanel ();



        RightPanel.add(new JButton ("hello"));
        RightPanel.add(new JButton ("hello2"));
        RightPanel.setBackground (new Color(0,0,0,145));


        this.add(emptyPanel);
        this.add(RightPanel);


    }

    private void loadImages() {

        try {

            dark = ImageIO.read(new File("cementdark.jpeg"));
            light = ImageIO.read(new File("cementlight.jpeg"));

            System.out.println("It loaded");
        } catch (IOException ex) {

            Logger.getLogger(Surface.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void doDrawing(Graphics g) {

        Graphics2D g2d = (Graphics2D) g;
        //this.setBounds (20,20,panelwidth, panelheight);
        if (squaresheight > squareswidth){
            if (panelheight < panelwidth)
                    squaresize = panelheight/squaresheight;
            else
                    squaresize = panelwidth/squaresheight;
        }

        else{
            if (panelheight < panelwidth)
                    squaresize = panelheight/squareswidth;
            else
                    squaresize = panelwidth/squareswidth;
        }

        System.out.println("frame width and height" + frameheight  + " and " + framewidth);
        System.out.println("width and height" + panelheight  + " and " + panelwidth);

       horizontalgap = abs(panelwidth - (squaresize* squareswidth))/2;

       verticalgap = abs(panelheight - (squaresize* squaresheight))/2+20;

       darktp = new TexturePaint(dark, new Rectangle(0, 0, squaresize, squaresize));
       lighttp = new TexturePaint(light, new Rectangle(0, 0, squaresize, squaresize));

       System.out.println("squaresize " + squaresize);

       System.out.println("vertical and horizontal " + verticalgap + " and " + horizontalgap);

       for (int y = 0; y< squaresheight; y++)        
       {
          for (int x = y%2; x< squareswidth ; x+=2) {

           if (y%2==0){
                g2d.setPaint(darktp);
                g2d.fillRect(horizontalgap +squaresize*x, 
                        verticalgap + squaresize*y, squaresize, squaresize);  
            }

           else{
                g2d.setPaint(darktp);
                g2d.fillRect((horizontalgap+squaresize*x),
                        verticalgap + squaresize*y, squaresize, squaresize);      

            } 
          }    
        }  

       for (int y = 0; y< squaresheight; y++)        
       {
          for (int x = (y+1)%2; x< squareswidth ; x+=2) {

           if (y%2==1){
                g2d.setPaint(lighttp);
                g2d.fillRect(horizontalgap +squaresize*x, 
                        verticalgap + squaresize*y, squaresize, squaresize);  
            }          
           else{
                g2d.setPaint(lighttp);
                g2d.fillRect((horizontalgap+squaresize*x),
                        verticalgap + squaresize*y, squaresize, squaresize);                     
            } 
          }    
        }   
    }


@Override
     public void paintComponent(Graphics g) {

        super.paintComponent(g);

        loadImages(); 
        doDrawing(g); // draws board  


    }
}

0 个答案:

没有答案