如何设置与gridbaglayout一起使用的定制面板的大小?

时间:2017-01-24 11:08:01

标签: java swing layout-manager gridbaglayout

这是我的问题,希望你能以某种方式帮助我。我有一个扩展JPanel的类,该类正在使用paintComponent方法创建一个矩形。当我将该课程添加到JPanel gridBagLayout的{​​{1}}时,首先不会出现。但是,当我在该类中设置Dimension.getPreferredSize()时,我可以看到矩形......问题是当我调用MouseListener并看到该矩形仅在框架中的小方块中移动时。所以我认为,某种方法getPreferredSize()正在控制矩形移动的位置。

这是我的问题的照片:

move3

limitet showing/moving4

这是代码:

主:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class PrikazGUI {

JFrame frejm;
JPanel k;
JButton b1,b2;

public PrikazGUI(){
    frejm = new JFrame("Lala");
    k = new JPanel();
    k.setLayout(new GridBagLayout());
    Kvadrat l = new Kvadrat();
    JPanel a = new JPanel();
    a.add(l);
    k.add(a);

    frejm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frejm.setSize(1900, 1000);
    frejm.getContentPane().add(k);
    frejm.setVisible(true);


}


public static void main(String[] args) {
    PrikazGUI a = new PrikazGUI();

}

第二课:

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

 import javax.swing.JPanel;

public class KvadratPravi extends JPanel {

int sizeH = 60;
int sizeW = 60;



@Override
protected void paintComponent(Graphics g) {
    // TODO Auto-generated method stub
    super.paintComponent(g);
        g.setColor(Color.PINK);
       g.drawRect(0, 0, sizeH, sizeW);
       g.fillRect(0, 0, sizeH, sizeW);
}

 @Override
 public Dimension getPreferredSize() {
     return new Dimension(sizeH,sizeW);
 }

 }

第三课:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JButton;
import javax.swing.JPanel;

public class Kvadrat extends JPanel {

JButton b1,b2;
JPanel panel;
KvadratPravi s1 = new KvadratPravi();
int x,y;


public Kvadrat(){
    GridBagConstraints cst = new GridBagConstraints();

    panel = new JPanel();
    panel.setLayout(new GridBagLayout());


    final KvadratPravi s = new KvadratPravi();
    cst.gridx = 0;
    cst.gridy = 0;
    s.setPreferredSize(new Dimension(40,40));
    s.setBounds(0, 0, 400, 400);
    panel.add(s,cst);

    JosJedanKvadrat j = new JosJedanKvadrat();
    j.setPreferredSize(new Dimension(40,40));
    j.setBounds(0, 0, 400, 400);
    cst.gridx = 0;
    cst.gridy = 4;

    panel.add(j,cst);

    s.setPreferredSize(new Dimension(400,400));
    s.setBounds(400, 400, 400, 1000);
    s.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
        if(!e.isMetaDown()){
        x = e.getX();
        y = e.getY();
        }
        }
        });
        s.addMouseMotionListener(new MouseMotionAdapter() {
        public void mouseDragged(MouseEvent e) {
        if(!e.isMetaDown()){
        Point p = s.getLocation();
        s.setLocation(p.x + e.getX() - x,
        p.y + e.getY() - y);
        }
        }
        });
    cst.gridx = 2;
    cst.gridy =4;
    panel.add(s1,cst);
    JPanel k = new JPanel();
    k.add(panel);
    add(k);

}



}

第四课:

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

import javax.swing.JPanel;

public class JosJedanKvadrat extends JPanel {

int sizeH = 60;
int sizeW = 60;



@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.BLUE);
       g.drawOval(0, 0, sizeH, sizeW);


}

 @Override
 public Dimension getPreferredSize() {
     return new Dimension(sizeH,sizeW);
 }

 }

0 个答案:

没有答案