使用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();
}
}
}
正是我想要的!這非常適合你的時間! – darkchampionz