Textfield使用卡片布局丢弃先前输入的值

时间:2014-06-06 15:08:47

标签: java swing jtextfield layout-manager cardlayout

我正在使用卡片布局并且有2个面板与之关联。一个面板有一个J文本字段。我希望在按照以下步骤操作后,我之前输入Jpanel的值会消失:

  1. 在JtextField中输入内容
  2. 转到其他面板
  3. 然后返回第一个小组
  4. 此外,当我在文本面板上时,当我将焦点丢失到文本字段时,我不希望文本消失。我该如何实现这种行为?

    这是一个显示我的问题的简单程序。

    import java.awt.BorderLayout;
    
    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".
        JPanel card1 = new JPanel();
        card1.add(new JButton("Button 1"));
        card1.add(new JButton("Button 2"));
        card1.add(new JButton("Button 3"));
    
    //my TEst panel----------------------------------------------------------------
        JPanel card2 = new JPanel();
        card2.setLayout(null);
        JLabel lblNewLabel = new JLabel("ncurrency");
        lblNewLabel.setBounds(77, 136, 92, 27);
        card2.add(lblNewLabel);
    
        //NCurrencyTextField currencyTextField = new NCurrencyTextField();
        JTextField currencyTextField = new JTextField();
        currencyTextField.setBounds(179, 139, 113, 27);
        card2.add(currencyTextField);
    
        JLabel lblNewLabel_1 = new JLabel("NText field label");
        lblNewLabel_1.setBounds(77, 212, 79, 14);
        card2.add(lblNewLabel_1);
    
        JTextField textField = new JTextField();
        textField.setBounds(179, 209, 113, 20);
        card2.add(textField);
        textField.setColumns(10);
        //END of my test panel----------------------------------------------------
    
        //Create the panel that contains the "cards".
        cards = new JPanel(new CardLayout());
        cards.add(card1, BUTTONPANEL);
        cards.add(card2, TEXTPANEL);
        ((CardLayout) cards.getLayout()).show(cards, TEXTPANEL);
    
        pane.add(comboBoxPane, BorderLayout.PAGE_START);
        pane.add(cards, BorderLayout.CENTER);
    }
    
    @Override
    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()
        {
            @Override
            public void run()
            {
                createAndShowGUI();
            }
        });
    }
    }
    

3 个答案:

答案 0 :(得分:1)

  

我希望我之前输入Jpanel的值消失。

在面板中添加ComponentListener并收听componentShow(...)事件以重置文本字段。

  

当我在文本面板上时,当我将焦点丢失到文本字段时,我不希望文本消失

我没有看到这种行为。一旦我在两个文本字段中的任何一个中输入文本,文本就会保留在那里。

作为一般性评论,请勿使用null布局。 Swing旨在与布局管理器一起使用。

答案 1 :(得分:0)

您可以放弃这样的值:

panel.remove();
revalidate();
repaint();

因此,在mouseClick或您认为合适的任何内容上,您可以尝试上述方法。

答案 2 :(得分:0)

更改您的itemStateChanged(ItemEvent evt),如下所示

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

也不要忘记宣布JTextField全球

完整代码:

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.awt.BorderLayout;

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";
    JTextField currencyTextField;
    JTextField textField;

    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".
        JPanel card1 = new JPanel();
        card1.add(new JButton("Button 1"));
        card1.add(new JButton("Button 2"));
        card1.add(new JButton("Button 3"));

        // my TEst
        // panel----------------------------------------------------------------
        JPanel card2 = new JPanel();
        card2.setLayout(null);
        JLabel lblNewLabel = new JLabel("ncurrency");
        lblNewLabel.setBounds(77, 136, 92, 27);
        card2.add(lblNewLabel);

        // NCurrencyTextField currencyTextField = new NCurrencyTextField();
        currencyTextField = new JTextField();
        currencyTextField.setBounds(179, 139, 113, 27);
        card2.add(currencyTextField);

        JLabel lblNewLabel_1 = new JLabel("NText field label");
        lblNewLabel_1.setBounds(77, 212, 79, 14);
        card2.add(lblNewLabel_1);

        textField = new JTextField();
        textField.setBounds(179, 209, 113, 20);
        card2.add(textField);
        textField.setColumns(10);
        // END of my test
        // panel----------------------------------------------------

        // Create the panel that contains the "cards".
        cards = new JPanel(new CardLayout());
        cards.add(card1, BUTTONPANEL);
        cards.add(card2, TEXTPANEL);
        ((CardLayout) cards.getLayout()).show(cards, 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());
        currencyTextField.setText("");
        textField.setText("");
    }

    /**
     * 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();
            }
        });
    }
}
相关问题