java 图像面板_java – 在面板内绘制图像

在更新后,尝试将图像包装在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)会更容易.查看Performing Custom Painting了解更多详情

用例子更新

基本上,我把你的例子改为

>使用布局管理器

>在EDT的上下文中加载UI

>重新验证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 code 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}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值