[英]Textfield discard previously entered value using card layout
我正在使用卡片布局,並且有2個面板與其關聯。 一個面板具有J文本字段。 在執行以下步驟之后,我希望以前輸入到Jpanel中的值消失:
另外,當我在“文本面板”上時,當我失去對文本字段的關注時,我也不希望文本消失。 我怎樣才能做到這一點?
這是一個顯示我的問題的簡單程序。
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();
}
});
}
}
我希望以前輸入Jpanel的值消失。
將ComponentListener
添加到面板中,並偵聽componentShow(...)
事件以重置文本字段。
當我在“文本面板”上時,當我失去對文本字段的關注時,我不希望文本消失
我沒有看到這種行為。 在兩個文本字段之一中輸入文本后,文本將保留在那里。
作為一般性評論,請勿使用空布局。 Swing旨在與布局管理器一起使用。
您可以舍棄這樣的值:
panel.remove();
revalidate();
repaint();
因此,在mouseClick上或您認為合適的任何方法上,都可以嘗試上述方法。
如下更改您的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();
}
});
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.