gpt4 book ai didi

Java 多边形调整大小

转载 作者:行者123 更新时间:2023-12-01 13:27:57 25 4
gpt4 key购买 nike

我正在尝试制作一个程序,该程序有一个显示多边形和两个按钮的窗口。多边形从 3 个点(一个三角形)开始,并允许用户按“+”和“-”按钮来添加或减去多边形的边。这是我的代码:

在测试多边形框中:

package testpolygonbox
import javax.swing.*;
import java.awt.*;
public class TestPolygonBox extends JFrame {


public TestPolygonBox(){
setLayout(new BorderLayout(5,5));
add(new PolygonBox ());
}
public static void main(String[] args) {
TestPolygonBox frame = new TestPolygonBox();
frame.setTitle("Polygon Box");
frame.setSize(400,420);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

}
}

接下来是控制类:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class PolygonBox extends JPanel {
private JButton jbtEnlarge = new JButton("+");
private JButton jbtShrink = new JButton("-");
private PolygonPanel polygonPanel = new PolygonPanel();

public PolygonBox(){
JPanel panel = new JPanel();
panel.add(jbtEnlarge);
panel.add(jbtShrink);
setLayout(new BorderLayout());
this.add(polygonPanel,BorderLayout.CENTER);
this.add(panel, BorderLayout.SOUTH);

jbtEnlarge.addActionListener(new EnlargeListener());
jbtShrink.addActionListener(new ShrinkListener());
}


class EnlargeListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e){
polygonPanel.enlarge();
}
}
class ShrinkListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e){
polygonPanel.shrink();
}
}
}

class PolygonPanel extends JPanel{
private int polygonSides = 3;

/** Add side to the polygon*/
public void enlarge(){
polygonSides++;
repaint();
}
public void shrink(){
polygonSides--;
repaint();
}
@Override /** Draw requested Shape*/
protected void paintComponent(Graphics g){

int frameWidth = getWidth() / 2;
int frameHeight = getHeight() / 2;
int radius = (int)(Math.min(getWidth(),getHeight())* 0.4);
int xCenter = getWidth() / 2;
int yCenter = getHeight() / 2;


g.setColor(Color.BLUE);
Polygon polygon = new Polygon();

polygon.addPoint(xCenter + radius, yCenter);
polygon.addPoint((int)(xCenter + radius * Math.cos(2 * Math.PI/polygonSides)),
(int)(yCenter - radius * Math.sin(2 * Math.PI / polygonSides)));
for (int polygonPoint = 2; polygonPoint <= polygonSides; polygonPoint++){
polygon.addPoint((int)(xCenter + radius * Math.cos(polygonPoint * 2 * Math.PI/polygonSides)),
(int)(yCenter - radius * Math.sin(polygonPoint * 2 * Math.PI / polygonSides)));
}

g.fillPolygon(polygon);
}


}

当我尝试运行该程序时,出现错误:线程“main”中的异常 java.lang.IllegalArgumentException:向容器添加窗口 在 java.awt.Container.checkNotAWindow(Container.java:483) 在 java.awt.Container.addImpl(Container.java:1084) 在 java.awt.Container.add(Container.java:998) 在 javax.swing.JFrame.addImpl(JFrame.java:562) 在 java.awt.Container.add(Container.java:410) 在 testpolygonbox.TestPolygonBox.(TestPolygonBox.java:21) 在 testpolygonbox.TestPolygonBox.main(TestPolygonBox.java:24)Java 结果:1

如果有人能告诉我在哪里向容器添加窗口?我不确定我做错了什么。

最佳答案

PolygonBox 是一个 JFrame 窗口,但您要将其添加到另一个 JFrame 中。更改该类,使其扩展 JPanel

public class PolygonBox extends JPanel {

关于Java 多边形调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21709619/

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