gpt4 book ai didi

Java JPanel JFrame 获取高度和宽度

转载 作者:行者123 更新时间:2023-12-02 03:15:37 26 4
gpt4 key购买 nike

我目前正在开发一个java项目,它由一个框架、主面板和3个子面板组成。在主面板的北边我有我的dataPanel,在西边我有我的buttonPanel,在中心我有我的有我的绘图面板

我的 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);
}
}

最佳答案

is there anyway to make it dynamic relative to the frame/panel size

您将 AncestorListener 添加到面板并处理 ancestorAdded 事件。当面板添加到可见 GUI 时,将生成此事件。

所以这意味着您的 create(...) 方法需要从此方法(而不是构造函数)调用。然后,当代码执行时,您可以使用面板的 getWidth()getHeight() 方法来获取面板的实际大小并进行处理。

关于Java JPanel JFrame 获取高度和宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40353322/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com