栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java中的paintComponent()未被调用

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java中的paintComponent()未被调用

虽然已经提供的答案可能会导致出现矩形,但这种方法并非最佳。此示例旨在显示一种更好的方法。阅读代码中的注释以获取详细信息。

请注意,应该在EDT上启动Swing / AWT GUI。这留给读者练习。

import java.awt.*;import javax.swing.*;public class Painting {    public static void main(String[] args) {        Jframe jf = new Jframe("JUST DRAW A RECTANGLE");        jf.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);        // null layouts cause more problems than they solve. DO NOT USE!        //jf.setLayout(null);        jf.setLocationRelativeTo(null);                //jf.setSize(600, 600);        //jf.setVisible(true); //  as mentioned, this should be last        Mainting maint = new Mainting();        jf.add(maint);        jf.pack(); // makes the GUI the size it NEEDS to be        jf.setVisible(true);    }}class Mainting extends JPanel {    @Override    public void paintComponent(Graphics g) {        super.paintComponent(g);        g.setColor(Color.RED);         g.drawRect(10, 10, 200, 200);        System.out.println("paintComponent called");                //g.setColor(Color.red);     }    @Override    public Dimension getPreferredSize() {        // Provide hints to the layout manager!         return new Dimension(220, 220);    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/428800.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号