图形在标题栏中保持渲染。 我使用封装在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);
答案 0 :(得分:2)
问题是在设置框架尺寸时你没有考虑框架的边框(也可能是菜单栏)......
您应该简单地调用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);
}
});
}
}
自我调整大小(基于其内容的首选大小)
左,绝对尺寸,正确的首选尺寸
{{1}}
答案 1 :(得分:-2)
正如我之前提到的,您应该使用
设置JLabel
的位置
aJLabel.setLocation(Point p)
OR
aJLabel.setLocation(int x, int y)
如果您的图片太大,您还需要调整其大小才能获得良好的展示位置(:
最诚挚的问候。