【发布时间】:2018-04-16 18:05:31
【问题描述】:
我正在编写一个代码,它将一个单词(棋盘游戏的名称)或一个字母作为输入并在我的数据库中搜索它。
如果有任何条目,我会在表格中显示它们,否则我不想显示空表,我想显示一个标签“未找到结果”,该标签会在 1500 毫秒后消失。
if 语句正常工作。例如,如果我写 't',我会在一个表格中找到所有以 't' 开头的游戏。
else 语句有时会出现问题:
- 如果我运行我的程序并且我首先写入输入“b”(在我的数据库中没有以“b”开头的条目)--> 它显示标签“未找到结果”,然后它消失了(它有效!)
- 如果我运行我的程序并写为输入“t”(如前所述),然后我写“a”(我有带有“a”的条目,它可以工作)[.....] 然后最后我写为输入'b'(在我的数据库中没有以'b'开头的条目)这是问题所在:它显示标签“未找到结果”但它也显示了带有我第一个输入结果的表(即以“t”开头的游戏)。
我做错了什么?谢谢!
点击“搜索”按钮后的代码如下:
btnCerca.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
/*Connect to DB:*/
Database db = new Database();
Connection connection = null;
try
{
connection = db.connectToDB();
}
catch (SQLException ex)
{
ex.printStackTrace();
}
/*Create a query which search in the DB the word/letter entered by the user*/
Query query = new Query (connection);
query.search(textField.getText()); //execute the query
/*Get results:*/
ResultSet rs = query.getResults();
/* check if rs is empty */
boolean res = false;
try
{
res = rs.isBeforeFirst();
}
catch (SQLException e1)
{
e1.printStackTrace();
}
ListTableModel model = null;
JScrollPane scrollPane = new JScrollPane();
if (res) //there is something in rs
{
try
{
model = ListTableModel.createModelFromResultSet(rs);
}
catch (SQLException e1)
{
e1.getMessage();
}
/*The table which will show the results*/
JTable table_1 = new JTable(model);
scrollPane.setBounds(10, 75, 1200, 300);
panelRicerca.add(scrollPane);
scrollPane.setViewportView(table_1);
}
else //rs is empty
{
JLabel lblNoGiochi = new JLabel("No results found.");
lblNoGiochi.setBounds(448, 30, 400, 14);
panelRicerca.add(lblNoGiochi);
panelRicerca.repaint();
/*...I want the label disappear after 1500ms:*/
int delay = 1500;
ActionListener taskPerformer = new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
panelRicerca.remove(lblNoGiochi);
panelRicerca.repaint();
}
};
new Timer(delay, taskPerformer).start();
}
}
});
------------编辑--------------- ---------
我做了一个更简单的例子,它受到相同问题的影响,因此您可以更快地编译和检查。
如果我写“你好”,我想显示一个表格。 如果我写其他东西,我希望表格显示一个标签。
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JLabel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Test {
private JFrame frame;
private JTextField textField;
private JTable table;
private JLabel lbl;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test window = new Test();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Test() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
lbl = new JLabel("Hey");
lbl.setBounds(378, 236, 46, 14);
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
textField = new JTextField();
textField.setBounds(102, 48, 86, 20);
frame.getContentPane().add(textField);
textField.setColumns(10);
JButton btnOk = new JButton("ok");
btnOk.addMouseListener(new MouseAdapter()
{
@Override
public void mouseClicked(MouseEvent arg0)
{
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(20, 20, 50, 50);
table = new JTable();
if (textField.getText().equals("hello"))
{
System.out.println("I'm in the if statement");
//show the table:
frame.getContentPane().add(scrollPane);
scrollPane.setViewportView(table);
}
else
{
System.out.println("I'm in the else statement");
//don't show the table:
scrollPane.remove(table);
/* Already tried to use:
- revalidate and repaint the scrollPane
- revalidate and repaint the contentPane
- remove the entire scrollPane from the ContentPane
- remove the table from the scrollPane, then remove the scrollPane from the ContentPane
*/
//and show a label instead:
frame.getContentPane().add(lbl);
frame.getContentPane().repaint();
}
}
});
btnOk.setBounds(211, 47, 89, 23);
frame.getContentPane().add(btnOk);
}
}
【问题讨论】:
-
如果您在添加表格时跟踪,我相信您需要在 else 条件下删除表格。
-
谢谢!我在添加标签之前尝试了 panelRicerca.remove(table_1) ,但结果是一样的。还有其他想法吗?
-
或许可以试试 scrollPane.remove(table_1)
-
没办法:/我从scrollPane中删除了表格,从面板中删除了表格,从面板中删除了scrollPane以及两者之间的组合。我还写了一个更简单的例子,只是为了排除数据库的问题,但没有任何改变。一旦我展示了表格,我就无法删除它。
-
我认为你应该看到这个stackoverflow.com/questions/6232355/…