首页 / JAVA / java – 为什么不绘制面板?
java – 为什么不绘制面板?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 为什么不绘制面板?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1280字,纯文字阅读大概需要2分钟。
内容图文
![java – 为什么不绘制面板?](/upload/InfoBanner/zyjiaocheng/748/77d3b6e57254456d90170c86b2c803a2.jpg)
码
import javax.swing.*;
import java.awt.*;
class tester {
public static void main(String args[]) {
JFrame fr = new JFrame();
JPanel p = new JPanel();
p.setBackground(Color.RED);
p.paintImmediately(20,20,500,500);
fr.add(p);
fr.setVisible(true);
fr.setSize(2000,2000);
}
}
我得到一个完全是红色的面板.我为什么不上线?我怎么才能得到它?
解决方法:
I get a panel painted completely of red color.
那是因为你设置背景并且没有做任何进一步的绘画……
Why dont i get the line ? How can i get it?
这不是这样做的方法.你为什么叫paintImmediately?以下是文档说的内容:
Paints the specified region in this component and all of its
descendants that overlap the region, immediately.It’s rarely necessary to call this method. In most cases it’s more
efficient to call repaint, which defers the actual painting and can
collapse redundant requests into a single paint call. This method is
useful if one needs to update the display while the current event is
being dispatched.
我建议你阅读AWT / Swing中的绘画.
> Lesson: Performing Custom Painting
得到这样的东西
你可以改变你的代码:
JFrame fr = new JFrame();
JPanel p = new JPanel() {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawLine(20, 20, 500, 500);
}
};
p.setBackground(Color.RED);
fr.add(p);
fr.setVisible(true);
fr.setSize(200, 200);
内容总结
以上是互联网集市为您收集整理的java – 为什么不绘制面板?全部内容,希望文章能够帮你解决java – 为什么不绘制面板?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。