Java 慢慢画一条线
我想慢慢地创建一条线,从10,0到10600绘制。这条线根本不会画,我认为它不起作用的唯一原因是我的格式和不同组件的放置有点不对劲?为什么?Java 慢慢画一条线,java,swing,user-interface,timer,Java,Swing,User Interface,Timer,我想慢慢地创建一条线,从10,0到10600绘制。这条线根本不会画,我认为它不起作用的唯一原因是我的格式和不同组件的放置有点不对劲?为什么? public class Moving extends JPanel { int counter; Timer time; public void setUp() { ActionListener action = new ActionListener() { public void ac
public class Moving extends JPanel {
int counter;
Timer time;
public void setUp() {
ActionListener action = new ActionListener() {
public void actionPerformed(ActionEvent event){
counter++;
repaint();
}
};
time = new Timer(100, action);
time.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.drawRect(10, counter, 20, 20);
}
public static void main(String[] args) {
Moving game = new Moving();
JFrame frame = new JFrame("Frame");
frame.setSize(320, 340);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.add(game);
game.setUp();
}
}
您尚未将移动<代码>添加到您创建的帧中 已更新 我稍微更新了代码以演示这一点,在一个帧中添加了
Moving
。我还包括了要求
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Moving extends JPanel {
int counter;
Timer time;
public Moving() {
ActionListener action = new ActionListener() {
public void actionPerformed(ActionEvent event) {
counter++;
repaint();
}
};
time = new Timer(100, action);
}
public void start() {
time.start();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.drawRect(10, counter, 20, 20);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
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) {
}
Moving moving = new Moving();
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(moving);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
moving.start();
}
});
}
}
您尚未将移动<代码>添加到您创建的帧中 已更新 我稍微更新了代码以演示这一点,在一个帧中添加了
Moving
。我还包括了要求
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Moving extends JPanel {
int counter;
Timer time;
public Moving() {
ActionListener action = new ActionListener() {
public void actionPerformed(ActionEvent event) {
counter++;
repaint();
}
};
time = new Timer(100, action);
}
public void start() {
time.start();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.drawRect(10, counter, 20, 20);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
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) {
}
Moving moving = new Moving();
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(moving);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
moving.start();
}
});
}
}
您尚未将移动<代码>添加到您创建的帧中 已更新 我稍微更新了代码以演示这一点,在一个帧中添加了
Moving
。我还包括了要求
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Moving extends JPanel {
int counter;
Timer time;
public Moving() {
ActionListener action = new ActionListener() {
public void actionPerformed(ActionEvent event) {
counter++;
repaint();
}
};
time = new Timer(100, action);
}
public void start() {
time.start();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.drawRect(10, counter, 20, 20);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
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) {
}
Moving moving = new Moving();
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(moving);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
moving.start();
}
});
}
}
您尚未将移动<代码>添加到您创建的帧中 已更新 我稍微更新了代码以演示这一点,在一个帧中添加了
Moving
。我还包括了要求
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Moving extends JPanel {
int counter;
Timer time;
public Moving() {
ActionListener action = new ActionListener() {
public void actionPerformed(ActionEvent event) {
counter++;
repaint();
}
};
time = new Timer(100, action);
}
public void start() {
time.start();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.drawRect(10, counter, 20, 20);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
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) {
}
Moving moving = new Moving();
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(moving);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
moving.start();
}
});
}
}
而且,
计数器
并不是易变的。@chrylis在这个上下文中,我不认为它需要,它只能从一个线程上下文中访问…啊,他们的我们走!volatile是什么意思?是的,谢谢!!但是你怎么才能让它不摆脱旧的矩形呢?我希望矩形每次移动时都保持不变,这样它就会在末尾创建一条线?volatile
基本上确保了某种级别的原子操作,其中一个值在写入完成之前无法读取,有关详细信息,请参阅。在这个上下文中,它是不相关的,因为您只从同一个线程上下文(EDT)读取和写入变量,计数器
不是易变的
。@chrylis在这个上下文中,我不认为它需要,它只能从单个线程上下文访问…啊,他们的我们开始!volatile是什么意思?是的,谢谢!!但是你怎么才能让它不摆脱旧的矩形呢?我希望矩形每次移动时都保持不变,这样它就会在末尾创建一条线?volatile
基本上确保了某种级别的原子操作,其中一个值在写入完成之前无法读取,有关详细信息,请参阅。在这个上下文中,它是不相关的,因为您只从同一个线程上下文(EDT)读取和写入变量,计数器
不是易变的
。@chrylis在这个上下文中,我不认为它需要,它只能从单个线程上下文访问…啊,他们的我们开始!volatile是什么意思?是的,谢谢!!但是你怎么才能让它不摆脱旧的矩形呢?我希望矩形每次移动时都保持不变,这样它就会在末尾创建一条线?volatile
基本上确保了某种级别的原子操作,其中一个值在写入完成之前无法读取,有关详细信息,请参阅。在这个上下文中,它是不相关的,因为您只从同一个线程上下文(EDT)读取和写入变量,计数器
不是易变的
。@chrylis在这个上下文中,我不认为它需要,它只能从单个线程上下文访问…啊,他们的我们开始!volatile是什么意思?是的,谢谢!!但是你怎么才能让它不摆脱旧的矩形呢?我希望矩形每次移动时都保持不变,这样它就会在末尾创建一条线?volatile
基本上确保了某种级别的原子操作,其中一个值在写入完成之前无法读取,有关详细信息,请参阅。在这种情况下,它是不相关的,因为如果要更改问题,你只能从同一线程上下文(EDT)读取和写入变量,特别是在添加了答案之后,最好附加更改,因为它会使任何现有问题变得不相关……如果你要更改问题,特别是在添加答案后,添加更改可能会更好,因为它会使任何现有问题变得无关紧要…如果您要更改问题,特别是在添加答案后,如果你想改变这个问题,特别是在添加了答案之后,你可以附加这些改变,因为这会使任何现有的问题变得无关紧要。。。