Does textfield can overlap the label at JDialog form?
i'm having trouble with my gui... want to have a background image using "label" and then there's an overlapping "text field"... i use "label" (at swing controls) so i can import an image.. and then thinking to overlap a text field.. but that doesn't work, coz every time i drag the text field.. my layout got ruined... can you help me or suggest better solution to my problem.
i just want to show an instruction with a background 开发者_Python百科image in my gui..
thnx in advance :)
Have you tried using a JLayeredPane
? From the Swing tutorial, it exactly looks like the required component in your case.
You can add images on a JLabel. If you want to add other components on top of the image you have to add the image to a JPanel. This way you will have layout control. There is a number of steps involved for this solution:
Create a class that extends JPanel and includes a reader and and a paintComponent
public class ImageJPanel extends JPanel { public ImageJPanel() { try { myImage = ImageIO.read(new File("singer.jpg")); } catch (IOException ex) { System.out.println("No image! " + ex.getMessage()); } } @Override public void paintComponent(Graphics g) { g.drawImage(myImage, 0, 0, null); } private BufferedImage myImage;}
Set the layout of your JDialog to BorderLayout.
Add another JPanel p, make it non opaque in order to see the image,
p.setOpaque(false);
with the JTextFields, JLabels etc. to your contentpane.Add an ImageJPanel instance to the center of your Border Layout.
Here is a sample constructor for a Tester class that extends JDialog:
public Tester() {
setLayout(new BorderLayout());
JPanel myImagePanel = new ImageJPanel();
add(myImagePanel);
JPanel workPanel = new JPanel();
workPanel.setOpaque(false);
workPanel.add(new JLabel("a label"));
workPanel.add(new JTextField(10));
myImagePanel.add(workPanel);
pack();
}
精彩评论