简体   繁体   English

打开另一个JFrame时JFrame卡住了

[英]JFrame stuck when opening another JFrame

EDIT2 : Tested it. EDIT2 :测试过它。 It is definetily a problem with the new JPanel. 它定义为新JPanel的一个问题。

SOLUTION : As proposed the answer is that i forgot to explicitely set my JPanel onto my JFrame. 解决方案 :正如所提出的,答案是我忘了将我的JPanel明确地设置到我的JFrame上。 Change: 更改:

setContentPane(contentPanetagesliste);

to

frame.setContentPane(contentPanetagesliste);

and it works for me! 它对我有用!


My Problem is a strange behavior of JFrame. 我的问题是JFrame的一个奇怪的行为。

In my code I got a radioButton on my MainFrame. 在我的代码中,我的MainFrame上有一个radioButton。 When it is pressed it opens up the other Frame. 按下它时会打开另一个框架。

RadioBTNtagesliste.addActionListener (new ActionListener () {
    public void actionPerformed(ActionEvent e) {
                    frame.setSize(590, 450); ...

In my new Frame I made a JPanel. 在我的新框架中,我制作了一个JPanel。 Before adding the JPanel the other Frame didn't freeze, so I believe the problem got something to do with the new JPanel. 在添加JPanel之前,其他Frame没有冻结,所以我认为这个问题与新的JPanel有关。

JPanel contentPanetagesliste;

contentPanetagesliste = new JPanel();

contentPanetagesliste.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPanetagesliste);

In the end I add a JScrollPane to the JPanel (that's the reason why I had to make one) and make the JFrame visible. 最后,我将一个JScrollPane添加到JPanel(这就是为什么我必须创建一个)并使JFrame可见。

JScrollPane jsp = new JScrollPane(contentPanetagesliste);
frame.add(jsp);


frame.setVisible(true);

I absolutely have no clue how the new JPanel could interfere with the Main JFrame or the JPanel lying above the MainJFrame. 我完全不知道新的JPanel如何干扰主JFrame或位于MainJFrame上方的JPanel。 I hope some of you can help me. 我希望你们中的一些人可以帮助我。

EDIT: because it wasn't clear to some: by stuck i mean unresponsive and when i move it, it goes blank grey. 编辑:因为有些人不清楚:卡住我的意思是没有反应,当我移动它时,它变成了空白的灰色。

To the image: When I press the radio button to the far top right, the new window opens, is fully functional but the old window is stuck. 到图像:当我按下单选按钮到最右上方,新窗口打开,功能完全但旧窗口卡住了。

image 图片


For additional information I'll post the complete code from my action listener with explanation. 有关其他信息,我将从我的动作监听器发布完整代码并附上说明。

RadioBTNtagesliste.addActionListener (new ActionListener () {
    public void actionPerformed(ActionEvent e) {
        try {
            if(RadioBTNtagesliste.isSelected() == true){
                String Eintrag = HttpClass.httpRequestEintrag(usernamevar,EintragStringPK,"eintragTABLE");
                 JSONObject jsonObjectEintrag = new JSONObject(Eintrag); 
                int ProjektJSONlength = jsonObjectEintrag.length() - 1;

                frame.setSize(590, 450);
                JPanel contentPanetagesliste;

                contentPanetagesliste = new JPanel();

                contentPanetagesliste.setBorder(new EmptyBorder(5, 5, 5, 5));
                setContentPane(contentPanetagesliste);

                double[] temp = new double[ProjektJSONlength+2];
                temp[0] = 10;
                temp[1] = 30;
                for(int i = 0; i < ProjektJSONlength; i++){
                    temp[i+2] = 25;
                }

                double size[][] = {{2, 90, 90, 90, 90, 90,90, 2}, // Columns
                    temp}; // Rows                      
                contentPanetagesliste.setLayout(new TableLayout(size));                     
                String label[] = {"Eintrag Nr.", "Projekt", "Aktivität", "Dauer", "Beschreibung", "Symbol"};

                JButton button[] = new JButton[label.length];
                JTextField textfield[] = new JTextField[label.length];

                for (int i = 0; i < label.length; i++) {
                    button[i] = new JButton(label[i]);
                }   

                contentPanetagesliste.add(button[0], "1, 1");
                contentPanetagesliste.add(button[1], "2, 1");
                contentPanetagesliste.add(button[2], "3, 1");
                contentPanetagesliste.add(button[3], "4, 1");
                contentPanetagesliste.add(button[4], "5, 1");
                contentPanetagesliste.add(button[5], "6, 1");

                EintragStringPK = "Neu";                        

                for (int key=0; key<=ProjektJSONlength;key++) {
                    JSONObject jsonObjectProjekteObjekt0 = jsonObjectEintrag.getJSONObject(String.valueOf(key));  
                    String Eintraege = jsonObjectProjekteObjekt0.toString();
                    String EintragStringDauer = jsonObjectProjekteObjekt0.getString("dauer");  // String auslesen!!!                            
                    String EintragStringBEschreibung = jsonObjectProjekteObjekt0.getString("beschreibung");  // String auslesen!!!      
                    String EintragStringProjektname = jsonObjectProjekteObjekt0.getString("projektname");  // String auslesen!!!                            
                    String EintragStringAktivitaet = jsonObjectProjekteObjekt0.getString("kategorie");  // String auslesen!!!                           
                    EintragStringPK = jsonObjectProjekteObjekt0.getString("pk_ei_id");  // String auslesen!!!               

                    System.out.println("test" + key);
                    System.out.println(ProjektJSONlength);  
                    String labelCONTENT[] = {EintragStringPK, EintragStringProjektname, EintragStringAktivitaet, EintragStringDauer, EintragStringBEschreibung, "Symbol"};

                    for (int i = 0; i < labelCONTENT.length; i++) {
                        textfield[i] = new JTextField(labelCONTENT[i]);
                    }
                    int keypone = key+2;
                    String keyString = Integer.toString(keypone);
                    contentPanetagesliste.add(textfield[0], "1, "+keyString);
                    contentPanetagesliste.add(textfield[1], "2, "+keyString);
                    contentPanetagesliste.add(textfield[2], "3, "+keyString);
                    contentPanetagesliste.add(textfield[3], "4, "+keyString);
                    contentPanetagesliste.add(textfield[4], "5, "+keyString);
                    contentPanetagesliste.add(textfield[5], "6, "+keyString);
                }

                JScrollPane jsp = new JScrollPane(contentPanetagesliste);
                frame.add(jsp);
                frame.setVisible(true);
                System.out.println("ende");
            }else{
                frame.setVisible(false);
                System.out.println("ende2");    
            }
            System.out.println("ende3");

        } catch (JSONException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        System.out.println("ende4");

    }
});

Explanation: 说明:

I create the JFrame, afterwards the JPanel. 我创建了JFrame,之后是JPanel。

Create an Array and the rest necessary for my Table, because the goal is to make a generated table with data from a SQL DB. 创建一个数组和我的表所需的其余部分,因为目标是使用来自SQL DB的数据生成一个生成的表。

There is also a HTTPrequest and a loop to get out the data and put it into the table fields. 还有一个HTTPrequest和一个循环来获取数据并将其放入表字段中。 Still in the loop the filled table fields are added to the content pane and at the very and the Jpanel get's his JScrollPane and the frame is set visible. 仍然在循环中,填充的表字段被添加到内容窗格中,并且在Jpanel上获取他的JScrollPane并且框架被设置为可见。

I really hope someone can help me, and if you got tips for making better questions I've got an open ear. 我真的希望有人可以帮助我,如果你得到了提出更好问题的提示,我就会耳目一新。

setContentPane(contentPanetagesliste); should be explicit set to the new frame you created like this: frame.setContentPane(contentPanetagesliste); 应该显式设置为您创建的新帧,如下所示: frame.setContentPane(contentPanetagesliste); .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM