I want to have multiple textfiled to be aligned in a customized order, so that they are on the top of the background image. I tried to use setBounds
but it did not work:
import javax.swing.*;
public class TestJP {
public static void main(String[] args) {
JLabel myLabel = new JLabel();
myLabel.setIcon ( new ImageIcon("system\\myBackground.jpg"));
myLabel.setBounds(0,0,750,500);
JTextField login = new JTextField(5);
login.setBounds(50,50,20,100); // This does not work
JPasswordField password = new JPasswordField(5);
password.setBounds( 50, 70, 20, 100); // Doesn't help either
JPanel myPanel = new JPanel();
myPanel.add(myLabel);
myPanel.add(login);
myPanel.add(password);
int result = JOptionPane.showConfirmDialog(null, myPanel,
"Please Login", JOptionPane.OK_CANCEL_OPTION);
// etc
}
}
Don't use setBounds()
. Swing was designed to be used with layout managers.
You can add the text fields to the label by doing something like:
JLabel myLabel = new JLabel( new ImageIcon("system\\myBackground.jpg") );
mylabel.setLayout( new FlowLayout() );
mylabel.add(login);
mylabel.add(password);
Use the appropriate layout manager for the label to get the desired layout.