【发布时间】: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