gpt4 book ai didi

java - 文本字段使用卡片布局丢弃先前输入的值

转载 作者:行者123 更新时间:2023-12-01 12:52:56 26 4
gpt4 key购买 nike

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

  1. 在 JtextField 中输入内容
  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();
}
});
}
}

最佳答案

I want the value I previously entered into Jpanel to disappear.

向面板添加一个 ComponentListener 并监听 componentShow(...) 事件以重置文本字段。

while I am on the Text Panel, I don't want the text to disappear when I lost focus to the text field

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

作为一般注释,不要使用空布局。 Swing 被设计为与布局管理器一起使用。

关于java - 文本字段使用卡片布局丢弃先前输入的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24085117/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com