先贴代码
public class MyPanel extends JPanel{
private ImagePanel imgPane;
private JLabel lbe;
private JTextField txtf;
protected String imagePath = "";
private String fileName = "";
public MyPanel(String filePath){
this.imagePath = filePath;
vertical = Box.createVerticalBox();
imgPane = new ImagePanel(this.imagePath);
imgPane.setPreferredSize(new Dimension(250,230));
fileName = this.imagePath.substring(15);
}
public void addZhu(){
lbe = new JLabel(fileName);
txtf = new JTextField(fileName);
vertical.add(imgPane);
vertical.add(Box.createVerticalStrut(1));
vertical.add(lbe);
add(vertical);
lbe.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e){
System.out.println("SSSS");
vertical.remove(lbe);
vertical.add(txtf);
txtf.requestFocus();//添加文本框时令文本框获取焦点
txtf.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent e) {
}
public void focusLost(FocusEvent e) {
fileName = txtf.getText();
vertical.remove(txtf);
lbe = new JLabel(fileName);
vertical.add(lbe);
revalidate();
}
});
add(vertical);
revalidate();
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
});
}
//其余的略
}
然后在Frame里面调用[code=Java]
scanDocContentPane[k] = new MyPanel(filePath);
scanDocContentPane[k].addZhu();
[/code]
这样label只能监听到一次点击事件,之后都无反应,这段代码要完成的功能是:原本在图像下面显示的是含有图像文件名的JLabel,当点击时能转变为JTextField修改,然后转移焦点,又变回JLabel。但是只能执行一次,而实际的需求是应该能执行任意次才对
public class MyPanel extends JPanel{
private ImagePanel imgPane;
private JLabel lbe;
private JTextField txtf;
protected String imagePath = "";
private String fileName = "";
public MyPanel(String filePath){
this.imagePath = filePath;
vertical = Box.createVerticalBox();
imgPane = new ImagePanel(this.imagePath);
imgPane.setPreferredSize(new Dimension(250,230));
fileName = this.imagePath.substring(15);
}
public void addZhu(){
lbe = new JLabel(fileName);
txtf = new JTextField(fileName);
vertical.add(imgPane);
vertical.add(Box.createVerticalStrut(1));
vertical.add(lbe);
add(vertical);
lbe.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e){
System.out.println("SSSS");
vertical.remove(lbe);
vertical.add(txtf);
txtf.requestFocus();//添加文本框时令文本框获取焦点
txtf.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent e) {
}
public void focusLost(FocusEvent e) {
fileName = txtf.getText();
vertical.remove(txtf);
lbe = new JLabel(fileName);
vertical.add(lbe);
revalidate();
}
});
add(vertical);
revalidate();
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
});
}
//其余的略
}
然后在Frame里面调用[code=Java]
scanDocContentPane[k] = new MyPanel(filePath);
scanDocContentPane[k].addZhu();
[/code]
这样label只能监听到一次点击事件,之后都无反应,这段代码要完成的功能是:原本在图像下面显示的是含有图像文件名的JLabel,当点击时能转变为JTextField修改,然后转移焦点,又变回JLabel。但是只能执行一次,而实际的需求是应该能执行任意次才对
public class RichTextLabel extends JPanel { private static final long serialVersionUID = 2638097214210073099L;
private String text;
private JTextField textfield;
private JLabel label;
private final CardLayout layout = new CardLayout(); public RichTextLabel(String text) {
setLayout(layout);
this.text = text;
initLabel();
initTextField(); } private void initLabel() {
label = new JLabel(this.text);
add(label, "label");
label.addMouseListener(new MouseAdapter() { @Override
public void mouseClicked(MouseEvent e) {
switchToTextField();
}
});
} public String getText() {
return this.text;
} private void initTextField() {
textfield = new JTextField(getText());
add(textfield, "textfield");
textfield.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent e) {
switchToLabel();
}
});
} private void switchToLabel() {
this.text = textfield.getText();
label.setText(getText());
layout.show(this, "label");
} private void switchToTextField() {
textfield.setText(label.getText());
layout.show(this, "textfield");
textfield.requestFocusInWindow();
} public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.getContentPane().add(new RichTextLabel("Just a demo"));
frame.getContentPane().add(new JButton("OK"), BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
});
}
}