【问题标题】:JLabel during mousePressed don't delete a previous textmousePressed期间的JLabel不要删除以前的文本
【发布时间】:2017-08-29 12:10:25
【问题描述】:

我为显示值创建了几个 JLabel(来自 MySQL),这些值必须通过 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);
        }
    });

【问题讨论】:

    标签: java mysql intellij-idea jlabel


    【解决方案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
    

    希望这会有所帮助:)

    【讨论】:

      猜你喜欢
      • 2014-12-05
      • 2013-08-08
      • 1970-01-01
      • 2012-08-12
      • 1970-01-01
      • 1970-01-01
      • 2011-01-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多