这个方法将从JFrame获取glasspane,那个引用存储在JPanel对象中,我使用JPanel对象的getGraphics获取图形引用,然后尝试绘制glasspane的背景图像,当我运行此代码时它显示闪烁的背景图像,然后图像消失..我尝试了很多方法,比如使用getClass.getResources(path)而不是File或扩展JPanel并设置图像,因为它有相同的结果。
private void createProfile()
{
gls = (JPanel) mainUi.getGlassPane();
gls.setLayout(new GridBagLayout());
Graphics g = gls.getGraphics();
BufferedImage img=null;
try {
img = ImageIO.read(new File("/data/images/Lighthouse.jpg"));
System.out.println("Hello");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
g.drawImage(img, 0, 0, gls.getWidth()
,gls.getHeight(), null);
//gls.removeAll();
JPanel profileName = new JPanel(new BorderLayout());
JPanel profileTitlePanel = new JPanel(new BorderLayout());
profileTitlePanel.setSize(0, 20);
profileTitlePanel.setBackground(Color.black);
JLabel profileTitleLabel = new JLabel(" Create New Profile ");
profileTitleLabel.setForeground(Color.white);
profileTitleLabel.setFont(getFont(Font.BOLD,20));
profileTitlePanel.add(profileTitleLabel);
profileName.add(BorderLayout.NORTH, profileTitlePanel);
JPanel profileSelection = new JPanel(new GridLayout(5,1));
profileSelection.add(new JPanel());
JLabel nameLabel = new JLabel(" Enter Your Name : ");
Font font = getFont(Font.HANGING_BASELINE,16);
nameLabel.setFont(font);
profileSelection.add(nameLabel);
final JTextField name = new JTextField();
name.setFont(font);
profileSelection.add(name);
profileSelection.add(new JPanel());
JPanel okClear = new JPanel(new FlowLayout(FlowLayout.RIGHT));
JButton ok = new JButton("Ok");
ok.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
File file = new File("data/profiles/"+name.getText()+"/save");
try {
if(!file.exists())
{
file.mkdirs();
new File("data/profiles/"+name.getText()+"/saves.dat").createNewFile();
noProfile=false;
System.out.println("The Current Profile -> "+noProfile);
// mainWindow();
currentProfile = name.getText();
new DataOutputStream(new FileOutputStream("data/state.dat")).writeBoolean(noProfile);
new DataOutputStream(new FileOutputStream("data/current_profile.dat")).writeUTF(currentProfile);
//new DataOutputStream(new FileOutputStream(file)).writeInt(1);
gls.setVisible(false);
mainUi.repaint();
mainWindow();
}
else
{
createProfile();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
okClear.add(ok);
JButton clear = new JButton("Clear");
clear.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
name.setText("");
}
});
okClear.add(clear);
profileSelection.add(okClear);
profileName.add(profileSelection);
GridBagConstraints gbc = new GridBagConstraints();
gbc.ipadx= 10;
gbc.ipady = 10;
profileName.setBorder(BorderFactory.createEtchedBorder());
profileName.setSize(400, 250);
gls.add(profileName,gbc);
gls.setVisible(true);
}
答案 0 :(得分:2)
当没有从swing的重绘机制中调用时,绘制成组件的图形对象不起作用。
您应该创建自己的JPanel派生类。在其上实现paintComponent(Graphics g)方法,并使用传入的图形对象在那里进行绘制。
然后取出JPanel,并调用frame.setGlassPane(thatpanel);
答案 1 :(得分:2)
不要使用getGraphics
,这不是自定义绘画在Swing中完成的方式。
请查看Performing Custom Painting了解详情。
你应该创建自己的组件,可能使用JPanel
并覆盖它的paintComponent
方法来执行自定义绘画。
不要忘记使用setOpaque(false)
使组件透明,并使用setGlassPane(...)
有关详细信息,请参阅How to use root panes