Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 单击按钮时重新加载/刷新JFrame窗口_Java_Swing_Arraylist_Jbutton_Jlabel - Fatal编程技术网

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

Java 单击按钮时重新加载/刷新JFrame窗口,java,swing,arraylist,jbutton,jlabel,Java,Swing,Arraylist,Jbutton,Jlabel,在过去的两天里,我尝试了很多想法,但我无法刷新框架:/ 我连接我的数据库并进行选择查询。。。在我的控制台窗口中,我在表单上也看到了4个ID-s。。。在我在数据库中创建另一个数据后,在控制台窗口中我看到5个ID-s,但在我的表单4上。。。如果我把frame.setvisible放在我的按钮上,会出现另一个窗体 这是我的班级: JLabel timeLabel; private JPanel contentPane; private ArrayList<JLabel> lbllis

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

这是我的班级:

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;
}
}
JLabel-timeLabel;
私有JPanel内容窗格;
private ArrayList lbllist=new ArrayList();
private ArrayList btnlist=新的ArrayList();
静态int brojac=0;
私人JLabel lblNewLabel;
私人JButton btnRefresh;
公共静态void main(字符串[]args){
invokeLater(新的Runnable(){
公开募捐{
试一试{
Mainframe=newmain();
frame.setVisible(true);
}捕获(例外e){
e、 printStackTrace();
}
}
});
}
//构造函数(我每次都需要查看数据库中的数据)
公用干管(){
getContentPane().setLayout(null);
lblNewLabel=新JLabel(“新标签”);
lblNewLabel.立根(0,185,46,14);
getContentPane().add(lblNewLabel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
立根(100100800300);
contentPane=newjpanel();
setboorder(新的EmptyBorder(5,5,5,5));
setContentPane(contentPane);
contentPane.setLayout(null);
getContentPane().removeAll();
btnRefresh=新的JButton(“刷新”);
btnRefresh.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件arg0){
是否执行了刷新操作(arg0);
}
});
btnRefresh.setBounds(335228,89,23);
contentPane.add(btnRefresh);
EntityManager em=Connection.getEmf().createEntityManager();
TypedQuery query=em.createQuery(
“从Entityn中选择n,其中finished=0”,
PokusajNarudzbe.级);
List resultList=query.getResultList();
INTA=20;
for(Entitypokusaj实体:结果列表){
System.err.println(entity.getName());//这里我看到了所有数据(但不是
//在形式上
JLabel list=newjlabel(entity.getBrojName());
立根(10,a,200,20);
lista.setText(entity.getNarudzbaID()+“”
+entity.Name()+“”;
lbllist.add(列表);
getContentPane().add(列表);
list.setVisible(true);
list.repaint();
list.revalidate();
JButton btn=newjbutton(entity.getName());
btn.立根(500,a,200,20);
btn.setText(“Gotovo”);
btnlist.add(btn);
getContentPane().add(btn);
a+=20;
树胶:可见(真);
口香糖。重涂();
牙龈重新验证();
}
getContentPane().repaint();
resultList.clear();
em.clear();
em.close();
}
`受保护的void do_btnRefresh_actionPerformed(ActionEvent arg0){
主框架1=新的主框架();
SwingUtilities.updateComponentTreeUI(框架1);
frame1.invalidate();
frame1.validate();
frame1.repaint();
//frame1.revalidate();
//frame1.setVisible(true);(如果我取消对另一个表单apears的注释…)
}
@凌驾
公共整型读取(CharBuffer arg0)引发IOException{
//TODO自动生成的方法存根
返回0;
}
}

我能做什么?

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


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

还有一个问题…如果没有按钮,我如何每30秒重新加载一次表单?使用计时器-请参阅。我无法实现这些计时器…我创建了一个函数服务()它调用数据库并绘制标签和按钮…我想我需要一个动作每30秒重复一次,但我不知道如何:(我希望你能帮助我…谢谢你看到-在你的情况下,
计时器
应该或多或少地这样分配
计时器=新计时器(30000,新ActionListener(){public void actionPerformed(ActionEvent e){service();}}};
。最好像@rimero建议的那样将数据库访问放在一个单独的线程中,但我想随着您的学习,一步一个脚印可能会更好。