问题描述
我创建了一个项目。当我按“新建框”时,我想创建一个蓝色框。然后当我按“线”时,我想画一条线并将线和框连接起来。我创建了框的代码,这是有效的,但行的代码不起作用。实际上行的代码正在另一个窗口/文件夹中工作,但在这里不起作用。可能是我写代码的时候写错了。
这个窗口和主要代码..
public class Cerceve extends JFrame implements ActionListener {
JPanel MAINPANEL,LEFTPANEL,RIGHTPANEL;
JButton btnBox,btnClear;
DragListener drag = new DragListener();
JButton btnLine;
public Cerceve() {
// ÇERÇEVE OLUŞTUR
super("ÇİZGE PROGRAMI");
setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
setSize(1200,700);
setResizable(false);
setLocationRelativeto(null);
setVisible(true);
// ÜZERİNE BİLEŞENLERİ EKLE
setLayout(new BorderLayout());
MAINPANEL = new JPanel();
MAINPANEL.setBackground(Color.WHITE);
this.add(MAINPANEL,BorderLayout.CENTER);
MAINPANEL.setLayout(new BorderLayout());
LEFTPANEL = new JPanel();
LEFTPANEL.setBackground(Color.GRAY);
MAINPANEL.add(LEFTPANEL,BorderLayout.WEST);
RIGHTPANEL = new JPanel();
RIGHTPANEL.setBackground(Color.WHITE);
MAINPANEL.add(RIGHTPANEL,BorderLayout.CENTER);
btnBox = new JButton("New Box");
LEFTPANEL.add(btnBox);
btnClear = new JButton("Clear");
LEFTPANEL.add(btnClear);
btnLine = new JButton("Line");
LEFTPANEL.add(btnLine);
btnBox.addActionListener(this);
btnClear.addActionListener(this);
btnLine.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnBox) {
Box Box = new Box();
RIGHTPANEL.add(Box);
RIGHTPANEL.repaint();
Box.addMouseMotionListener(drag);
Box.addMouseListener(drag);
}
else if (e.getSource() == btnClear) {
RIGHTPANEL.removeAll();
RIGHTPANEL.repaint();
}
else if (e.getSource() == btnLine) {
Line line = new Line();
RIGHTPANEL.repaint();
RIGHTPANEL.add(line);
line.addMouseListener(line.mouseHandler);
line.addMouseMotionListener(line.mouseMotionListener);
}
}
}
这是一个盒子包装
public class Box extends JPanel {
public Box() {
setLayout(null);
setBackground(Color.BLUE);
setSize(50,50);
setLocation(0,0);
}
}
这是一个 DRAG LISTENER 包
class DragListener extends MouseInputAdapter {
Point location;
MouseEvent mouseInfo;
@Override
public void mousepressed(MouseEvent me) {
mouseInfo = me;
}
@Override
public void mouseDragged(MouseEvent me) {
if (SwingUtilities.isLeftMouseButton(me) == true) {
Component component = me.getComponent();
location = component.getLocation(location);
int x = location.x - mouseInfo.getX() + me.getX();
int y = location.y - mouseInfo.getY() + me.getY();
component.setLocation(x,y);
}
}
}
这是一个完整的包裹
public class Line extends JFrame {
private int xBegin = 0;
private int xEnd = 0;
private int yBegin = 0;
private int yEnd = 0;
public MouseListener mouseHandler = new MouseAdapter() {
@Override
public void mousepressed(MouseEvent e) {
xBegin = e.getX();
yBegin = e.getY();
repaint();
}
@Override
public void mouseReleased(MouseEvent e) {
xEnd = e.getX();
yEnd = e.getY();
repaint();
}
};
public MouseMotionListener mouseMotionListener = new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
xEnd = e.getX();
yEnd = e.getY();
repaint();
}
};
public void paint(Graphics g) {
super.paint(g);
g.drawLine(xBegin,yBegin,xEnd,yEnd);
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)