Java 我的JPanel程序无法成功运行
运行时,该程序在位置70,70处显示一个椭圆形,并有一个开始按钮。 单击开始按钮后,程序停止一段时间,椭圆向东南移动一个位置。它实际上应该滚到另一个角落 这是节目Java 我的JPanel程序无法成功运行,java,swing,event-dispatch-thread,Java,Swing,Event Dispatch Thread,运行时,该程序在位置70,70处显示一个椭圆形,并有一个开始按钮。 单击开始按钮后,程序停止一段时间,椭圆向东南移动一个位置。它实际上应该滚到另一个角落 这是节目 package javaapplication1.pkg161; import java.awt.BorderLayout; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; impor
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);
}
}
}
PackageJavaApplication1.pkg161;
导入java.awt.BorderLayout;
导入java.awt.Graphics;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
公开课选手{
int x=70;
int y=70;
静态jf帧;
公共静态void main(字符串参数[]){
玩家p=新玩家();
p、 go();
}
公开作废go(){
f=新JFrame(“标题”);
f、 设置大小(200200);
f、 setVisible(真);
win窗口=新窗口();
f、 添加(BorderLayout.CENTER,win);
JButton b=新JButton(“开始”);
f、 添加(BorderLayout.SOUTH,b);
b、 addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
对于(int i=0;i<130;i++){
win.repaint();
试一试{
睡眠(50);
}捕捉(中断异常e1){
System.out.println(“错误”);
}
}
}
});
}
类窗口扩展了JPanel{
公共组件(图形g){
x++;
y++;
g、 椭圆形(x,y,100100);
}
}
}
Swing是一个单线程框架,这意味着您不能在事件调度线程的上下文中执行长时间运行的操作,否则您将阻止处理事件队列,并且您的程序将挂起
首先看一看,然后
你也违反了油漆连锁合同。查看和了解有关如何在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);
}
}
}
导入java.awt.BorderLayout;
导入java.awt.Dimension;
导入java.awt.Graphics;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.Timer;
公开课选手{
int x=70;
int y=70;
私人定时器;
win窗口=新窗口();
公共静态void main(字符串参数[]){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex){
例如printStackTrace();
}
玩家p=新玩家();
p、 go();
}
});
}
公开作废go(){
JFrame f=新JFrame(“标题”);
f、 添加(BorderLayout.CENTER,win);
JButton b=新JButton(“开始”);
f、 添加(BorderLayout.SOUTH,b);
f、 包装();
f、 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f、 setVisible(真);
计时器=新计时器(40,新ActionListener(){
专用整数计数器=0;
@凌驾
已执行的公共无效操作(操作事件e){
如果(计数器<130){
计数器++;
win.updateLocation();
win.repaint();
}否则{
timer.stop();
计数器=0;
}
}
});
b、 addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
如果(!timer.isRunning()){
timer.start();
}
}
});
}
类窗口扩展了JPanel{
私有整数x=70;
私人int y=70;
public void updateLocation(){
x++;
y++;
}
@凌驾
公共维度getPreferredSize(){
返回新维度(170+130、170+130);
}
@凌驾
受保护组件(图形g){
超级组件(g);
g、 椭圆形(x,y,100100);
}
}
}
首先看一看