Hello fellow programmers, quick question about a simple Java program. I am trying to create a login screen for my program. I am currently using a ImageIcon, which contains an animated gif. I want to have multiple JTextFields or JTextAreas (whichever is easier) on top of this animated gif, so that I can enter info with a really nice background. Is this possible? If so how would I start?
class PaintPane extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(background.getImage(), 0, 0, null);
add(enterUsername);//enterUsername is a JTextField
add(enterPassword);//enterPassword is a JPasswordField
repaint();
revalidate();
}
}
Extend JPanel and override paintComponent() to draw the background in that JPanel. Add your JTextFields and JTextAreas (both are equally "easy") to that JPanel just like you would any other JPanel.
Recommended reading: http://docs.oracle.com/javase/tutorial/uiswing/painting/
Edit after updated code: You shouldn't put the code that adds the JTextField inside the paintComponent() method. That's going to add a new JTextField to your JPanel every time it's painted, which is a lot! Instead, treat the PaintPane as you would any other JPanel and add the components to it just once, whenever you add it to the JFrame. Something like:
public class Test{
public static void main(String... args){
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//this JPanel paints its own background
JPanel myPanel = new PaintPane();
frame.add(myPanel);
myPanel.add(new JTextField("text field");
myPanel.add(new JButton("button"));
frame.setSize(500, 500);
frame.setVisible(true);
}
}