----------------解决方案--------------------------------------------------------
如果你加上了 没有报错就是能用 应该从JCompotent继承下来的组件都能实现键盘监听吧
----------------解决方案--------------------------------------------------------
我加上了,也没有报错,但是按键的时候却没有反应,很奇怪;我用同样的方法添加到JFrame中就可以
----------------解决方案--------------------------------------------------------
你把代码拿出来,让人看看,不就知道了```
----------------解决方案--------------------------------------------------------
肯定是能用的 你贴出代码来 给你看看拿写错了
----------------解决方案--------------------------------------------------------
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编辑过]
----------------解决方案--------------------------------------------------------
这是一段画线的程序,类似PPT中画折线段的.代码有点长,但与keyListener直接相关的就是红色的部分.我觉得添加keyListener是比较简单的,不应该会有错.可能是别的地方影响到了,但我却找不出来.
----------------解决方案--------------------------------------------------------