最早一次成功了,就是在面板里画下“Banner",点击延时按钮以后画一条线。第二次就不成功了,百思不得其解,后来加了一个标红的句子,就又能实现了,不加就不行,这,有联系吗,奇了怪了,完全不懂。
这光有一句
frame.jlblBanner.getGraphics();
怎么就能使后边的
Graphics graphics = frame.jlblBanner.getGraphics();
graphics.drawLine(0,0,50,50);
生效呢?怎么产生的联系?有这句没这句结果就不一样了。测试了别的句子,比如写
System.out.println();
是没意义的
package chapter13;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class TestGetGraphics extends JFrame{
private JLabel jlblBanner=new JLabel("Banner");
public TestGetGraphics(){
add(jlblBanner);
System.out.println(jlblBanner.getGraphics());
}
public static void main(String[] args){
TestGetGraphics frame=new TestGetGraphics();
frame.setTitle("TestGetGraphics");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,100);
frame.setVisible(true);
//System.out.println(frame.jlblBanner.getGraphics());
//或者
//frame.jlblBanner.getGraphics();
JOptionPane.showMessageDialog(null,"Delay on purpose/nClick OK to dismiss the dialog");
Graphics graphics = frame.jlblBanner.getGraphics();
graphics.drawLine(0,0,50,50);
}
}
而且:
似乎frame.jlblBanner.getGraphics();才是获得Graphics类的关键,因为把frame.setVisible(true);放在它后边最后也不能划线了。那难道后边的Graphics graphics = frame.jlblBanner.getGraphics();
就不能获得Graphics用来划线了??
来源:CSDN
作者:秦伟H
链接:https://blog.csdn.net/huqinweI987/article/details/6191826