mousePressed期间的JLabel不删除以前的文本

时间:2017-08-29 12:10:26

标签: java mysql intellij-idea jlabel

我为显示值(来自MySQL)创建了几个JLabel,它必须通过mousePressed切换,但每次点击后所有值都会覆盖其他值。

在显示新值时,清除以前的文字需要做些什么?也许我的目标比JLabel有更好的元素?

    jt.addMouseListener(new MouseListener() {
        public void mouseClicked(MouseEvent me) { }
        public void mouseReleased(MouseEvent e) {}
        public void mouseEntered(MouseEvent e) {}
        public void mouseExited(MouseEvent e) {}
        public void mousePressed(MouseEvent me) {
            String director = null;
            String year = null;
            String bestrole = null;
            String genre = null;
            String mors = null;
            String production = null;
            String rate = null;
            String time = null;
            String date = null;
            if (me.getClickCount() == 1) {
                int typ = jt.getSelectedRow();
                if (typ>-1) EditButton.setEnabled(true);
                if (typ>-1) DeleteButton.setEnabled(true);
                {
                    try {
                        Connection conn2;
                        conn2 = DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);
                        Statement stmt2 = (Statement) conn2.createStatement();
                        String title = String.valueOf(jt.getValueAt(jt.getSelectedRow(), 0));
                        String query2 = "select * from movie where title='"+title+"'";
                        ResultSet rs2 = stmt2.executeQuery(query2);
                        rs2.next();
                        director = rs2.getString("Director");
                        year = rs2.getString("Year");
                        bestrole = rs2.getString("BestRole");
                        genre = rs2.getString("Genre");
                        mors = rs2.getString("Mors");
                        production = rs2.getString("Production");
                        rate = rs2.getString("Rate");
                        time = rs2.getString("Time");
                        date = rs2.getString("Date");
                    }
                    catch (Exception er) {System.err.println(er);}
                }
            }
            JLabel labDirector = new JLabel("Director: "+director);
            panel1.add(labDirector, constT1);
            JLabel labYear = new JLabel("Year: "+year);
            panel1.add(labYear, constT2);
            JLabel labBestrole = new JLabel("Best role: "+bestrole);
            panel1.add(labBestrole, constT3);
            JLabel labGenre = new JLabel("Genre: "+genre);
            panel1.add(labGenre, constT4);
            JLabel labMors = new JLabel("Type: "+mors);
            panel1.add(labMors, constT5);
            JLabel labProduction = new JLabel("Production: "+production);
            panel1.add(labProduction, constT6);
            JLabel labRate = new JLabel("Rate: "+rate);
            panel1.add(labRate, constT7);
            JLabel labTime = new JLabel("Time: "+time);
            panel1.add(labTime, constT8);
            JLabel labDate = new JLabel("Date: "+date);
            panel1.add(labDate, constT9);
            panel1.revalidate();
            System.out.println(director);
        }
    });

1 个答案:

答案 0 :(得分:1)

您可以通过调用它们上的remove()函数来删除标签。 假设你想修改'labDirector'标签,你可以这样做:

panel1.remove(labDirector);

删除以前的标签,然后将修改后的标签添加为:

panel1.add(labDirector, constT1);
正如您在代码中所做的那样

您可以对所有标签执行此操作以修改它们。

<强>更新

你也可以试试这个:

全局声明JLabel:

JLabel labDirector;    // Global declaration

并在函数中访问它们:

labDirector = new JLabel("Director: "+director);

全局声明将确保每次使用labDirector标签时,每次都访问相同的JLabel实例。

现在,在删除旧标签并向面板添加新标签后,您必须重新绘制面板以使用新值更新它。你可以这样做:

panel.remove(label1); // Remove the old label
labDirector = new JLabel("Director: "+director); // assign new value to new label
panel.add(label1); // add new label
panel.revaidate(); // revalidate panel
panel.repaint(); // repaint the window to update it

希望这会有所帮助:)