java中button与Jpanel上绘图如何关联 这是应该是因为 已经设置了setVisible(true), 早就显示了,你添加其实是加上了,只是显示不出来,应该是充写一个 重绘的方法, 类似于 paint / 调用repaint 之类的方法重绘 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 package csdn.examples;import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.EventQueue;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Shape;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.geom.AffineTransform;import java.awt.geom.Ellipse2D;import java.awt.geom.Rectangle2D;import javax.swing.Box;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.Timer;public class MotionExample { private static class PaintPanel extends JPanel { private static final long serialVersionUID = 6816695698342551101L; private Shape shape; private final Timer timer; private final AffineTransform at = AffineTransform.getTranslateInstance(1, 0); public PaintPanel(Shape shape){ this.shape = shape; timer = new Timer(100, new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { step(); } }); timer.start(); } public void step(){ shape = at.createTransformedShape(shape); repaint(); } public void setShape(Shape shape){ this.shape = shape; timer.restart(); repaint(); } @Override public void paintComponent(Graphics g){ super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.draw(shape); } } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { final JFrame frame = new JFrame(); final Ellipse2D.Double circle = new Ellipse2D.Double(0, 100, 10, 10); final Rectangle2D.Double square = new Rectangle2D.Double(0,100,10,10); final PaintPanel panel = new PaintPanel(circle); panel.setPreferredSize(new Dimension(600, 120)); frame.add(panel); final Box control = Box.createHorizontalBox(); final JButton circleb = new JButton("Circle"); final JButton squareb = new JButton("Square"); circleb.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { panel.setShape(circle); } }); squareb.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { panel.setShape(square); } }); control.add(circleb); control.add(Box.createHorizontalGlue()); control.add(squareb); frame.add(control, BorderLayout.PAGE_START); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }); }} windows 的搜索功能如何实现 字符串处理算法 关于this的用法。。一个小问题啊。。 帮帮我 请问有在深圳计算机行业协会培训JAVA的吗?? 在JBuilder中查询SQL中的表并显示出来,请问语句怎么写呀!急,谢谢! 2008奥运趣味题 求最大值堆排序算法的Java实现代码! 请问“==”和“equer()”的区别? 对话框的问题(幼稚的) 用java操作 删除了word里面的内容(已实现),然后word目录想同步更新!非常感谢给建议 java里面请问下如何将long类型的数据转换成无符号的int数据
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;public class MotionExample { private static class PaintPanel extends JPanel { private static final long serialVersionUID = 6816695698342551101L;
private Shape shape;
private final Timer timer;
private final AffineTransform at = AffineTransform.getTranslateInstance(1, 0); public PaintPanel(Shape shape){
this.shape = shape;
timer = new Timer(100, new ActionListener() { @Override
public void actionPerformed(ActionEvent arg0) {
step();
}
});
timer.start();
}
public void step(){
shape = at.createTransformedShape(shape);
repaint();
}
public void setShape(Shape shape){
this.shape = shape;
timer.restart();
repaint();
} @Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.draw(shape);
}
} public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
final JFrame frame = new JFrame();
final Ellipse2D.Double circle = new Ellipse2D.Double(0, 100, 10, 10);
final Rectangle2D.Double square = new Rectangle2D.Double(0,100,10,10);
final PaintPanel panel = new PaintPanel(circle);
panel.setPreferredSize(new Dimension(600, 120));
frame.add(panel);
final Box control = Box.createHorizontalBox();
final JButton circleb = new JButton("Circle");
final JButton squareb = new JButton("Square");
circleb.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent e) {
panel.setShape(circle);
}
});
squareb.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent e) {
panel.setShape(square); }
});
control.add(circleb);
control.add(Box.createHorizontalGlue());
control.add(squareb);
frame.add(control, BorderLayout.PAGE_START);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}