簡體   English   中英

Java JPanel JFrame獲取高度和寬度

[英]Java JPanel JFrame getting Height and Width

我目前正在一個Java項目中,該項目由一個框架,一個主面板和3個子面板組成。在主面板的北部,我有我的dataPanel,在西部,我有我的buttonPanel,在中心有我的drawPanel

我的DrawPanel應該顯示5個圓的首字母,每當單擊創建按鈕時,它應該繪制X個由用戶指定的圓。 但是我希望圓心在DrawPanel中全部可見,這意味着從面板上切下的圓不超過1/2。 我不希望設置長度和寬度來使它相對於框架/面板尺寸動態變化。

這是我的代碼

public class MainPanel extends JPanel{

private DataPanel data;
private JPanel buttonPanel;
private DrawPanel dPanel;

private JButton create;
private JButton sort;
private JButton coCenter;
private JButton reset;

public MainPanel()
{

    setLayout(new BorderLayout());

    data = new DataPanel();
    add(data, BorderLayout.NORTH);

    buttonPanelInitialize();
    add(buttonPanel,BorderLayout.WEST);

    int a,b,c;
    a = data.getDataField();
    b = data.getDataField1();
    c = data.getDataField2();
    dPanel = new DrawPanel(a,b,c);
    add(dPanel, BorderLayout.CENTER);

}

private void buttonPanelInitialize()
{
    buttonPanel = new JPanel();
    buttonPanel.setLayout(new GridLayout(4,1));
    buttonCreate();
    buttonSort();
    buttonCoCenter();
    buttonReset();

    buttonPanel.add(create);
    buttonPanel.add(sort);
    buttonPanel.add(coCenter);
    buttonPanel.add(reset);

}

private void buttonCreate()
{
    create = new JButton("Create");
    class cListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent event) {

            //int amount = 0; 
            int amount = data.getDataField();
            int smallestR = data.getDataField1();
            int largestR = data.getDataField2();

            dPanel.create(amount,smallestR,largestR);
        }
    }

    ActionListener createListener = new cListener();    
    create.addActionListener(createListener);

}

public class DrawPanel extends JPanel{

private ArrayList<ColorCircle> circles;
private int numberOfCircles;
private int smallestRadiusSize;
private int biggestRadiusSize;
private int width;
private int height;
public DrawPanel()
{

    circles = new ArrayList<ColorCircle>();

}
public DrawPanel(int number, int smallestRadius, int biggestRadius)
{

    circles = new ArrayList<ColorCircle>();
    create(number,smallestRadius,biggestRadius);
    width = (int)Math.random()*getWidth();
    height = (int)Math.random()*getHeight();
}

public void create(int number, int smallestRadius, int biggestRadius)
{
    numberOfCircles = number;
    smallestRadiusSize = smallestRadius;
    biggestRadiusSize = biggestRadius;
    for(int i = 0; i < numberOfCircles ; i++)
    {
        int radius = (int) ((int)smallestRadiusSize+Math.random()*((int)biggestRadiusSize-(int)smallestRadiusSize+1));
        width = (int) ((int)100+Math.random()*701);    //the problem is here
        height = (int) ((int)100+Math.random()*501);   //and here this part should be dynamic
        circles.add(new ColorCircle(width,height,radius));  
        System.out.println(smallestRadiusSize);
        System.out.println(biggestRadiusSize);
        System.out.println(radius+"-----");
        System.out.println(circles.size());
        System.out.println(width+" THis is x");
        System.out.println(height+" THIs is Y");
        repaint();
    }   
}

public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    for(ColorCircle c : circles)
    {
        c.fill(g2);
    }
}

無論如何,使其相對於框架/面板尺寸動態變化

您將AncestorListener添加到面板中並處理ancestorAdded事件。 將面板添加到可見GUI時將生成此事件。

因此,這意味着需要從此方法(而不是構造函數)中調用您的create(...)方法。 然后,在執行代碼時,可以使用面板的getWidth()getHeight()方法來獲取面板的實際大小並進行處理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM