2015-09-05 102 views
1

我有一個JLabel與一些文本,我想只要鼠標進入它,一個JTextField出現在文本的位置,以便用戶可以輸入一些文本。如果鼠標不在上面,它會正常顯示其文本。我用: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有一些麻煩。我怎麼能這樣做?

回答

3

使用JPanel使用CardLayout來保存JLabel和JTextField,然後在需要時使用CardLayout的show(...)方法交換它們。不要忘記將文本從JTextField傳送到mouseExited方法中的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(); 
      } 
     }); 
    } 
} 

或者,如果你要交換回的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(); 
     } 
    } 
} 
+0

正是我想要的!這非常適合你的時間! – darkchampionz