我的 JPanel 程序无法成功运行
My JPanel program not working successfully
当运行在位置70,70处显示一个椭圆并且有一个开始按钮时,这个程序。
单击开始按钮后,程序停止了一段时间,椭圆向东南移动了一个位置。其实应该是往另一个角落滚下去的
这是程序....
package javaapplication1.pkg161;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Player {
int x = 70;
int y = 70;
static JFrame f;
public static void main(String args[]) {
Player p = new Player();
p.go();
}
public void go() {
f = new JFrame("title");
f.setSize(200, 200);
f.setVisible(true);
Window win = new Window();
f.add(BorderLayout.CENTER, win);
JButton b = new JButton("Start");
f.add(BorderLayout.SOUTH, b);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < 130; i++) {
win.repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e1) {
System.out.println("error");
}
}
}
});
}
class Window extends JPanel {
public void paintComponent(Graphics g) {
x++;
y++;
g.fillOval(x, y, 100, 100);
}
}
}
Swing 是一个单线程框架,这意味着您不能在事件调度线程的上下文中执行长时间的 运行 操作,否则您将无法处理事件队列并且您的程序将挂起。
先看看 Concurrency in Swing and How to use Swing Timers
你也违反了油漆链合同。查看 Painting in AWT and Swing and Performing Custom Painting 了解有关如何在 Swing 中完成绘制的更多详细信息
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Player {
int x = 70;
int y = 70;
private Timer timer;
Window win = new Window();
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
Player p = new Player();
p.go();
}
});
}
public void go() {
JFrame f = new JFrame("title");
f.add(BorderLayout.CENTER, win);
JButton b = new JButton("Start");
f.add(BorderLayout.SOUTH, b);
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
timer = new Timer(40, new ActionListener() {
private int counter = 0;
@Override
public void actionPerformed(ActionEvent e) {
if (counter < 130) {
counter++;
win.updateLocation();
win.repaint();
} else {
timer.stop();
counter = 0;
}
}
});
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!timer.isRunning()) {
timer.start();
}
}
});
}
class Window extends JPanel {
private int x = 70;
private int y = 70;
public void updateLocation() {
x++;
y++;
}
@Override
public Dimension getPreferredSize() {
return new Dimension(170+130, 170+130);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillOval(x, y, 100, 100);
}
}
}
当运行在位置70,70处显示一个椭圆并且有一个开始按钮时,这个程序。 单击开始按钮后,程序停止了一段时间,椭圆向东南移动了一个位置。其实应该是往另一个角落滚下去的
这是程序....
package javaapplication1.pkg161;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Player {
int x = 70;
int y = 70;
static JFrame f;
public static void main(String args[]) {
Player p = new Player();
p.go();
}
public void go() {
f = new JFrame("title");
f.setSize(200, 200);
f.setVisible(true);
Window win = new Window();
f.add(BorderLayout.CENTER, win);
JButton b = new JButton("Start");
f.add(BorderLayout.SOUTH, b);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < 130; i++) {
win.repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e1) {
System.out.println("error");
}
}
}
});
}
class Window extends JPanel {
public void paintComponent(Graphics g) {
x++;
y++;
g.fillOval(x, y, 100, 100);
}
}
}
Swing 是一个单线程框架,这意味着您不能在事件调度线程的上下文中执行长时间的 运行 操作,否则您将无法处理事件队列并且您的程序将挂起。
先看看 Concurrency in Swing and How to use Swing Timers
你也违反了油漆链合同。查看 Painting in AWT and Swing and Performing Custom Painting 了解有关如何在 Swing 中完成绘制的更多详细信息
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Player {
int x = 70;
int y = 70;
private Timer timer;
Window win = new Window();
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
Player p = new Player();
p.go();
}
});
}
public void go() {
JFrame f = new JFrame("title");
f.add(BorderLayout.CENTER, win);
JButton b = new JButton("Start");
f.add(BorderLayout.SOUTH, b);
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
timer = new Timer(40, new ActionListener() {
private int counter = 0;
@Override
public void actionPerformed(ActionEvent e) {
if (counter < 130) {
counter++;
win.updateLocation();
win.repaint();
} else {
timer.stop();
counter = 0;
}
}
});
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!timer.isRunning()) {
timer.start();
}
}
});
}
class Window extends JPanel {
private int x = 70;
private int y = 70;
public void updateLocation() {
x++;
y++;
}
@Override
public Dimension getPreferredSize() {
return new Dimension(170+130, 170+130);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillOval(x, y, 100, 100);
}
}
}