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

在面板内绘制图像

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

在面板内绘制图像

尝试

image
ImageIcon
更新后包装内部。同样,
Graphics#dispose
当您完成对图形上下文的渲染时,还应该调用。

    public void jButton1_ActionPerformed(ActionEvent evt) {        BufferedImage image=new BufferedImage(jPanel1.getWidth(), jPanel1.getHeight(), BufferedImage.TYPE_3BYTE_BGR);        Graphics2D g = image.createGraphics();        g.fillRect(0, 0, image.getWidth(), image.getHeight());        g.setColor(Color.BLUE);        g.drawLine(0, 0, 300, 400);        g.dispose();        JLabel l=new JLabel(new ImageIcon(image));        jPanel1.add(l);    }

您还应该依靠布局管理器,而不是自己动手做,这只会使您的生活更轻松。

就个人而言,我认为将其直接绘制到自定义组件会更容易

JPanel
。查看执行自定义绘画以获取更多详细信息

用示例更新

基本上,我将您的示例更改为

  1. 使用布局管理器
  2. 在EDT上下文中加载UI
  3. revalidate
    jPanel1
    public class BadLabel extends Jframe {        // start attributes        private JPanel jPanel1 = new JPanel(new BorderLayout(), true);        private JButton jButton1 = new JButton();        private JButton jButton2 = new JButton();        // end attributes        public BadLabel(String title) { // frame-Init super(title); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); int frameWidth = 300; int frameHeight = 300; setSize(frameWidth, frameHeight); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); int x = (d.width - getSize().width) / 2; int y = (d.height - getSize().height) / 2; setLocation(x, y);    //        setResizable(false); Container cp = getContentPane();    //        cp.setLayout(null); // start components    //        jPanel1.setBounds(48, 24, 209, 145); jPanel1.setOpaque(true); jPanel1.setBackground(Color.RED); cp.add(jPanel1); JPanel buttons = new JPanel();    //        jButton1.setBounds(88, 208, 75, 25); jButton1.setText("jButton1"); jButton1.setMargin(new Insets(2, 2, 2, 2)); jButton1.addActionListener(new ActionListener() {     public void actionPerformed(ActionEvent evt) {         jButton1_ActionPerformed(evt);     } }); buttons.add(jButton1);    //        jButton2.setBounds(184, 208, 75, 25); jButton2.setText("jButton2"); jButton2.setMargin(new Insets(2, 2, 2, 2)); jButton2.addActionListener(new ActionListener() {     public void actionPerformed(ActionEvent evt) {         jButton2_ActionPerformed(evt);     } }); buttons.add(jButton2); // end components cp.add(buttons, BorderLayout.SOUTH); setVisible(true);        } // end of public BadLabel        // start methods        public void jButton1_ActionPerformed(ActionEvent evt) { BufferedImage image = new BufferedImage(jPanel1.getWidth(), jPanel1.getHeight(), BufferedImage.TYPE_3BYTE_BGR); Graphics2D g = image.createGraphics(); g.fillRect(0, 0, image.getWidth(), image.getHeight()); g.setColor(Color.BLUE); g.drawLine(0, 0, 300, 400); g.dispose(); JLabel l = new JLabel(new ImageIcon(image)); l.setBorder(new LineBorder(Color.BLUE)); jPanel1.add(l); jPanel1.revalidate();        } // end of jButton1_ActionPerformed        public void jButton2_ActionPerformed(ActionEvent evt) { // TODO add your pre here        } // end of jButton2_ActionPerformed        // end methods        public static void main(String[] args) { EventQueue.invokeLater(new Runnable() {     @Override     public void run() {         try {  UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());         } catch (Exception exp) {         }         new BadLabel("BadLabel");     } });        } // end of main    } // end of class BadLabel}


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

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

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