[英]How to use two paint methods in java? Or outside the basic paint method
我怎样才能创建两种绘画方法? 当我试图使用两种涂料方法时,它们永远不会起作用。 如果不能,我想在基本的油漆方法之外绘画,我不知道如何。 例如:
public class test extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
test frame = new test();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public void paint(Graphics g) {
g.fillRect(100, 100, 100, 100);
}
public void pp(Graphics g) {
g.fillRect(250, 100, 100, 100);
}
/**
* Create the frame.
*/
public test() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
}
}
当我试图使用两种涂料方法时,它们永远不会起作用。
paintComponent(...)
不是JFrame的方法。 每当您尝试覆盖一个方法时,您应该使用@Override
注释,编译器会在您尝试覆盖不存在的方法时告诉您。
通常,对于其他Swing组件, paint(...)
方法负责调用paintComponent(...)
方法,因此不应覆盖paint()
方法。 请参阅:详细了解Paint Mechanism了解更多信息。
无论如何,你不应该覆盖JFrame上的paint()。 阅读教程链接中有关Performing Custom Painting
的整个部分,以获取应如何进行自定义绘制的工作示例。
我找到了一个方法。
public void paint(Graphics g) { super.paint(g); draw(g); draw2(g); }
public void draw(Graphics g){
g.fillRect(100, 100, 100, 100);
}
public void draw2(Graphics g){
g.setColor(Color.blue);
g.fillRect(200, 100, 100, 100);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.