gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 10:38:42 27 4
gpt4 key购买 nike

我创建了几个用于显示值的 JLabels(来自 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);
}
});

最佳答案

您可以通过调用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

希望这有帮助:)

关于java - JLabel 在 mousePressed 期间不删除以前的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45938822/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com