更新时间:2023-01-06 07:39:17
JFrame
没有 paintComponent(Graphics g)
方法。添加 @Override
注释,您将收到编译时错误。
JFrame
does not have a paintComponent(Graphics g)
method. Add the @Override
annotation and you will get a compile time error.
1)使用 JPanel
和覆盖 paintComponent
(您只需将 JPanel
添加到 JFrame
viad JFrame #add(..)
)
1) Use JPanel
and override paintComponent
(you would than add JPanel
to the JFrame
viad JFrame#add(..)
)
2)覆盖 getPreferredSize()
返回正确的 Dimension
,它们适合您在Graphics对象上的绘图,否则它们将不会被视为 JPanel
没有组件的大小是0,0
2) Override getPreferredSize()
to return correct Dimension
s which fit your drawing on Graphics object or else they wont be seen as JPanel
size without components is 0,0
3)不要在 JFrame上调用
...而是使用正确的 setSize
LayoutManager
和/或覆盖 getPrefferedSize()
并调用添加所有组件后但在设置可见之前 pack()
JFrame
3) dont call setSize
on JFrame
... rather use a correct LayoutManager
and/or override getPrefferedSize()
and call pack()
on JFrame
after adding all components but before setting it visible
4) Have a read on Concurrency in Swing specifically about Event Dispatch Thread
5)监视类命名方案应以大写字母开头,此后新单词的每个首字母都应大写
5) watch class naming scheme should begin with a capital letter and every first letter of a new word thereafter should be capitalized
6)你还扩展JFrame
并有一个变量 JFrame
?拿走扩展JFrame
并保留 JFrame
变量,因为我们不想要2 JFrame
s以及扩展 JFrame
的不良做法,除非添加功能
6) Also you extend JFrame
and have a variable JFrame
? Take away the extend JFrame
and keep the JFrame
variable as we dont want 2 JFrame
s and its not good practice to extend JFrame
unless adding functionality
这是你的代码以上修复(原谅图片质量,但不得不调整大小或者是800x600):
Here is your code with above fixes (excuse picture quality but had to resize or it was going to 800x600):
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Polygon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class JRisk {
private JFrame mainMap;
private Polygon poly;
public JRisk() {
initComponents();
}
private void initComponents() {
mainMap = new JFrame();
mainMap.setResizable(false);
mainMap.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
int xPoly[] = {150, 250, 325, 375, 450, 275, 100};
int yPoly[] = {150, 100, 125, 225, 250, 375, 300};
poly = new Polygon(xPoly, yPoly, xPoly.length);
JPanel p = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
g.drawPolygon(poly);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(800, 600);
}
};
mainMap.add(p);
mainMap.pack();
mainMap.setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new JRisk();
}
});
}
}
根据您的评论:
我正在准备一张包含大量多边形的地图,昨天i
在JFrame上使用了JPanel,我试图检查鼠标是否在$ b内带有MouseListener的多边形的$ b。后来我看到mouseListener给了
false响应(就像鼠标不在多边形内部,但它像
一样,就像它在多边形内部一样)。所以我删除了JPanel然后它
工作
i am preparing a map which includes lots of polygons and yesterday i used a JPanel over a JFrame and i tried to check if mouse was inside of the polygon with MouseListener. later i saw that mouseListener gave false responds (like mouse is not inside of the polygon but it acts like it was inside the polygon). so i deleted the JPanel and then it worked
以下是使用 MouseAdapter更新的代码
并覆盖 mouseClicked
以检查点击是否在多边形内。
Here is updated code with MouseAdapter
and overridden mouseClicked
to check if click was within polygon.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class JRisk {
private JFrame mainMap;
private Polygon poly;
public JRisk() {
initComponents();
}
private void initComponents() {
mainMap = new JFrame();
mainMap.setResizable(false);
mainMap.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
int xPoly[] = {150, 250, 325, 375, 450, 275, 100};
int yPoly[] = {150, 100, 125, 225, 250, 375, 300};
poly = new Polygon(xPoly, yPoly, xPoly.length);
JPanel p = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
g.drawPolygon(poly);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(800, 600);
}
};
MouseAdapter ma = new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent me) {
super.mouseClicked(me);
if (poly.contains(me.getPoint())) {
System.out.println("Clicked polygon");
}
}
};
p.addMouseListener(ma);//add listener to panel
mainMap.add(p);
mainMap.pack();
mainMap.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new JRisk();
}
});
}
}