java swing 请问代码使用JPanel为什么不能实现 鼠标拖动出现方形框?急急,求助,谢谢。
packagefileJtree;importjava.awt.Color;importjava.awt.Graphics;importjava.awt.Point;im...
package fileJtree;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class RectLabel extends JLabel {
private int x1 = 0;
private int y1 = 0;
private int x2 = 0;
private int y2 = 0;
private boolean current = true;
public RectLabel() {
super();
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent event) {
Point p = event.getPoint();
x1 = (int) p.getX();
y1 = (int) p.getY();
}
public void mouseReleased(MouseEvent event) {
Point p = event.getPoint();
x2 = (int) p.getX();
y2 = (int) p.getY();
current = true;
repaint();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent event) {
Point p = event.getPoint();
x2 = (int) p.getX();
y2 = (int) p.getY();
current = false;
repaint();
}
});
}
// 画圆的背景和标签
protected void paintChildren(Graphics g) {
if (!current)
return;
g.setColor(Color.red);
g.fillRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2),
Math.abs(y1 - y2));
super.paintComponent(g);
}
// 用简单的弧画按钮的边界。
protected void paintBorder(Graphics g) {
if (current)
return;
g.setColor(getForeground());
g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2),
Math.abs(y1 - y2));
}
public static void main(String[] args) {
JLabel test = new RectLabel();
JFrame frame = new JFrame();
JPanel jPanel=new JPanel();
jPanel.add(test);
frame.add(jPanel);
frame.setSize(300, 300);
frame.setVisible(true);
}
} 展开
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class RectLabel extends JLabel {
private int x1 = 0;
private int y1 = 0;
private int x2 = 0;
private int y2 = 0;
private boolean current = true;
public RectLabel() {
super();
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent event) {
Point p = event.getPoint();
x1 = (int) p.getX();
y1 = (int) p.getY();
}
public void mouseReleased(MouseEvent event) {
Point p = event.getPoint();
x2 = (int) p.getX();
y2 = (int) p.getY();
current = true;
repaint();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent event) {
Point p = event.getPoint();
x2 = (int) p.getX();
y2 = (int) p.getY();
current = false;
repaint();
}
});
}
// 画圆的背景和标签
protected void paintChildren(Graphics g) {
if (!current)
return;
g.setColor(Color.red);
g.fillRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2),
Math.abs(y1 - y2));
super.paintComponent(g);
}
// 用简单的弧画按钮的边界。
protected void paintBorder(Graphics g) {
if (current)
return;
g.setColor(getForeground());
g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2),
Math.abs(y1 - y2));
}
public static void main(String[] args) {
JLabel test = new RectLabel();
JFrame frame = new JFrame();
JPanel jPanel=new JPanel();
jPanel.add(test);
frame.add(jPanel);
frame.setSize(300, 300);
frame.setVisible(true);
}
} 展开
2个回答
展开全部
你的程序很混乱,我给你写了一个鼠标拖动出现方形框的Java程序,你看看吧.
H5.java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
class Point{
int x;
int y;
}
class MyRectPanel extends JPanel implements MouseListener,MouseMotionListener{
ArrayList<Point> al=new ArrayList<Point>();
int x,y;
MyRectPanel(){
setBackground(Color.WHITE);
addMouseListener(this);
addMouseMotionListener(this);
}
public void paint(Graphics g){
super.paint(g);
for(int i=0;i<al.size()-1;i+=2){
int width=Math.abs(al.get(i+1).x-al.get(i).x);
int height=Math.abs(al.get(i+1).y-al.get(i).y);
g.drawRect(al.get(i).x,al.get(i).y,width,height);//画矩形
}
if(al.size()>=1){
int width=Math.abs(x-al.get(al.size()-1).x);
int height=Math.abs(y-al.get(al.size()-1).y);
g.drawRect(al.get(al.size()-1).x,al.get(al.size()-1).y,width,height);
}
}
@Override
public void mouseClicked(MouseEvent me) {}
@Override
public void mouseEntered(MouseEvent me) {}
@Override
public void mouseExited(MouseEvent me) {}
@Override
public void mousePressed(MouseEvent me) {
Point p=new Point();
p.x=me.getX();
p.y=me.getY();
al.add(p);
}
@Override
public void mouseReleased(MouseEvent me) {
Point p=new Point();
p.x=me.getX();
p.y=me.getY();
al.add(p);
repaint();
}
@Override
public void mouseDragged(MouseEvent me) {
x=me.getX();
y=me.getY();
repaint();
}
@Override
public void mouseMoved(MouseEvent me) {}
}
public class H5 extends JFrame{
MyRectPanel p=new MyRectPanel();
H5(){
super("DrawRectangleDemo");
add(p);
setSize(500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
new H5();
}
}
运行结果
展开全部
跟Jpanel有什么关系,你监听的是JLabel test
而你的test跟本没有显示出来,
public static void main(String[] args) {
JLabel test = new RectLabel();
test.setPreferredSize(new Dimension(0, 0));
JFrame frame = new JFrame();
frame.getContentPane().add(test,BorderLayout.CENTER);
frame.setSize(300, 300);
frame.setVisible(true);
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询