当前位置: 首页 > 知识库问答 >
问题:

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

楮自珍
2023-03-14

我使用的是卡片布局,有两个面板与之关联。一个面板有一个J文本字段。我希望在执行以下步骤后,之前输入到Jpanel中的值消失:

  1. 在JextField中输入某物
  2. 转到另一个面板
  3. 然后回到第一个面板

此外,当我在文本面板上时,我不希望文本在我失去对文本字段的焦点时消失。我如何才能完成这种行为?

下面是一个简单的程序来显示我的问题。

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个答案

岳枫
2023-03-14

可以放弃如下值:

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

所以在mouseclickor任何你认为合适的地方,你可以尝试上面的方法

陆甫
2023-03-14

更改您的itemStateChanged(Itemeventevt)如下所示

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

另外,不要忘记声明JTextFieldglobal

完整代码:

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();
            }
        });
    }
}
司徒斌
2023-03-14

我希望我以前输入到Jboard的值消失。

在面板中添加一个ComponentListener,并监听componentShow(…) 事件重置文本字段。

当我在文本面板上时,我不希望文本在我失去对文本字段的焦点时消失

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

一般来说,不要使用空布局。Swing设计用于布局管理器。

 类似资料:
  • 我试图得到一个布局就像下面的图像为一个游戏在android studio 当前我的xml是 我想在下一个级别增加更多的卡(如lvl 2)我需要为每个级别做几个布局吗?

  • 我正在尝试改变当前可见的卡片布局与幻灯片效果。但我在幻灯片的开始看到一个我无法调试/解决的闪烁。我怎么才能避开那部电影呢? 下面是再现错误的示例代码: 这里首先显示(“Harry Joy”)。然后我使(“Harsh Raval”)可见,并尝试更改两者的位置以提供幻灯片效果。但这里发生的是第一次两个标签显示在彼此的顶部,然后它开始滑动。我的意思是把两个标签都显示在对方的上面,我怎么才能停止呢?如果你

  • 我必须为学校制作一个游戏,而且我在点击jb按钮切换jpanel时遇到了一些麻烦。我想使用CardLayout,但我对Java还不熟悉,这使得它非常困难。我的目标是将我的所有面板放在不同的类中,比如类“Panel 1”,类“Panel 2”等等(而不是在我的主(JFrame)类中创建我的JPanel,这样我的代码更容易阅读)。是否可以将CardLayout容器放入包含my JFrame的类中?还有,

  • 描述 (Description) 卡片HTML布局包含许多类,如下所示 - S.No 课程和描述 1 cards 这是卡片容器。 2 card-header 它是可选的卡片标题,用于显示卡片标题。 3 card-footer 它是可选的,用于指定其他信息或自定义链接。 4 card-content 它是卡内容的主要容器,是必需的。 5 card-content-inner 它是可选的附加内包装,用

  • 我有一个应用程序,它使用选项卡进行导航。通过这些选项卡,我在布局中使用和。 我已经在应用程序的主中配置了这些内容。在这里,我为添加了一个页面更改监听器,并更改了关于位置的选项卡。当按下选项卡时,我也会以另一种方式进行操作。这都没问题。 然后我就有了标签中的内容。它们都扩展了类。第一个必须根据设备改变布局。在横向平板电脑()上,我有一个包含两个片段的布局,而其他设备只有一个片段。这是由appiate

  • 我是刚到爪哇的。我想把我的cardlayout添加到JFrame中。我之所以要这样做,是因为我可以通过关闭jframe在单击exit按钮时退出框架(窗口)。下面的程序是对JFrame的扩展。但我想声明JFrame,并在上面添加卡片。我试过了,但没有成功。我还想把card1的Jpanel设置为500(宽),500(高),但全屏显示。