java中怎样设置一个移动的面板?
以下是课本中的例题,设置了一条移动的信息,那么,怎么把message换成panel?我想要让很多东西一起移动,所以需要让整个面板移动。importjava.awt.Gra...
以下是课本中的例题,设置了一条移动的信息,那么,怎么把message换成panel?我想要让很多东西一起移动,所以需要让整个面板移动。
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;
public class AnimationDemo extends JFrame {
public AnimationDemo() {
add(new MovingMessagePanel("message moving?"));
}
public static void main(String[] args) {
AnimationDemo frame = new AnimationDemo();
frame.setTitle("AnimationDemo");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(280, 100);
frame.setVisible(true);
}
static class MovingMessagePanel extends JPanel {
private String message = "Welcome to Java";
private int xCoordinate = 0;
private int yCoordinate = 20;
public MovingMessagePanel(String massage) {
this.message = message;
Timer timer = new Timer(50, new TimerListener());
timer.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (xCoordinate > getWidth()) {
xCoordinate = -20;
}
xCoordinate +=5;
g.drawString(message, xCoordinate, yCoordinate);
}
class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
repaint();
}
}
}
} 展开
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;
public class AnimationDemo extends JFrame {
public AnimationDemo() {
add(new MovingMessagePanel("message moving?"));
}
public static void main(String[] args) {
AnimationDemo frame = new AnimationDemo();
frame.setTitle("AnimationDemo");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(280, 100);
frame.setVisible(true);
}
static class MovingMessagePanel extends JPanel {
private String message = "Welcome to Java";
private int xCoordinate = 0;
private int yCoordinate = 20;
public MovingMessagePanel(String massage) {
this.message = message;
Timer timer = new Timer(50, new TimerListener());
timer.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (xCoordinate > getWidth()) {
xCoordinate = -20;
}
xCoordinate +=5;
g.drawString(message, xCoordinate, yCoordinate);
}
class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
repaint();
}
}
}
} 展开
展开全部
组件在面板中移动还是面板在显示器上移动呢??
不管怎样都可以用setLocaltion()方法设置组建的坐标
如果是重写组建的paint()方法可以使用Graphics 中相关的绘图方法
不管怎样都可以用setLocaltion()方法设置组建的坐标
如果是重写组建的paint()方法可以使用Graphics 中相关的绘图方法
更多追问追答
追问
我想要在JFrame中,创建一个JPanel,然后点击某个JButton,可以使这个JPanel移出JFrame,就像一叠纸拿掉最上面第一张一样,当然如果你能教我做成翻页的效果就更好了……
追答
使JPanel移出JFrame,可以用remove()方法删除此组件
也可以用setVisible()将其隐藏掉的
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询