当我运行程序时,我遇到了 JFrame Buffer 的一些问题,我不知道问题到底是什么。当我运行该程序时,它会在缓冲区的左上角显示一些对话框部分。
这是我的程序的输出:
下面是代码
谢谢。
import java.awt.*;
import java.util.*;
import javax.swing.*;
public class Main extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int[] x1 = new int[10];
int[] y1 = new int[10];
int i,n;
Polygon p=new Polygon();
n = Integer.parseInt(JOptionPane.showInputDialog("Enter no. of co-ordinates of polygon: "));
System.out.println(" no. of co-ordinates of polygon are :"+n);
for(i=0;i<n;i++)
{
x1[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter x co-ordinates of polygon: "));
y1[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter y co-ordinates of polygon: "));
}
for(i=0;i<n-1;i++)
{
g.drawLine(x1[i],y1[i],x1[i+1],y1[i+1]);
}
g.drawLine(x1[n-1],y1[n-1],x1[0],y1[0]);
}
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setTitle("Polygon");
frame.setSize(500,500);
Container contentPane = frame.getContentPane();
contentPane.add(new Main());
frame.setVisible(true);
}
}
最佳答案
从不通过绘画方法显示 JOptionPane。绘画方法仅用于绘画,不用于获取用户输入。
相反,您需要执行以下操作:
应该从 main 方法中显示 JOptionPane 以收集 x/y 参数。
修改您的 Main() 类,使其具有类似于
addPoint(int x, int y)
的方法。上面的方法会将 x/y 值保存到类中的 ArrayList 对象中。我会将 Point 对象存储在此列表中。
绘画方法将遍历列表并绘制每一行。
关于Java JFrame 对话框问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35959967/