论坛首页 Java企业应用论坛

刷新和添加事件监听出现的问题

浏览 1984 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-09-02   最后修改:2011-09-02
我先描述一下我遇到的问题:   我想实现这样的功能:每当我没按下“新增Label”按钮的时候会动态添加一个JLabel到我们显示的面板中,而且还要使得我们双击新添加的JLabel的时候会将这个新增的JLabel的内容打印出来。
但是,问题就是当我双击JLabel的时候,控制台都没办法打印这个JLabel的text内容。
我想请问一下有没有人知道如何解决这样的问题,谢谢!  (我有自己试过很多方法,也搜索过相应的内容,但是都没有的到想要的结果)


下面是我的代码:

package com.atschool.swing.test;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class UpdateTest extends JFrame implements ActionListener ,MouseListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private JButton clickBt = null;
private int size = 5;

public static void main(String [] args){
new UpdateTest().init();
}

private void init(){
this.setLayout(new BorderLayout());
this.setBounds(700, 100, 400, 500);
JPanel p_center = new JPanel(new GridLayout(size, 1));
JPanel p_south = new JPanel();
JLabel [] labels = new JLabel[size];
for(int i = 0; i < size; i ++) {
labels[i] = new JLabel("test : " + i);
labels[i].addMouseListener(this);
p_center.add(labels[i]);
}
clickBt = new JButton("新增Label");
clickBt.addActionListener(this);
p_south.add(clickBt);
this.add(p_center,BorderLayout.CENTER);
this.add(p_south,BorderLayout.SOUTH);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == clickBt) {
JLabel []labels = new JLabel[++size];
System.out.println("add ...");
JPanel p_center = new JPanel(new GridLayout(size,1));
for(int i = 0; i < size; i++){
labels[i] = new JLabel("test : " + i);
labels[i].addMouseListener(this);
p_center.add(labels[i]);
}
this.add(p_center,BorderLayout.CENTER);
//this.validate();
this.revalidate();
}
}

@Override
public void mouseClicked(MouseEvent e) {
if(e.getClickCount() == 2) {
JLabel j = (JLabel)e.getSource();
System.out.println("double click..." + j.getText() );
}
}

@Override
public void mousePressed(MouseEvent e) {

}

@Override
public void mouseReleased(MouseEvent e) {

}

@Override
public void mouseEntered(MouseEvent e) {
}

@Override
public void mouseExited(MouseEvent e) {

}
}

   发表时间:2011-09-02  

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;

public class UpdateTest extends JFrame {
	private JButton btnClick;
	private int size=5;
	private JPanel pnlLabels;
	private List<JLabel> labels=new ArrayList<JLabel>();
	public static void main(String[] args) throws Exception{
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		new UpdateTest().init();
	}
	
	private void init(){
		this.setLayout(new BorderLayout());
		this.setSize(400, 300);
		btnClick=new JButton("新增Label");
		JPanel pnlBtn=new JPanel();
		pnlBtn.add(btnClick);
		this.getContentPane().add(pnlBtn,BorderLayout.NORTH);
		
		btnClick.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnClickAction(e);
			}
		});
		
		pnlLabels=new JPanel();
		pnlLabels.setLayout(new GridLayout(5, 1));
		this.getContentPane().add(pnlLabels,BorderLayout.CENTER);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
	}
	
	private void btnClickAction(ActionEvent e){
		if (labels.size()<size) {
			final JLabel lbl=new JLabel("test:"+(labels.size()+1));
//			lbl.setBorder(new LineBorder(Color.blue));
			pnlLabels.add(lbl);
			labels.add(lbl);
			lbl.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent e) {
					if (e.getClickCount()==2) {
						System.out.println(lbl.getText());
					}
				}
			});
			pnlLabels.validate();
			pnlLabels.repaint();
		}
	}
}

0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics