我有一个带有一些文本的JLabel,我希望只要在它上面输入鼠标,就会在文本的位置出现一个JTextField,以便用户可以输入一些文本。如果鼠标不在其上,则只显示正常的文本。我用:
JLabel jl = new JLabel("Enter mouse to make a search!")
jl.addMouseListener(new MouseAdapter()
{
public void mouseEntered(MouseEvent evt)
{
JTextField textField = new JTextField(20);
//????
}
public void mouseExited(MouseEvent evt)
{
jl.setText("Enter mouse to make a search!");
}
});
然而,我在如何用jtextfield替换jlabel方面遇到了一些麻烦。我怎么能这样做?
答案 0 :(得分:3)
使用使用CardLayout的JPanel来保存JLabel和JTextField,然后在需要时使用CardLayout的show(...)
方法交换它们。不要忘记在mouseExited方法中将文本从JTextField传输到JLabel。
例如:
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
class SwapPanel extends JPanel {
private static final int COLUMNS = 20;
public static final String LABEL = "label";
public static final String TEXT_FIELD = "text field";
private JLabel label = new JLabel();
private JTextField textField = new JTextField(COLUMNS);
private CardLayout cardLayout = new CardLayout();
public SwapPanel() {
setLayout(cardLayout);
add(label, LABEL);
add(textField, TEXT_FIELD);
MyMouse myMouse = new MyMouse();
label.addMouseListener(myMouse);
textField.addMouseListener(myMouse);
}
private class MyMouse extends MouseAdapter {
@Override
public void mouseEntered(MouseEvent e) {
cardLayout.show(SwapPanel.this, TEXT_FIELD);
}
@Override
public void mouseExited(MouseEvent e) {
label.setText(textField.getText());
cardLayout.show(SwapPanel.this, LABEL);
}
}
}
import javax.swing.*;
public class TestSwapPanel extends JPanel {
private static final int GAP = 20;
private SwapPanel swapPanel = new SwapPanel();
public TestSwapPanel() {
setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
add(swapPanel);
}
private static void createAndShowGui() {
TestSwapPanel mainPanel = new TestSwapPanel();
JFrame frame = new JFrame("Swap Components");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
或者,如果您想在按Enter键时切换回JLabel,并且在交换时选择JTextField并选择所有文本:
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
class SwapPanel2 extends JPanel {
private static final long serialVersionUID = 1L;
private static final int COLUMNS = 20;
public static final String LABEL = "label";
public static final String TEXT_FIELD = "text field";
private JLabel label = new JLabel();
private JTextField textField = new JTextField(COLUMNS);
private CardLayout cardLayout = new CardLayout();
public SwapPanel2() {
setLayout(cardLayout);
add(label, LABEL);
add(textField, TEXT_FIELD);
MyMouse myMouse = new MyMouse();
label.addMouseListener(myMouse);
textField.addMouseListener(myMouse);
textField.addActionListener(myMouse);
}
public void showLabel() {
label.setText(textField.getText());
cardLayout.show(SwapPanel2.this, LABEL);
}
public void showTextField() {
textField.selectAll();
cardLayout.show(SwapPanel2.this, TEXT_FIELD);
textField.requestFocusInWindow();
}
private class MyMouse extends MouseAdapter implements ActionListener {
@Override
public void mouseEntered(MouseEvent e) {
showTextField();
}
@Override
public void mouseExited(MouseEvent e) {
showLabel();
}
@Override
public void actionPerformed(ActionEvent arg0) {
showLabel();
}
}
}