如何在JOptionPane中的背景图像上对齐多个文本框? [英] How to align multiple textfields on a background image within a JOptionPane?

查看:104
本文介绍了如何在JOptionPane中的背景图像上对齐多个文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有多个文本文件以自定义顺序对齐,以便它们位于背景图像的顶部.我尝试使用setBounds,但没有用:

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

               }

        }

推荐答案

请勿使用setBounds(). Swing旨在与布局管理器一起使用.

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.

这篇关于如何在JOptionPane中的背景图像上对齐多个文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆