gpt4 book ai didi

java - 单击按钮时重新加载/刷新 JFrame 窗口

转载 作者:行者123 更新时间:2023-12-01 14:35:41 26 4
gpt4 key购买 nike

过去两天我尝试了很多想法,但我就是无法刷新框架:/我连接到我的数据库并进行选择查询...在我的控制台窗口中我也在我的表单上看到 4 个 ID-s...在我的数据库中创建另一个数据后,在我的控制台窗口中我看到 5 个 ID-s 但是在我的表单 4 上...如果我将 frame.setvisible 放在按钮上,则会出现另一个表单...

这是我的类(class):

JLabel timeLabel;


private JPanel contentPane;
private ArrayList<JLabel> lbllist= new ArrayList<JLabel>();
private ArrayList<JButton> btnlist= new ArrayList<JButton>();
static int brojac = 0;
private JLabel lblNewLabel;
private JButton btnRefresh;

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {


Mainframe = new Main();
frame.setVisible(true);

} catch (Exception e) {
e.printStackTrace();
}
}
});
}
//constructor (I need every time to see data from the database)
public Main() {


getContentPane().setLayout(null);

lblNewLabel = new JLabel("New label");
lblNewLabel.setBounds(0, 185, 46, 14);
getContentPane().add(lblNewLabel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

getContentPane().removeAll();
btnRefresh = new JButton("Refresh");
btnRefresh.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
do_btnRefresh_actionPerformed(arg0);
}
});
btnRefresh.setBounds(335, 228, 89, 23);
contentPane.add(btnRefresh);

EntityManager em = Connection.getEmf().createEntityManager();
TypedQuery<Entity> query = em.createQuery(
"select n from Entityn where finished= 0",
PokusajNarudzbe.class);
List<Entity> resultList = query.getResultList();
int a = 20;
for (Entitypokusaj entity: resultList) {

System.err.println(entity.getName());//here i see all data (but not
//in the form

JLabel list= new JLabel(entity.getBrojName());
lista.setBounds(10, a, 200, 20);
lista.setText(entity.getNarudzbaID() + " "
+ entity.Name() + " ";
lbllist.add(list);
getContentPane().add(list);
list.setVisible(true);
list.repaint();
list.revalidate();
JButton btn= new JButton(entity.getName());
btn.setBounds(500, a, 200, 20);
btn.setText("Gotovo");
btnlist.add(btn);
getContentPane().add(btn);
a += 20;
gumb.setVisible(true);
gumb.repaint();
gumb.revalidate();
}

getContentPane().repaint();
resultList.clear();
em.clear();
em.close();

}
` protected void do_btnRefresh_actionPerformed(ActionEvent arg0) {
Main frame1 = new Main();
SwingUtilities.updateComponentTreeUI(frame1);
frame1.invalidate();
frame1.validate();
frame1.repaint();
// frame1.revalidate();
// frame1.setVisible(true); (if I uncomment this another form apears...)

}


@Override
public int read(CharBuffer arg0) throws IOException {
// TODO Auto-generated method stub
return 0;
}
}

我能做什么?

最佳答案

您不应该在构造函数内从数据库中获取数据。这样您就无法更新表单 - 它仅在创建时加载数据。将数据获取逻辑移至单独的方法,并在按钮单击处理程序中调用它。

您现在所拥有的只会创建新表单,而不会更新原始表单。这就是为什么如果您取消注释 setVisible 部分,则会打开另一个表单。

关于java - 单击按钮时重新加载/刷新 JFrame 窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16508159/

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