首页 / JAVA / java – 标题栏中的图形渲染
java – 标题栏中的图形渲染
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 标题栏中的图形渲染,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2083字,纯文字阅读大概需要3分钟。
内容图文
图形在标题栏中保持渲染.
我使用封装在jlabel中的缓冲图像,并使用生成的图形对象在我的代码中绘制矩形.这是jframe类构造函数的重要部分:
super();
BufferedImage image=new BufferedImage(680,581,BufferedImage.TYPE_INT_ARGB);
m_graphicsObject =image.getGraphics();
JLabel label=new JLabel(new ImageIcon(image));
// buttons, mouse events and other controls use listeners to handle actions
// these listener are classes
btn1 = new JButton("Go!");
//btn1.setPreferredSize(new Dimension(100, 30));
btn1.addActionListener(new button_go_Click()); //listener 1
btn2 = new JButton("Clear!");
//btn2.setPreferredSize(new Dimension(100, 30));
btn2.addActionListener(new button_clear_Click()); //listener 2
//always add created buttons/controls to form
JPanel panel=new JPanel(new GridLayout(20,2));
panel.add(btn1);
panel.add(btn2);
Container pane = this.getContentPane();
pane.add(label);
pane.add(panel, BorderLayout.EAST);
this.setSize(680,581);
this.setVisible(true);
解决方法:
问题是你在设置框架的大小时没有考虑框架的边框(也可能是菜单栏)……
而不是使用this.setSize(680,581),它将导致图像在框架边框内呈现(并超出非可见空间),您应该简单地调用JFrame#pack并让框架决定如何最好地自我调整大小(根据其内容的首选大小)
左,绝对尺寸,正确的首选尺寸
public class SimpleImageLabel {
public static void main(String[] args) {
new SimpleImageLabel();
}
public SimpleImageLabel() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JLabel imageLabel = new JLabel();
try {
imageLabel.setIcon(new ImageIcon(ImageIO.read(new File("/path/to/image"))));
} catch (Exception e) {
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(imageLabel);
frame.pack(); // <-- The better way
// frame.setSize(imageLabel.getPreferredSize()); // <-- The not better way
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
内容总结
以上是互联网集市为您收集整理的java – 标题栏中的图形渲染全部内容,希望文章能够帮你解决java – 标题栏中的图形渲染所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。