| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1311 人关注过本帖
标题:[求助]JLabel能添加KeyListener事件吗?
只看楼主 加入收藏
zcx8532
Rank: 1
等 级:新手上路
帖 子:60
专家分:0
注 册:2005-11-9
收藏
 问题点数:0 回复次数:6 
[求助]JLabel能添加KeyListener事件吗?
如题,我试过程序却没有反应.
搜索更多相关主题的帖子: JLabel KeyListener 
2007-07-19 11:21
a276202460
Rank: 2
等 级:新手上路
威 望:4
帖 子:392
专家分:1
注 册:2007-4-10
收藏
得分:0 

如果你加上了 没有报错就是能用 应该从JCompotent继承下来的组件都能实现键盘监听吧


2007-07-19 15:59
zcx8532
Rank: 1
等 级:新手上路
帖 子:60
专家分:0
注 册:2005-11-9
收藏
得分:0 
我加上了,也没有报错,但是按键的时候却没有反应,很奇怪;我用同样的方法添加到JFrame中就可以

2007-07-19 20:39
heilong
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:777
专家分:0
注 册:2007-6-7
收藏
得分:0 
你把代码拿出来,让人看看,不就知道了```

风水鸡蛋壳,财去人安乐!----->
2007-07-20 12:23
a276202460
Rank: 2
等 级:新手上路
威 望:4
帖 子:392
专家分:1
注 册:2007-4-10
收藏
得分:0 

肯定是能用的 你贴出代码来 给你看看拿写错了


2007-07-20 17:54
zcx8532
Rank: 1
等 级:新手上路
帖 子:60
专家分:0
注 册:2005-11-9
收藏
得分:0 

package gui.line;

import gui.MainFrame;
import gui.ipslabel.HandleBorder;
import gui.ipslabel.IPSLabel;
import gui.mainright.workarea.WorkArea;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.util.Vector;

import javax.swing.JComponent;
import javax.swing.JLabel;

public class Line extends JLabel implements MouseListener,
MouseMotionListener, KeyListener {

private Vector<Point> path;
private NodeLabel[] nodeLabels;

private int startX;
private int startY;
private int endX;
private int endY;

private int thicknessValue = 3;

private boolean isPartition;
private double severedDegreeValue = 6.0;

private BasicStroke basicStroke;
private Color lineColor;

private boolean isFinished = false;
private boolean isSelected = true;

private Point topLeft;
private Point bottomRight;
private HandleBorder border;

private IPSLabel ipsLabel;

public Line() {
super();
this.basicStroke = new BasicStroke(thicknessValue);
this.startX = 0;
this.startY = 0;
this.endX = 0;
this.endY = 0;
this.isPartition = false;
this.path = new Vector<Point>();
path.add(new Point(startX, startY));
// initialize topLeft and bottomRight points
topLeft = getTopLeft();
topLeft.x = startX;
topLeft.y = startY;
bottomRight = getBottomRight();
bottomRight.x = startX;
bottomRight.y = startY;
initializeComponents();
}

public Line(int startX, int startY) {
super();
this.startX = startX;
this.startY = startY;
this.path = new Vector<Point>();
this.addPoint(new Point(startX, startY));
this.addPoint(new Point(startX, startY));
this.endX = startX;
this.endY = startY;
this.basicStroke = new BasicStroke(thicknessValue);
this.lineColor = Color.YELLOW;
this.isPartition = false;
//initialize topLeft and bottomRight points
topLeft = getTopLeft();
topLeft.x = startX;
topLeft.y = startY;
bottomRight = getBottomRight();
bottomRight.x = startX;
bottomRight.y = startY;
initializeComponents();
}

/**
* initialize components
*/
private void initializeComponents() {
this.setLinePosition(startX, startY, endX, endY, 6);
this.setLayout(null);

addMouseMotionListener(this);
addMouseListener(this);
addKeyListener(this);
}

/**
* when the attributes of SeverDegreeLine changed reset the attributes and
* changed its state
*
* @param beginX
* @param beginY
* @param endX
* @param endY
* @param severValue
*/
public void setLinePosition(int beginX, int beginY, int endX, int endY,
double severValue) {
if (beginX != -100)
startX = beginX;
if (beginY != -100)
startY = beginY;
if (endX != -100)
this.endX = endX;
if (endY != -100)
this.endY = endY;
if (severValue != -100)
severedDegreeValue = severValue;
}

public void setPartition(boolean isPartition) {
this.isPartition = isPartition;
this.lineColor = Color.black;
}

// public void showBorder() {
// if(border == null)
// border = new HandleBorder(topLeft.x, topLeft.y, bottomRight.x - topLeft.x, bottomRight.y - topLeft.y);
// setBorder(border);
// }
//
// public void hideBorder() {
// setBorder(null);
// }

public void mouseClicked(MouseEvent e) {
if(!isFinished) {
if(e.getClickCount() == 2) {
isFinished = true;
path.remove(path.size() - 1);
setEndPoint(e.getPoint());
setSelected(true);
}
else {
addPoint(e.getPoint());
topLeft.x = topLeft.x < e.getX() ? topLeft.x : e.getX();
topLeft.y = topLeft.y < e.getY() ? topLeft.y : e.getY();
bottomRight.x = bottomRight.x > e.getX() ? bottomRight.x : e.getX();
bottomRight.y = bottomRight.y > e.getY() ? bottomRight.y : e.getY();
repaint();
}
}
else {
setSelected(true);
}

}

public void mouseEntered(MouseEvent enteredE) {
}

public void mouseExited(MouseEvent exitedE) {
}

public void mousePressed(MouseEvent pressedE) {
}

public void mouseReleased(MouseEvent e) {
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}

public void mouseMoved(MouseEvent e) {
if(!isFinished) {
setEndPoint(e.getPoint());
repaint();
}
else if(isSelected) {
if(getNodeNumber(e.getX(), e.getY()) != -1)
setCursor(new Cursor(Cursor.HAND_CURSOR));
else
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
}

public void mouseDragged(MouseEvent e) {
if(isFinished && isSelected) {
int index = getNodeNumber(e.getX(), e.getY());
if(index != -1) {
nodeLabels[index].setLocation(e.getX() - 3, e.getY() - 3);
path.get(index).x = nodeLabels[index].getLocation().x + 3;
path.get(index).y = nodeLabels[index].getLocation().y + 3;
repaint();
}
}
}

private void addNodeLabels() {
if(nodeLabels == null) {
nodeLabels = new NodeLabel[path.size()];
for(int i = 0; i < path.size(); i++) {
Point p = path.get(i);
nodeLabels[i] = new NodeLabel();
nodeLabels[i].setLocation(p.x - 3, p.y - 3);
add(nodeLabels[i]);
}
repaint();
}
else {
for(int i = 0; i < nodeLabels.length; i++)
nodeLabels[i].setVisible(true);
}
}

private int getNodeNumber(int x, int y) {
for(int i = 0; i < path.size(); i++) {
// if(nodeLabels[i].contains(x - nodeLabels[i].getLocation().x, y - nodeLabels[i].getLocation().y))
if(x <= nodeLabels[i].getLocation().x + 4 && x >= nodeLabels[i].getLocation().x - 4 &&
y <= nodeLabels[i].getLocation().y + 4 && y >= nodeLabels[i].getLocation().y - 4)
return i;
}
return -1;
}

public void setSelected(boolean selected) {
isSelected = selected;
if(selected) {
addNodeLabels();
WorkArea workArea = WorkArea.getInstance();
MainFrame.getInstance().getMenuBarComp().getMoveMenu().getDrawPath().setEnabled(false);
workArea.setSelectedObject(this);

for(int i = 0; i < workArea.getComponentCount(); i++){
if(workArea.getComponent(i) instanceof IPSLabel) {
IPSLabel ipsLabel = (IPSLabel) workArea.getComponent(i);
if(ipsLabel.getBoolSet().isSelected()) {
ipsLabel.getBoolSet().setSelected(false);
ipsLabel.hideBorder();
}
}
else if(workArea.getComponent(i) instanceof Line) {
Line line = (Line) workArea.getComponent(i);
if(!line.equals(this))
line.setSelected(false);
}
}
}
else {
for(int i = 0; i < nodeLabels.length; i++)
nodeLabels[i].setVisible(false);
}
}

/**
* override paintComponent(Graphics g)
*
* @see JComponent
*/
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setStroke(basicStroke);
g2d.setColor(lineColor);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
Line2D line;
for(int i = 0; i < this.path.size()-1; i++) {
line = new Line2D.Float(path.get(i).x, path.get(i).y, path.get(i+1).x, path.get(i+1).y);
g2d.draw(line);
}
}

public boolean contains(int x, int y) {
if(isFinished){
return new Rectangle2D.Float(topLeft.x, topLeft.y,
bottomRight.x - topLeft.x, bottomRight.y - topLeft.y).contains(x, y);
}
else
return true;
}

public void setEndPoint(Point p) {
this.path.lastElement().x = p.x;
this.path.lastElement().y = p.y;
}

public void addPoint(Point p) {
this.path.add(p);
}

public Vector<Point> getPath() {
return this.path;
}

private Point getTopLeft() {
if(topLeft == null)
topLeft = new Point();
return topLeft;
}

private Point getBottomRight() {
if(bottomRight == null)
bottomRight = new Point();
return bottomRight;
}

public IPSLabel getIpsLabel() {
return ipsLabel;
}

public void setIpsLabel(IPSLabel ipsLabel) {
this.ipsLabel = ipsLabel;
}

public void keyPressed(KeyEvent e) {
System.out.println(e.getKeyChar());
}

public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub

}

public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub

}

}

[此贴子已经被作者于2007-7-22 20:09:54编辑过]


2007-07-22 20:02
zcx8532
Rank: 1
等 级:新手上路
帖 子:60
专家分:0
注 册:2005-11-9
收藏
得分:0 
这是一段画线的程序,类似PPT中画折线段的.代码有点长,但与keyListener直接相关的就是红色的部分.我觉得添加keyListener是比较简单的,不应该会有错.可能是别的地方影响到了,但我却找不出来.

2007-07-22 20:06
快速回复:[求助]JLabel能添加KeyListener事件吗?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021598 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved