0

这里我的面板上的按钮只有当我将鼠标放在它们上时才可见,密码按钮和 JTextfield 甚至不可见。请提出一些想法来解决这个问题。我有三个按钮和标签和文本字段。

public class FileOperation extends JFrame
{
    private Player player;
    private File file;
    private class BackgroundPanel extends Panel  
    {  
    Image img;  
    public BackgroundPanel()  
    {  
    try  
    {  
    img = Toolkit.getDefaultToolkit().createImage("17.jpg");  
    }  
    catch(Exception e){/*handled in paint()*/}  
    }  
@Override 
public void paint(Graphics g)  
{  
super.paint(g);  
if(img != null) g.drawImage(img, 0,0,this.getWidth(),this.getHeight(),this); 
else g.drawString("No Image",100,100);  
}

}
 public FileOperation()
 {
     super( "File Protection Tool" );
     BackgroundPanel panel = new BackgroundPanel();
     panel.setLayout (null);
     JLabel label5=new JLabel("SecretPassword");    
     final JTextField hashkey = new JTextField(15);
     JButton openFilee = new JButton( "Open file to encrypt" );
     openFilee.addActionListener( new ActionListener() 
     {
         public void actionPerformed( ActionEvent e )
         {
             openFile(0,hashkey);
         }
     });
    JButton openFiled = new JButton("Open file to decrypt");
    openFiled.addActionListener( new ActionListener()
    {
        public void actionPerformed( ActionEvent d )
        {
            openFile(1,hashkey);
        }
    });
    JButton openFilefol = new JButton("Choose folder to lock");
    openFilefol.addActionListener( new ActionListener()
    {
        public void actionPerformed( ActionEvent d )
        {
            openFilefol();
        }
    });
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;
setSize(screenWidth / 2, screenHeight / 2);
setLocation(screenWidth / 4, screenHeight / 4);
setTitle("FileEncrypt");
hashkey.setBounds(200,210,150,20);
    label5.setBounds(100,210,150,20);
openFilee.setBounds(50,120,190,20);
openFiled.setBounds(300,120,190,20);
openFilefol.setBounds(80,240,190,20);
panel.add(openFilee);
panel.add(openFiled);
panel.add(openFilefol);
panel.add(label5);
panel.add(hashkey);
panel.setVisible(true);
getContentPane().add(panel);
 }
4

1 回答 1

1

您需要使用 JPanel 而不是 Panel 并覆盖paintComponent

private class BackgroundPanel extends JPanel  
    {  
    Image img;  
    public BackgroundPanel()  
    {  
    try  
    {  
    img = Toolkit.getDefaultToolkit().createImage("17.jpg");  
    }  
    catch(Exception e){/*handled in paint()*/}  
    }  
@Override 
public void paintComponent(Graphics g)  
{  
super.paintComponent(g);  
if(img != null) g.drawImage(img, 0,0,this.getWidth(),this.getHeight(),this); 
else g.drawString("No Image",100,100);  
}

}
于 2013-10-09T15:13:58.777 回答