设置方法管理JOptionPane的布局 [英] how to set & manage the layout of JOptionPane

查看:93
本文介绍了设置方法管理JOptionPane的布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的JOptionPane,其中包含一些组件,我想对其进行排列.

I have a simple JOptionPane with some components and I want to arrange them.

例如,我想将标签放在文本/密码字段旁边,设置其宽度等.

For example I want to put the label next to the text/password field, sets their width etc..

这就像我做的那样,结果当然不是我想要的:

This is like how I did it, of course the result wasn't what I wanted:

public class CreateApplicationUserScreen {

  CreateApplicationUserScreen(){
    JLabel l_name=new JLabel("Username");
    JLabel l_pass=new JLabel("Password");
    JTextField tf_name=new JTextField(10);
    JPasswordField pf_pass=new JPasswordField(10);
    JButton b_privs = new JButton("Privs");
    JButton b_databases = new JButton("Databases");

    int res = JOptionPane.showOptionDialog(window, 
                                          new Object[] { l_name, tf_name, 
                                                         l_pass, pf_pass, 
                                                         b_privs, b_databases }, 
                                          "Create application user", 
                                          JOptionPane.OK_CANCEL_OPTION,
                                          JOptionPane.PLAIN_MESSAGE,null, 
                                          new String[]{"Create", "Cancel"},
                                          "default");
  }
}

推荐答案

您可以将任何Swing组件添加到选项窗格中.因此,建立您自己的面板:

You can add any Swing component to the option pane. So build your own panel:

import java.awt.*;
import javax.swing.*;

public class OptionPanePanel
{
    private static void createAndShowUI()
    {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );

        //  Build a custom panel

        JPanel panel = new JPanel( new GridLayout(2, 2) );
        panel.add( new JLabel("First Name") );
        JTextField firstName = new JTextField(10);
//      firstName.addAncestorListener( new RequestFocusListener(false) );
        panel.add( firstName );
        panel.add( new JLabel("Last Name") );
        JTextField lastName = new JTextField(10);
        panel.add( lastName );

        int result = JOptionPane.showConfirmDialog(
            frame, // use your JFrame here
            panel,
            "Use a Panel",
            JOptionPane.YES_NO_OPTION,
            JOptionPane.PLAIN_MESSAGE);

        if(result == JOptionPane.YES_OPTION)
        {
            System.out.println(firstName.getText() + " : " + lastName.getText());
        }
        else
        {
            System.out.println("Canceled");
        }

        //  Let Option Pane build the panel for you

        Object[] msg = {"First Name:", firstName, "Last Name:", lastName};

        result = JOptionPane.showConfirmDialog(
            frame,
            msg,
            "Use default layout",
            JOptionPane.OK_CANCEL_OPTION,
            JOptionPane.PLAIN_MESSAGE);

        if (result == JOptionPane.YES_OPTION)
        {
            System.out.println(firstName.getText() + " : " + lastName.getText());
        }
        else
        {
            System.out.println("Canceled");
        }
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

此方法的一个缺点是焦点将集中在按钮上,而不是文本字段上.因此,您可能需要使用 RequestFocusListener 将焦点放在第一个文本字段.

One drawback to this approach is that the focus will be on the button, not the text field. So you may want to use the RequestFocusListener to place focus on your first text field.

这篇关于设置方法管理JOptionPane的布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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