Java 为什么画的线还没有消失?

Java 为什么画的线还没有消失?,java,swing,jframe,components,draw,Java,Swing,Jframe,Components,Draw,我不明白为什么已经绘制的组件不会消失,因为我更新了组件的位置 只有底部的正方形应该是可见的! 这是我的自定义组件类(抱歉是德语) 这是我的DrawUtils课程: package de.jakob.java.sitzplan.utils; import java.awt.Graphics; public class DrawUtils { public static void drawRelativeLineWithStroke(Graphics g, int x, int y,

我不明白为什么已经绘制的组件不会消失,因为我更新了组件的位置

只有底部的正方形应该是可见的! 这是我的自定义组件类(抱歉是德语)

这是我的DrawUtils课程:

package de.jakob.java.sitzplan.utils;

import java.awt.Graphics;

public class DrawUtils {


    public static void drawRelativeLineWithStroke(Graphics g, int x, int y, int width, int height) throws Exception{
        g.drawLine(x, y, x + width, y + height);

    }

    public static void drawPosLineWithStroke(Graphics g, int x, int y, int xend, int yend) throws Exception  {
        g.drawLine(x, y, xend, yend);
    }



}
这是一个又长又复杂的画面。。。(不要介意nok已知对象,只需使用带有onUpdate(称为每50毫秒一次)的鼠标和JTisch关注更新即可

package de.jakob.java.sitzplan.frames;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
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 javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import de.jakob.java.sitzplan.core.Core;
import de.jakob.java.sitzplan.elements.JDoppeltisch;
import de.jakob.java.sitzplan.elements.JTisch;
import de.jakob.java.sitzplan.tickthread.FrameUpdate;
import de.jakob.java.sitzplan.tickthread.TickHandler;
import java.awt.event.MouseMotionAdapter;

@SuppressWarnings("serial")
public class SitzplanErstellen extends JFrame implements FrameUpdate {

    public JFrame frame;
    public JFrame parent;

    public boolean mousePressed = false;

    public int mouseX = 0;
    public int mouseY = 0;

    public boolean hovered;

    ArrayList<JTisch> tische = new ArrayList<JTisch>();
    ArrayList<JDoppeltisch> doppeltische = new ArrayList<JDoppeltisch>();
    // ArrayList<Tafel> tafeln = new ArrayList<Tafel>();

    @Override
    public void onUpdate() {
        for (JTisch t : tische) {

            if (mouseX >= t.posX && mouseX <= (t.posX + t.width)) {
                System.out.println("hi");
                if (mouseY >= t.posY + t.height && mouseY <= (t.posY + t.height * 2)) {
                    if (mousePressed) {
                        t.posX = mouseX;
                        t.posY = mouseY;
                    }
                }
            }
            t.paintComponent(frame.getContentPane().getGraphics());
        }
        for (JDoppeltisch dt : doppeltische) {
            dt.paintComponent(frame.getContentPane().getGraphics());
        }


    }

    /**
     * Create the application.
     */
    public SitzplanErstellen(boolean b, JFrame parent) {
        this.parent = parent;
        TickHandler.registerClass(this);
        initialize();
        frame.setVisible(b);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();

        frame.getContentPane().setBackground(Color.GRAY);
        frame.setBounds(Core.rect);

        frame.getContentPane().setLayout(null);

        frame.setIconImage(
                new ImageIcon(SitzplanErstellen.class.getResource("/de/jakob/java/sitzplan/core/icon.png")).getImage());

        JLabel lblKomponenten = new JLabel("Komponenten");
        lblKomponenten.setFont(new Font("SansSerif", Font.BOLD, 11));
        lblKomponenten.setBounds(13, 61, 78, 15);
        frame.getContentPane().add(lblKomponenten);

        JPanel panel = new JPanel();
        panel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                hovered = true;
            }
        });
        panel.setBackground(Color.WHITE);
        panel.setBounds(136, 0, 1153, 635);
        frame.getContentPane().add(panel);

        JLabel lblSchler = new JLabel("Sch\u00FCler");
        lblSchler.setFont(new Font("SansSerif", Font.BOLD, 11));
        lblSchler.setBounds(13, 201, 78, 15);
        frame.getContentPane().add(lblSchler);

        JButton btnTafel = new JButton("Tafel");
        btnTafel.setBounds(13, 121, 97, 23);
        frame.getContentPane().add(btnTafel);
        btnTafel.setBackground(Color.LIGHT_GRAY);
        btnTafel.setToolTipText("Einen neue Tafel zum Klassenraum hinzuf\u00FCgen");

        JButton btnDoppeltisch = new JButton("Doppeltisch");
        btnDoppeltisch.setBounds(13, 87, 97, 23);
        frame.getContentPane().add(btnDoppeltisch);
        btnDoppeltisch.setBackground(Color.LIGHT_GRAY);
        btnDoppeltisch.setToolTipText("Einen neuen Doppeltisch zum Klassenraum hinzuf\u00FCgen");

        JButton btnEinzeltisch = new JButton("Einzeltisch");
        btnEinzeltisch.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JTisch jt = new JTisch(200, 100, 0, null, null, frame);

                System.out.println("hi2");
                tische.add(jt);
            }
        });
        btnEinzeltisch.setBounds(13, 153, 97, 23);
        frame.getContentPane().add(btnEinzeltisch);
        btnEinzeltisch.setBackground(Color.LIGHT_GRAY);
        btnEinzeltisch.setToolTipText("Einen neuen Einzeltisch zum Klassenraum hinzuf\u00FCgen");

        JButton btnNewButton = new JButton("<html>Schüler<br>eintragen</html>");
        btnNewButton.setBounds(13, 227, 97, 37);
        frame.getContentPane().add(btnNewButton);
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                frame.dispose();
                Core.setSchuelerListeVisible(true, frame);
            }
        });
        btnNewButton.setBackground(Color.LIGHT_GRAY);

        JButton button = new JButton("<html>Sch\u00FCler<br>Wünsche<br>eintragen</html>");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        button.setBackground(Color.LIGHT_GRAY);
        button.setBounds(12, 275, 98, 51);
        frame.getContentPane().add(button);

        JButton button_1 = new JButton("Back");
        button_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                parent.setVisible(true);
                frame.dispose();
            }
        });
        button_1.setIcon(new ImageIcon(SitzplanErstellen.class.getResource("/de/jakob/java/sitzplan/core/pfeil.png")));
        button_1.setFont(new Font("Dialog", Font.BOLD, 12));
        button_1.setBackground(Color.LIGHT_GRAY);
        button_1.setBounds(0, 0, 98, 26);
        frame.getContentPane().add(button_1);
        btnDoppeltisch.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                doppeltische.add(new JDoppeltisch(100, 50, 0, null, null, frame));
            }
        });

        for (Component c : frame.getContentPane().getComponents()) {
            c.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    mousePressed = true;
                    System.out.println("press");
                }

                @Override
                public void mouseReleased(MouseEvent e) {
                    mousePressed = false;
                    System.out.println("not press");
                }
            });

            c.addMouseMotionListener(new MouseMotionAdapter() {
                @Override
                public void mouseMoved(MouseEvent e) {
                    mouseX = e.getLocationOnScreen().x - frame.getLocationOnScreen().x;
                    System.out.println(mouseX);
                    mouseY = e.getLocationOnScreen().y - frame.getLocationOnScreen().y;
                    System.out.println(mouseY);
                }

                @Override
                public void mouseDragged(MouseEvent e) {
                    mouseX = e.getLocationOnScreen().x - frame.getLocationOnScreen().x;
                    System.out.println(mouseX);
                    mouseY = e.getLocationOnScreen().y - frame.getLocationOnScreen().y;
                    System.out.println(mouseY);
                }
            });
        }

        frame.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                mousePressed = true;
                System.out.println("press");
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                mousePressed = false;
                System.out.println("not press");
            }
        });

        frame.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                mouseX = e.getLocationOnScreen().x - frame.getLocationOnScreen().x;
                System.out.println(mouseX);
                mouseY = e.getLocationOnScreen().y - frame.getLocationOnScreen().y;
                System.out.println(mouseY);
            }

            @Override
            public void mouseDragged(MouseEvent e) {
                mouseX = e.getLocationOnScreen().x - frame.getLocationOnScreen().x;
                System.out.println(mouseX);
                mouseY = e.getLocationOnScreen().y - frame.getLocationOnScreen().y;
                System.out.println(mouseY);
            }
        });
    }
}
package de.jakob.java.sitzplan.frames;
导入java.awt.Color;
导入java.awt.Component;
导入java.awt.Font;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.awt.event.MouseAdapter;
导入java.awt.event.MouseEvent;
导入java.util.ArrayList;
导入javax.swing.ImageIcon;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入de.jakob.java.sitzplan.core.core;
导入de.jakob.java.sitzplan.elements.JDoppeltisch;
导入de.jakob.java.sitzplan.elements.JTisch;
导入de.jakob.java.sitzplan.tickthread.FrameUpdate;
导入de.jakob.java.sitzplan.tickshread.TickHandler;
导入java.awt.event.MouseMotionAdapter;
@抑制警告(“串行”)
公共类SitzplanErstellen扩展JFrame实现框架更新{
公共框架;
公共框架父级;
公共布尔值mousePressed=false;
公共int mouseX=0;
公共int mouseY=0;
公共布尔值悬停;
ArrayList tische=新的ArrayList();
ArrayList doppeltische=新的ArrayList();
//ArrayList tafeln=新的ArrayList();
@凌驾
公共无效更新(){
for(JTisch t:tische){

如果(mouseX>=t.posX&&mouseX=t.posY+t.height&&mouseY
public void paintComponent(Graphics g){try{DrawUtils.drawP..
“为什么还没有画线消失?”因为此代码会打断绘制链。请先调用super方法,BG将被绘制。同时替换
dt.paintComponent(frame.getContentPane().getGraphics());
by
dt.repaint('):
t.paintComponent(frame.getContentPane().getGraphics());
by
t.repaint()
package de.jakob.java.sitzplan.frames;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
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 javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import de.jakob.java.sitzplan.core.Core;
import de.jakob.java.sitzplan.elements.JDoppeltisch;
import de.jakob.java.sitzplan.elements.JTisch;
import de.jakob.java.sitzplan.tickthread.FrameUpdate;
import de.jakob.java.sitzplan.tickthread.TickHandler;
import java.awt.event.MouseMotionAdapter;

@SuppressWarnings("serial")
public class SitzplanErstellen extends JFrame implements FrameUpdate {

    public JFrame frame;
    public JFrame parent;

    public boolean mousePressed = false;

    public int mouseX = 0;
    public int mouseY = 0;

    public boolean hovered;

    ArrayList<JTisch> tische = new ArrayList<JTisch>();
    ArrayList<JDoppeltisch> doppeltische = new ArrayList<JDoppeltisch>();
    // ArrayList<Tafel> tafeln = new ArrayList<Tafel>();

    @Override
    public void onUpdate() {
        for (JTisch t : tische) {

            if (mouseX >= t.posX && mouseX <= (t.posX + t.width)) {
                System.out.println("hi");
                if (mouseY >= t.posY + t.height && mouseY <= (t.posY + t.height * 2)) {
                    if (mousePressed) {
                        t.posX = mouseX;
                        t.posY = mouseY;
                    }
                }
            }
            t.paintComponent(frame.getContentPane().getGraphics());
        }
        for (JDoppeltisch dt : doppeltische) {
            dt.paintComponent(frame.getContentPane().getGraphics());
        }


    }

    /**
     * Create the application.
     */
    public SitzplanErstellen(boolean b, JFrame parent) {
        this.parent = parent;
        TickHandler.registerClass(this);
        initialize();
        frame.setVisible(b);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();

        frame.getContentPane().setBackground(Color.GRAY);
        frame.setBounds(Core.rect);

        frame.getContentPane().setLayout(null);

        frame.setIconImage(
                new ImageIcon(SitzplanErstellen.class.getResource("/de/jakob/java/sitzplan/core/icon.png")).getImage());

        JLabel lblKomponenten = new JLabel("Komponenten");
        lblKomponenten.setFont(new Font("SansSerif", Font.BOLD, 11));
        lblKomponenten.setBounds(13, 61, 78, 15);
        frame.getContentPane().add(lblKomponenten);

        JPanel panel = new JPanel();
        panel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                hovered = true;
            }
        });
        panel.setBackground(Color.WHITE);
        panel.setBounds(136, 0, 1153, 635);
        frame.getContentPane().add(panel);

        JLabel lblSchler = new JLabel("Sch\u00FCler");
        lblSchler.setFont(new Font("SansSerif", Font.BOLD, 11));
        lblSchler.setBounds(13, 201, 78, 15);
        frame.getContentPane().add(lblSchler);

        JButton btnTafel = new JButton("Tafel");
        btnTafel.setBounds(13, 121, 97, 23);
        frame.getContentPane().add(btnTafel);
        btnTafel.setBackground(Color.LIGHT_GRAY);
        btnTafel.setToolTipText("Einen neue Tafel zum Klassenraum hinzuf\u00FCgen");

        JButton btnDoppeltisch = new JButton("Doppeltisch");
        btnDoppeltisch.setBounds(13, 87, 97, 23);
        frame.getContentPane().add(btnDoppeltisch);
        btnDoppeltisch.setBackground(Color.LIGHT_GRAY);
        btnDoppeltisch.setToolTipText("Einen neuen Doppeltisch zum Klassenraum hinzuf\u00FCgen");

        JButton btnEinzeltisch = new JButton("Einzeltisch");
        btnEinzeltisch.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JTisch jt = new JTisch(200, 100, 0, null, null, frame);

                System.out.println("hi2");
                tische.add(jt);
            }
        });
        btnEinzeltisch.setBounds(13, 153, 97, 23);
        frame.getContentPane().add(btnEinzeltisch);
        btnEinzeltisch.setBackground(Color.LIGHT_GRAY);
        btnEinzeltisch.setToolTipText("Einen neuen Einzeltisch zum Klassenraum hinzuf\u00FCgen");

        JButton btnNewButton = new JButton("<html>Schüler<br>eintragen</html>");
        btnNewButton.setBounds(13, 227, 97, 37);
        frame.getContentPane().add(btnNewButton);
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                frame.dispose();
                Core.setSchuelerListeVisible(true, frame);
            }
        });
        btnNewButton.setBackground(Color.LIGHT_GRAY);

        JButton button = new JButton("<html>Sch\u00FCler<br>Wünsche<br>eintragen</html>");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        button.setBackground(Color.LIGHT_GRAY);
        button.setBounds(12, 275, 98, 51);
        frame.getContentPane().add(button);

        JButton button_1 = new JButton("Back");
        button_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                parent.setVisible(true);
                frame.dispose();
            }
        });
        button_1.setIcon(new ImageIcon(SitzplanErstellen.class.getResource("/de/jakob/java/sitzplan/core/pfeil.png")));
        button_1.setFont(new Font("Dialog", Font.BOLD, 12));
        button_1.setBackground(Color.LIGHT_GRAY);
        button_1.setBounds(0, 0, 98, 26);
        frame.getContentPane().add(button_1);
        btnDoppeltisch.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                doppeltische.add(new JDoppeltisch(100, 50, 0, null, null, frame));
            }
        });

        for (Component c : frame.getContentPane().getComponents()) {
            c.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    mousePressed = true;
                    System.out.println("press");
                }

                @Override
                public void mouseReleased(MouseEvent e) {
                    mousePressed = false;
                    System.out.println("not press");
                }
            });

            c.addMouseMotionListener(new MouseMotionAdapter() {
                @Override
                public void mouseMoved(MouseEvent e) {
                    mouseX = e.getLocationOnScreen().x - frame.getLocationOnScreen().x;
                    System.out.println(mouseX);
                    mouseY = e.getLocationOnScreen().y - frame.getLocationOnScreen().y;
                    System.out.println(mouseY);
                }

                @Override
                public void mouseDragged(MouseEvent e) {
                    mouseX = e.getLocationOnScreen().x - frame.getLocationOnScreen().x;
                    System.out.println(mouseX);
                    mouseY = e.getLocationOnScreen().y - frame.getLocationOnScreen().y;
                    System.out.println(mouseY);
                }
            });
        }

        frame.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                mousePressed = true;
                System.out.println("press");
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                mousePressed = false;
                System.out.println("not press");
            }
        });

        frame.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                mouseX = e.getLocationOnScreen().x - frame.getLocationOnScreen().x;
                System.out.println(mouseX);
                mouseY = e.getLocationOnScreen().y - frame.getLocationOnScreen().y;
                System.out.println(mouseY);
            }

            @Override
            public void mouseDragged(MouseEvent e) {
                mouseX = e.getLocationOnScreen().x - frame.getLocationOnScreen().x;
                System.out.println(mouseX);
                mouseY = e.getLocationOnScreen().y - frame.getLocationOnScreen().y;
                System.out.println(mouseY);
            }
        });
    }
}