Java JPanel JFrame获取高度和宽度
我目前正在从事一个java项目,该项目由一个框架、主面板和3个子面板组成。在主面板的北面我有我的dataPanel,在西面我有我的buttonPanel,在中间我有我的drawPanel 我的DrawPanel应显示5个圆圈的首字母,每当单击“创建”按钮时,它应绘制用户指定的X个圆圈。但是,我希望圆的中心点在DrawPanel中都是可见的,这只意味着任何圆的1/2都不会从面板上被切掉。我不希望设置长度和宽度,以使其相对于框架/面板大小具有动态性 这是我的密码Java JPanel JFrame获取高度和宽度,java,swing,Java,Swing,我目前正在从事一个java项目,该项目由一个框架、主面板和3个子面板组成。在主面板的北面我有我的dataPanel,在西面我有我的buttonPanel,在中间我有我的drawPanel 我的DrawPanel应显示5个圆圈的首字母,每当单击“创建”按钮时,它应绘制用户指定的X个圆圈。但是,我希望圆的中心点在DrawPanel中都是可见的,这只意味着任何圆的1/2都不会从面板上被切掉。我不希望设置长度和宽度,以使其相对于框架/面板大小具有动态性 这是我的密码 public class MainP
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);
}
及
公共类DrawPanel扩展了JPanel{
私人ArrayList圈子;
私人圈子;
私有化;
私有int BIGGESTRADIUSIZE;
私有整数宽度;
私人内部高度;
公共事务委员会()
{
圆圈=新的ArrayList();
}
公共绘图面板(整数编号、整数小半径、整数大半径)
{
圆圈=新的ArrayList();
创建(数字、小半径、大半径);
宽度=(int)Math.random()*getWidth();
高度=(int)Math.random()*getHeight();
}
创建公共空心(整数编号、整数小半径、整数大半径)
{
numberOfCircles=数字;
smallestradiusize=smallestRadius;
biggestRadiusSize=biggestRadius;
for(int i=0;i
是否存在使其相对于框架/面板大小动态的方法
您可以向面板添加一个antestorlistener
,并处理antestoradded
事件。将面板添加到可见GUI时将生成此事件
因此,这意味着您的create(…)
方法需要从该方法(而不是构造函数)调用。然后在执行代码时,您可以使用面板的getWidth()
和getHeight()
方法来获取面板的实际大小并进行处理
是否存在使其相对于框架/面板大小动态的方法
您可以向面板添加一个antestorlistener
,并处理antestoradded
事件。将面板添加到可见GUI时将生成此事件
因此,这意味着您的
create(…)
方法需要从该方法(而不是构造函数)调用。然后在执行代码时,您可以使用getWidth()
和getHeight()
获取面板实际大小并进行处理的面板方法。hm好的,我以前没有使用过ancestorlistener,但我会尝试一下。m好的,我以前没有使用过ancestorlistener,但我会尝试一下
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);
}
}