這...
public class Ch3PA extends JFrame {
//...
public PaintPanel()
是沒有意義的。您已經定義了一個名爲Ch3PA
類,而是定義了一個名爲PaintPanel
構造它
然後定義構造函數中的類....
static class PaintPanel extends JPanel
{
polygon.addPoint(40, 20);
polygon.addPoint(70, 40);
polygon.addPoint(60, 80);
polygon.addPoint(45, 45);
polygon.addPoint(20, 60);
@addMouseMotionListener(new MouseAdapter()
{
public void mouseMoved(MouseEvent e)
{
point.x = e.getX();
point.y = e.getY();
if (polygon.contains(e.getX(), e.getY()))
{
inside = true;
repaint();
}
else
{
inside = false;
repaint();
}
}
});
即使這是合法的,在polygon.addPoint
韓元」將不起作用,因爲你想一個可執行上下文之外執行代碼(方法,構造函數,static
塊)
@addMouseMotionListener
是非法的語法,你不應該用@
0領先
不要叫repaint();
任何paint方法裏面,這會消耗你的CPU與上房重繪
你打電話super.paintComponents
< - 注意s
,這是不是你正在尋找的方法。你應該打電話給super.paintComponent
,但我懷疑你的IDE不適合並且不知道如何解決它。還有一種情況@Override
註釋是這樣的幫助,但我懷疑,因爲其他所有的問題,它真的會immeditatly幫助
開始通過定義你PaintPanel
,定義它的操作和責任......
public class PaintPanel extends JPanel {
Polygon polygon = new Polygon();
Point point = new Point(0, 0);
boolean inside = false;
public PaintPanel() {
polygon.addPoint(40, 20);
polygon.addPoint(70, 40);
polygon.addPoint(60, 80);
polygon.addPoint(45, 45);
polygon.addPoint(20, 60);
addMouseMotionListener(new MouseAdapter() {
public void mouseMoved(MouseEvent e) {
point.x = e.getX();
point.y = e.getY();
if (polygon.contains(e.getX(), e.getY())) {
inside = true;
repaint();
} else {
inside = false;
repaint();
}
}
});
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawPolygon(polygon);
if (inside) {
g.drawString(inside ? "Mouse point is in the polygon" : "Mouse point is not in the polygon", point.x, point.y);
}
}
}
現在,這增加一些容器
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new PaintPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
Runnable的例子...
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.JFrame;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Ch3PA extends JFrame {
public static void main(String[] args) {
new Ch3PA();
}
public Ch3PA() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new PaintPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class PaintPanel extends JPanel {
Polygon polygon = new Polygon();
Point point = new Point(0, 0);
boolean inside = false;
public PaintPanel() {
polygon.addPoint(40, 20);
polygon.addPoint(70, 40);
polygon.addPoint(60, 80);
polygon.addPoint(45, 45);
polygon.addPoint(20, 60);
addMouseMotionListener(new MouseAdapter() {
public void mouseMoved(MouseEvent e) {
point.x = e.getX();
point.y = e.getY();
if (polygon.contains(e.getX(), e.getY())) {
inside = true;
repaint();
} else {
inside = false;
repaint();
}
}
});
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawPolygon(polygon);
if (inside) {
g.drawString(inside ? "Mouse point is in the polygon" : "Mouse point is not in the polygon", point.x, point.y);
}
}
}
}
你的代碼沒有意義。你可以定義一個名爲'Ch3PA'的類,然後定義一個名爲'PaintPanel'的構造函數(對於這個類),它看起來像你正在試圖創建的內部類... – MadProgrammer
看起來她是複製別人的代碼,但這樣做很糟糕,沒有想到。這永遠不會成爲學習編程的一種方法。請嘗試複製在其他程序中找到的想法,但編寫自己的代碼。 –
你在哪裏添加鼠標監聽器?我將它添加到JPanel中,沒有使用未使用方法的意義。 –