在卡布局内定位组件 [英] Positioning component inside card layout

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

问题描述

我有一个主框架,我希望使用卡片布局在中心位置显示我的NewUser类的对象。这是我的主要课程

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



公共类CardLayoutDemo实现ItemListener {
JPanel卡; //一个使用CardLayout的面板
final static String BUTTONPANEL =Card with JButtons;
final static String TEXTPANEL =带JTextField的卡;

public void addComponentToPane(容器窗格){
//将JComboBox放在JPanel中以获得更好的外观。
JPanel comboBoxPane = new JPanel(); //使用FlowLayout
String comboBoxItems [] = {BUTTONPANEL,TEXTPANEL};
JComboBox cb = new JComboBox(comboBoxItems);
cb.setEditable(false);
cb.addItemListener(this);
comboBoxPane.add(cb);

//创建卡片。
NewUser newUser = new NewUser();

JPanel card1 = new JPanel();
card1.add(新JButton(按钮1));
card1.add(新JButton(按钮2));
card1.add(新JButton(按钮3));

JPanel card2 = new NewUser();


//创建包含卡片的面板。
cards = new JPanel(new CardLayout());
cards.add(card1,BUTTONPANEL);
cards.add(card2,TEXTPANEL);

pane.add(comboBoxPane,BorderLayout.PAGE_START);
pane.add(cards,BorderLayout.CENTER);
}

public void itemStateChanged(ItemEvent evt){
CardLayout cl =(CardLayout)(cards.getLayout());
cl.show(cards,(String)evt.getItem());
}

/ **
*创建GUI并显示它。对于线程安全,
*应该从
*事件派发线程调用此方法。
* /
private static void createAndShowGUI(){
//创建并设置窗口。
JFrame frame = new JFrame(CardLayoutDemo);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//创建并设置内容窗格。
CardLayoutDemo demo = new CardLayoutDemo();
demo.addComponentToPane(frame.getContentPane());

//显示窗口。
frame.pack();
frame.setVisible(true);
}

public static void main(String [] args){
/ *使用适当的外观* /
试试{
// UIManager.setLookAndFeel( com.sun.java.swing.plaf.windows.WindowsLookAndFeel);
UIManager.setLookAndFeel(javax.swing.plaf.metal.MetalLookAndFeel);
} catch(UnsupportedLookAndFeelException ex){
ex.printStackTrace();
} catch(IllegalAccessException ex){
ex.printStackTrace();
} catch(InstantiationException ex){
ex.printStackTrace();
} catch(ClassNotFoundException ex){
ex.printStackTrace();
}
/ *关闭金属使用粗体字体* /
UIManager.put(swing.boldMetal,Boolean.FALSE);

//为事件调度线程安排作业:
//创建并显示此应用程序的GUI。
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run(){
createAndShowGUI();
}
});
}
}

这是我的NewUser类

  import java.awt.Dimension; 
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

公共类NewUser扩展JPanel {

private static final long serialVersionUID = 1L;
private JLabel lblUsername,lblPassword,lblConfirmMsg;
private JPasswordField txtPassword,txtCPassword;
private JTextField txtUsername,txtName;
private JButton btnSave,btnCancel;
JPanel panelNewUser;

Dimension screen = Toolkit.getDefaultToolkit()。getScreenSize();

public NewUser(){

this.setSize(350,270);
this.setLocation((screen.width - 500)/ 2,((screen.height - 350)/ 2));
this.setLayout(null);
panelNewUser = this;
lblUsername = new JLabel(用户名);
lblPassword = new JLabel(密码);
lblConfirmMsg = new JLabel(重新输入密码);
txtName = new JTextField();

txtUsername = new JTextField();
txtPassword = new JPasswordField();
txtCPassword = new JPasswordField();
btnSave =新JButton(保存);
btnCancel = new JButton(取消);

lblUsername.setBounds(30,30,100,25);
this.add(lblUsername);
txtUsername.setBounds(150,30,150,25);
this.add(txtUsername);
lblPassword.setBounds(30,70,100,25);
this.add(lblPassword);
txtPassword.setBounds(150,70,150,25);
this.add(txtPassword);
lblConfirmMsg.setBounds(30,110,110,25);
this.add(lblConfirmMsg);
txtCPassword.setBounds(150,110,150,25);
this.add(txtCPassword);
btnSave.setBounds(60,155,100,25);
this.add(btnSave);
btnCancel.setBounds(180,155,100,25);
this.add(btnCancel);

txtName.addKeyListener(new KeyAdapter(){

public void keyTyped(KeyEvent e){
char c = e.getKeyChar();
if(!(Character.isLetter(c)||(c == KeyEvent.VK_BACK_SPACE)
||(c == KeyEvent.VK_SPACE)||(c == KeyEvent.VK_DELETE))){
getToolkit()。beep();
JOptionPane.showMessageDialog(null,Invalid Character,
ERROR,JOptionPane.ERROR_MESSAGE);
e.consume();
}
}
});
txtUsername.addKeyListener(new KeyAdapter(){

public void keyTyped(KeyEvent e){
char c = e.getKeyChar();
if(!( Character.isLetter(c)||(c == KeyEvent.VK_BACK_SPACE)
||(Character.isDigit(c))||(c == KeyEvent.VK_DELETE))){
getToolkit() .beep();
JOptionPane.showMessageDialog(null,无效字符,
ERROR,JOptionPane.ERROR_MESSAGE);
e.consume();
}
}
});
btnCancel.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
resetField();
panelNewUser.setVisible(false);
System.out.println(hello);
}
});
btnSave.addActionListener(new java.awt.event.ActionListener(){


public void actionPerformed(ActionEvent e){

// DatabaseHelper databaseHelper = new DatabaseHelper();
if(txtUsername.getText()== null
|| txtUsername.getText()。equals()){
JOptionPane.showMessageDialog(null, 输入用户名,
缺少字段,JOptionPane.DEFAULT_OPTION);
txtUsername.requestFocus();
return;
}
if(txtPassword.getPassword( )== null
|| txtPassword.getPassword()。equals()){
JOptionPane.showMessageDialog(null,输入密码,
缺少字段,JOptionPane.DEFAULT_OPTION );
txtPassword.requestFocus();
return;
}
if(txtCPassword.getPasswor) d()== null
|| txtCPassword.getPassword()。equals()){
JOptionPane.showMessageDialog(null,
确认密码,缺少字段,
JOptionPane.DEFAULT_OPTION);
txtCPassword.requestFocus();
返回;
}
if(!txtPassword.getText()
.equals(txtPassword.getText())){
JOptionPane.showMessageDialog(null,
密码不匹配。,错误,
JOptionPane.DEFAULT_OPTION);
txtCPassword.requestFocus();
返回;
}


if(true){
JOptionPane.showMessageDialog(null,
创建新用户,SUCCESS,
JOptionPane.DEFAULT_OPTION);
resetField();
}


}
});

} //构造函数关闭

//动作监听器


public void resetField()
{

txtUsername.setText();
txtCPassword.setText();
txtPassword.setText();
}
} //类关闭

我想要NewUser的对象来显示在主类的中心位置

解决方案

欢迎来到图形用户界面设计的世界,其中字体的大小,屏幕分辨率和你开发出令人敬畏的用户界面的DPI,永远不会与最终用户相匹配......



现在,你有两个选择。花费你的余生试图遇到每一个奇怪的边缘情况,在重新发明轮子的过程中,一个轮子已经为你提供了,以便您可以正确考虑字体大小的差异系统之间......


I have a main frame where i want to display object of my NewUser class in center position using card layout.Here is my main class

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



public class CardLayoutDemo implements ItemListener {
JPanel cards; //a panel that uses CardLayout
final static String BUTTONPANEL = "Card with JButtons";
final static String TEXTPANEL = "Card with JTextField";

public void addComponentToPane(Container pane) {
    //Put the JComboBox in a JPanel to get a nicer look.
    JPanel comboBoxPane = new JPanel(); //use FlowLayout
    String comboBoxItems[] = { BUTTONPANEL, TEXTPANEL };
    JComboBox cb = new JComboBox(comboBoxItems);
    cb.setEditable(false);
    cb.addItemListener(this);
    comboBoxPane.add(cb);

    //Create the "cards".
    NewUser newUser = new NewUser();

    JPanel card1 = new JPanel();
    card1.add(new JButton("Button 1"));
    card1.add(new JButton("Button 2"));
    card1.add(new JButton("Button 3"));

    JPanel card2 = new NewUser();


    //Create the panel that contains the "cards".
    cards = new JPanel(new CardLayout());
    cards.add(card1, BUTTONPANEL);
    cards.add(card2, TEXTPANEL);

    pane.add(comboBoxPane, BorderLayout.PAGE_START);
    pane.add(cards, BorderLayout.CENTER);
}

public void itemStateChanged(ItemEvent evt) {
    CardLayout cl = (CardLayout)(cards.getLayout());
    cl.show(cards, (String)evt.getItem());
}

/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event dispatch thread.
 */
private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("CardLayoutDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create and set up the content pane.
    CardLayoutDemo demo = new CardLayoutDemo();
    demo.addComponentToPane(frame.getContentPane());

    //Display the window.
    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {
    /* Use an appropriate Look and Feel */
    try {
        //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
    } catch (UnsupportedLookAndFeelException ex) {
        ex.printStackTrace();
    } catch (IllegalAccessException ex) {
        ex.printStackTrace();
    } catch (InstantiationException ex) {
        ex.printStackTrace();
    } catch (ClassNotFoundException ex) {
        ex.printStackTrace();
    }
    /* Turn off metal's use of bold fonts */
    UIManager.put("swing.boldMetal", Boolean.FALSE);

    //Schedule a job for the event dispatch thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}
}

Here is my NewUser class

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class NewUser extends JPanel {

private static final long serialVersionUID = 1L;
private JLabel lblUsername, lblPassword, lblConfirmMsg;
private JPasswordField txtPassword, txtCPassword;
private JTextField txtUsername, txtName;
private JButton btnSave, btnCancel;
JPanel panelNewUser;

Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

public NewUser() {

    this.setSize(350, 270);
    this.setLocation((screen.width - 500) / 2, ((screen.height - 350) / 2));
    this.setLayout(null);
    panelNewUser= this;
    lblUsername = new JLabel("Username");
    lblPassword = new JLabel("Password");
    lblConfirmMsg = new JLabel("Re-enter Password");
    txtName = new JTextField();

    txtUsername = new JTextField();
    txtPassword = new JPasswordField();
    txtCPassword = new JPasswordField();
    btnSave = new JButton("Save");
    btnCancel = new JButton("Cancel");

    lblUsername.setBounds(30, 30, 100, 25);
    this.add(lblUsername);
    txtUsername.setBounds(150, 30, 150, 25);
    this.add(txtUsername);
    lblPassword.setBounds(30, 70, 100, 25);
    this.add(lblPassword);
    txtPassword.setBounds(150, 70, 150, 25);
    this.add(txtPassword);
    lblConfirmMsg.setBounds(30, 110, 110, 25);
    this.add(lblConfirmMsg);
    txtCPassword.setBounds(150, 110, 150, 25);
    this.add(txtCPassword);
    btnSave.setBounds(60, 155, 100, 25);
    this.add(btnSave);
    btnCancel.setBounds(180, 155, 100, 25);
    this.add(btnCancel);

    txtName.addKeyListener(new KeyAdapter() {

        public void keyTyped(KeyEvent e) {
            char c = e.getKeyChar();
            if (!(Character.isLetter(c) || (c == KeyEvent.VK_BACK_SPACE)
                    || (c == KeyEvent.VK_SPACE) || (c == KeyEvent.VK_DELETE))) {
                getToolkit().beep();
                JOptionPane.showMessageDialog(null, "Invalid Character",
                        "ERROR", JOptionPane.ERROR_MESSAGE);
                e.consume();
            }
        }
    });
    txtUsername.addKeyListener(new KeyAdapter() {

        public void keyTyped(KeyEvent e) {
            char c = e.getKeyChar();
            if (!(Character.isLetter(c) || (c == KeyEvent.VK_BACK_SPACE)
                    || (Character.isDigit(c)) || (c == KeyEvent.VK_DELETE))) {
                getToolkit().beep();
                JOptionPane.showMessageDialog(null, "Invalid Character",
                        "ERROR", JOptionPane.ERROR_MESSAGE);
                e.consume();
            }
        }
    });
    btnCancel.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(ActionEvent e) {
            resetField();
            panelNewUser.setVisible(false);
        System.out.println("hello");
        }
    });
    btnSave.addActionListener(new java.awt.event.ActionListener() {


        public void actionPerformed(ActionEvent e) {

            //DatabaseHelper databaseHelper = new DatabaseHelper();
            if (txtUsername.getText() == null
                    || txtUsername.getText().equals("")) {
                JOptionPane.showMessageDialog(null, "Enter Username",
                        "Missing fields", JOptionPane.DEFAULT_OPTION);
                txtUsername.requestFocus();
                return;
            }
            if (txtPassword.getPassword() == null
                    || txtPassword.getPassword().equals("")) {
                JOptionPane.showMessageDialog(null, "Enter Password",
                        "Missing fields", JOptionPane.DEFAULT_OPTION);
                txtPassword.requestFocus();
                return;
            }
            if (txtCPassword.getPassword() == null
                    || txtCPassword.getPassword().equals("")) {
                JOptionPane.showMessageDialog(null,
                        "Confirm your password", "Missing fields",
                        JOptionPane.DEFAULT_OPTION);
                txtCPassword.requestFocus();
                return;
            }
            if (!txtPassword.getText()
                    .equals(txtPassword.getText())) {
                JOptionPane.showMessageDialog(null,
                        "Passwords do not match.", "ERROR",
                        JOptionPane.DEFAULT_OPTION);
                txtCPassword.requestFocus();
                return;
            }


                if (true) {
                    JOptionPane.showMessageDialog(null,
                            "A new user is created", "SUCCESS",
                            JOptionPane.DEFAULT_OPTION);
                    resetField();
                }


        }
    });

    }// constructor closed

//action listener


public void resetField()
{

    txtUsername.setText("");
    txtCPassword.setText("");
    txtPassword.setText("");
}
}// class closed

I want the object of NewUser to be displayed at center position in main class

解决方案

Welcome to the wounderful world of Graphical User Interface Design, where the size of the font, the screen resolution and the DPI you develop your mind blowingly awesome UI on, won't ever match that of the end user...

Now, you have two choices. Spend the rest of your life trying to meet every weird edge case that comes up, in the process re-inventing the wheel, a wheel that has been made available for you in the guise of the layout managers (sorry if sound fictitious, but most people's problems in Swing could be solved by understanding layout managers :P).

You might like to have a read through Using Layout Managers and A Visual Guide to Layout Managers

public class TestLayout02 {

    public static void main(String[] args) {
        new TestLayout02();
    }

    public TestLayout02() {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                /* Use an appropriate Look and Feel */
                try {
                    //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                    UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
                } catch (UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                } catch (IllegalAccessException ex) {
                    ex.printStackTrace();
                } catch (InstantiationException ex) {
                    ex.printStackTrace();
                } catch (ClassNotFoundException ex) {
                    ex.printStackTrace();
                }
                /* Turn off metal's use of bold fonts */
                UIManager.put("swing.boldMetal", Boolean.FALSE);

                //Schedule a job for the event dispatch thread:
                //creating and showing this application's GUI.
                createAndShowGUI();
            }
        });
    }

    public class CardLayoutDemo implements ItemListener {

        JPanel cards; //a panel that uses CardLayout
        final static String BUTTONPANEL = "Card with JButtons";
        final static String TEXTPANEL = "Card with JTextField";

        public void addComponentToPane(Container pane) {
            //Put the JComboBox in a JPanel to get a nicer look.
            JPanel comboBoxPane = new JPanel(); //use FlowLayout
            String comboBoxItems[] = {BUTTONPANEL, TEXTPANEL};
            JComboBox cb = new JComboBox(comboBoxItems);
            cb.setEditable(false);
            cb.addItemListener(this);
            comboBoxPane.add(cb);

            //Create the "cards".
            NewUser newUser = new NewUser();

            JPanel card1 = new JPanel();
            card1.add(new JButton("Button 1"));
            card1.add(new JButton("Button 2"));
            card1.add(new JButton("Button 3"));

            JPanel card2 = new NewUser();


            //Create the panel that contains the "cards".
            cards = new JPanel(new CardLayout());
            cards.add(card1, BUTTONPANEL);
            cards.add(card2, TEXTPANEL);

            pane.add(comboBoxPane, BorderLayout.PAGE_START);
            pane.add(cards, BorderLayout.CENTER);
        }

        public void itemStateChanged(ItemEvent evt) {
            CardLayout cl = (CardLayout) (cards.getLayout());
            cl.show(cards, (String) evt.getItem());
        }
    }

    /**
     * Create the GUI and show it. For thread safety, this method should be
     * invoked from the event dispatch thread.
     */
    protected void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("CardLayoutDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        CardLayoutDemo demo = new CardLayoutDemo();
        demo.addComponentToPane(frame.getContentPane());

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public class NewUser extends JPanel {

        private static final long serialVersionUID = 1L;
        private JLabel lblUsername, lblPassword, lblConfirmMsg;
        private JPasswordField txtPassword, txtCPassword;
        private JTextField txtUsername, txtName;
        private JButton btnSave, btnCancel;
        JPanel panelNewUser;
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

        public NewUser() {

//            this.setSize(350, 270);
//            this.setLocation((screen.width - 500) / 2, ((screen.height - 350) / 2));
//            this.setLayout(null);

            setLayout(new GridBagLayout());

            panelNewUser = this;
            lblUsername = new JLabel("Username");
            lblPassword = new JLabel("Password");
            lblConfirmMsg = new JLabel("Re-enter Password");
            txtName = new JTextField();

            txtUsername = new JTextField(12);
            txtPassword = new JPasswordField(12);
            txtCPassword = new JPasswordField(12);
            btnSave = new JButton("Save");
            btnCancel = new JButton("Cancel");

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.insets = new Insets(2, 2, 2, 2);
            gbc.anchor = GridBagConstraints.WEST;

//            lblUsername.setBounds(30, 30, 100, 25);
            this.add(lblUsername, gbc);
//            txtUsername.setBounds(150, 30, 150, 25);
            gbc.gridx++;
            this.add(txtUsername, gbc);
//            lblPassword.setBounds(30, 70, 100, 25);
            gbc.gridx = 0;
            gbc.gridy++;
            this.add(lblPassword, gbc);
//            txtPassword.setBounds(150, 70, 150, 25);
            gbc.gridx++;
            this.add(txtPassword, gbc);
//            lblConfirmMsg.setBounds(30, 110, 110, 25);
            gbc.gridx = 0;
            gbc.gridy++;
            this.add(lblConfirmMsg, gbc);
//            txtCPassword.setBounds(150, 110, 150, 25);
            gbc.gridx++;
            this.add(txtCPassword, gbc);
//            btnSave.setBounds(60, 155, 100, 25);
            gbc.gridx = 0;
            gbc.gridy++;
            gbc.anchor = GridBagConstraints.CENTER;
            this.add(btnSave, gbc);
//            btnCancel.setBounds(180, 155, 100, 25);
            gbc.gridx++;
            this.add(btnCancel, gbc);

            // This is a bad idea, use a DocumentFilter
            txtName.addKeyListener(new KeyAdapter() {
                public void keyTyped(KeyEvent e) {
                    char c = e.getKeyChar();
                    if (!(Character.isLetter(c) || (c == KeyEvent.VK_BACK_SPACE)
                                    || (c == KeyEvent.VK_SPACE) || (c == KeyEvent.VK_DELETE))) {
                        getToolkit().beep();
                        JOptionPane.showMessageDialog(null, "Invalid Character",
                                        "ERROR", JOptionPane.ERROR_MESSAGE);
                        e.consume();
                    }
                }
            });
            // This is a bad idea, use a DocumentFilter
            txtUsername.addKeyListener(new KeyAdapter() {
                public void keyTyped(KeyEvent e) {
                    char c = e.getKeyChar();
                    if (!(Character.isLetter(c) || (c == KeyEvent.VK_BACK_SPACE)
                                    || (Character.isDigit(c)) || (c == KeyEvent.VK_DELETE))) {
                        getToolkit().beep();
                        JOptionPane.showMessageDialog(null, "Invalid Character",
                                        "ERROR", JOptionPane.ERROR_MESSAGE);
                        e.consume();
                    }
                }
            });
            btnCancel.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    resetField();
                    panelNewUser.setVisible(false);
                    System.out.println("hello");
                }
            });
            btnSave.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(ActionEvent e) {

                    //DatabaseHelper databaseHelper = new DatabaseHelper();
                    if (txtUsername.getText() == null
                                    || txtUsername.getText().equals("")) {
                        JOptionPane.showMessageDialog(null, "Enter Username",
                                        "Missing fields", JOptionPane.DEFAULT_OPTION);
                        txtUsername.requestFocus();
                        return;
                    }
                    if (txtPassword.getPassword() == null
                                    || txtPassword.getPassword().equals("")) {
                        JOptionPane.showMessageDialog(null, "Enter Password",
                                        "Missing fields", JOptionPane.DEFAULT_OPTION);
                        txtPassword.requestFocus();
                        return;
                    }
                    if (txtCPassword.getPassword() == null
                                    || txtCPassword.getPassword().equals("")) {
                        JOptionPane.showMessageDialog(null,
                                        "Confirm your password", "Missing fields",
                                        JOptionPane.DEFAULT_OPTION);
                        txtCPassword.requestFocus();
                        return;
                    }
                    if (!txtPassword.getText()
                                    .equals(txtPassword.getText())) {
                        JOptionPane.showMessageDialog(null,
                                        "Passwords do not match.", "ERROR",
                                        JOptionPane.DEFAULT_OPTION);
                        txtCPassword.requestFocus();
                        return;
                    }


                    if (true) {
                        JOptionPane.showMessageDialog(null,
                                        "A new user is created", "SUCCESS",
                                        JOptionPane.DEFAULT_OPTION);
                        resetField();
                    }


                }
            });

        }// constructor closed

//action listener
        public void resetField() {

            txtUsername.setText("");
            txtCPassword.setText("");
            txtPassword.setText("");
        }
    }// class closed
}

You may also like to have a read through DocumentFilter.

If you prefer to do it manually, you need to attach a ComponentListener and monitor the componentResized event and re-layout all your components again.

You'll also want to look at the FontMetrics so you can properly take into account the differences in the font size between systems...

这篇关于在卡布局内定位组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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