简体   繁体   English

从另一个Jframe打开一个Jframe,

[英]Open a Jframe from another Jframe,

i have a strange problem. 我有一个奇怪的问题。 I want to create a gui with multi Jframe, called when I press a button. 我想创建一个带有多个Jframe的gui,当我按下一个按钮时会调用它。 I have created the Jframe before with WindowsBuilder PRO, but is very very difficult to use because it does what he wants and not that what i need! 我之前使用WindowsBuilder PRO创建了Jframe,但是使用起来非常困难,因为它可以满足他的需求,而不是我所需要的! (for example if I resize a Object, WBP brings it another where) So i start to use JformDesigner and the problem is this: I have created 2 Jframe one is call Jform_GUI and the other Add_a. (例如,如果我调整一个对象的大小,WBP将它带到另一个位置),所以我开始使用JformDesigner,问题是这样的:我创建了2个Jframe,一个叫Jform_GUI,另一个叫Jform_GUI。 My program basically starts from Jform_GUI and by a button it calls Add_a, but if i use 我的程序基本上从Jform_GUI开始,并通过按钮调用Add_a,但是如果我使用

Add_a ad= new Add_a();
ad.setVisible(true);

it doesn't work and what i get is a little Jframe with no size and all blank. 它不起作用,我得到的是一个没有大小且全部空白的小Jframe。 I tried different solution but it doesn't work. 我尝试了其他解决方案,但没有用。 here is my code 这是我的代码

package palestra;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import com.intellij.uiDesigner.core.*;
import com.jgoodies.forms.factories.*;
import com.jgoodies.forms.layout.*;

import info.clearthought.layout.*;

/**
 * 
 */

public class Jform_GUI extends JFrame {
    Add_a ad;
    public Jform_GUI() {
        initComponents();
    }



    public static void main(String[] args) {


        EventQueue.invokeLater(new Runnable() {  //Main copied from WindowBuilder           
            public void run() {
                try {
                    Jform_GUI window = new Jform_GUI();
                    window.Login.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });                                                 //Main copied from WindowBuilder 
    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner Evaluation license - Calogero Carrabbotta
        Login = new JFrame();
        menuBar1 = new JMenuBar();
        menu1 = new JMenu();
        menuItem1 = new JMenuItem();
        label2 = new JLabel();
        label3 = new JLabel();
        label4 = new JLabel();
        passwordField1 = new JPasswordField();
        label5 = new JLabel();
        label6 = new JLabel();
        textField1 = new JTextField();
        button1 = new JButton();
        button2 = new JButton();
        button3 = new JButton();
        label1 = new JLabel();

        //======== Login ========
        {
            Login.setResizable(false);
            Login.setTitle("KGymm - Login");
            Container LoginContentPane = Login.getContentPane();
            LoginContentPane.setLayout(null);

            //======== menuBar1 ========
            {

                //======== menu1 ========
                {
                    menu1.setText("?");

                    //---- menuItem1 ----
                    menuItem1.setText("Credits");
                    menu1.add(menuItem1);
                }
                menuBar1.add(menu1);
            }
            Login.setJMenuBar(menuBar1);

            //---- label2 ----
            label2.setText("KGYMM");
            label2.setFont(new Font("Know Your Product", Font.PLAIN, 72));
            LoginContentPane.add(label2);
            label2.setBounds(250, 10, 315, 75);

            //---- label3 ----
            label3.setText("Benvenuto nel programma di gestione della vostra palestra. ");
            label3.setFont(new Font("Lusitana", Font.PLAIN, 20));
            LoginContentPane.add(label3);
            label3.setBounds(135, 90, 505, 41);

            //---- label4 ----
            label4.setText("Inserisci i tuoi dati per cominciare.");
            label4.setFont(new Font("Lusitana", Font.PLAIN, 20));
            LoginContentPane.add(label4);
            label4.setBounds(235, 135, 305, 41);
            LoginContentPane.add(passwordField1);
            passwordField1.setBounds(400, 260, 150, passwordField1.getPreferredSize().height);

            //---- label5 ----
            label5.setText("Password: ");
            label5.setFont(new Font("Lusitana", Font.PLAIN, 20));
            LoginContentPane.add(label5);
            label5.setBounds(195, 250, 155, 41);

            //---- label6 ----
            label6.setText("Cognome:");
            label6.setFont(new Font("Lusitana", Font.PLAIN, 20));
            LoginContentPane.add(label6);
            label6.setBounds(195, 210, 155, 41);
            LoginContentPane.add(textField1);
            textField1.setBounds(400, 220, 150, textField1.getPreferredSize().height);

            //---- button1 ----
            button1.setText("Registra un nuovo Allenatore");
            button1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) { //HERE IS THE BUTTON FOR CALLING THE NEW FRAME
                    Add_a ad= new Add_a();   
                    ad.setVisible(true);
                    Login.dispose(); //Login is the name of this Jframe


                }
            });
            LoginContentPane.add(button1);
            button1.setBounds(10, 450, 230, 80);

            //---- button2 ----
            button2.setText("Configurazione Inziale");
            LoginContentPane.add(button2);
            button2.setBounds(245, 450, 230, 80);

            //---- button3 ----
            button3.setText("Login");
            LoginContentPane.add(button3);
            button3.setBounds(550, 450, 230, 80);

            //---- label1 ----
            label1.setText("text");
            label1.setIcon(new ImageIcon(getClass().getResource("/img/Sfondo_no_KG_sfumano.png")));
            LoginContentPane.add(label1);
            label1.setBounds(new Rectangle(new Point(-10, -15), label1.getPreferredSize()));

            { // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < LoginContentPane.getComponentCount(); i++) {
                    Rectangle bounds = LoginContentPane.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = LoginContentPane.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                LoginContentPane.setMinimumSize(preferredSize);
                LoginContentPane.setPreferredSize(preferredSize);
            }
            Login.setSize(800, 600);
            Login.setLocationRelativeTo(null);
        }
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner Evaluation license 
    private JFrame Login;
    private JMenuBar menuBar1;
    private JMenu menu1;
    private JMenuItem menuItem1;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JPasswordField passwordField1;
    private JLabel label5;
    private JLabel label6;
    private JTextField textField1;
    private JButton button1;
    private JButton button2;
    private JButton button3;
    private JLabel label1;

    // JFormDesigner - End of variables declaration  //GEN-END:variables

} }

The second JFrame that i want to call is:Add_a 我要调用的第二个JFrame是:Add_a

package palestra;

import java.awt.*;

import javax.swing.*;

/**
 * 
 */
public class Add_a extends JFrame {
    /**
     * 
     */

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Add_a window = new Add_a();
                    window.Add_a.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private static final long serialVersionUID = 1L;
    public Add_a() {

        initComponents();
    }

    public void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner Evaluation license - Calogero Carrabbotta
        Add_a = new JFrame();
        label3 = new JLabel();
        label4 = new JLabel();
        label5 = new JLabel();
        label6 = new JLabel();
        label7 = new JLabel();
        label8 = new JLabel();
        label9 = new JLabel();
        label10 = new JLabel();
        textField1 = new JTextField();
        textField2 = new JTextField();
        textField3 = new JTextField();
        textField4 = new JTextField();
        textField5 = new JTextField();
        textField6 = new JTextField();
        button3 = new JButton();
        label1 = new JLabel();

        //======== Add_a ========
        {
            Add_a.setBackground(new Color(223, 217, 223));
            Container Add_aContentPane = Add_a.getContentPane();
            Add_aContentPane.setLayout(null);

            //---- label3 ----
            label3.setText("Compila tutti i campi qui sotto riportati per completare la tua registrazione.");
            label3.setFont(new Font("Lusitana", Font.PLAIN, 20));
            Add_aContentPane.add(label3);
            label3.setBounds(65, 25, 655, 41);

            //---- label4 ----
            label4.setText("Nome");
            label4.setFont(new Font("Lusitana", Font.PLAIN, 20));
            Add_aContentPane.add(label4);
            label4.setBounds(20, 71, 65, 41);

            //---- label5 ----
            label5.setText("Cognome");
            label5.setFont(new Font("Lusitana", Font.PLAIN, 20));
            Add_aContentPane.add(label5);
            label5.setBounds(20, 117, 100, 41);

            //---- label6 ----
            label6.setText("Password");
            label6.setFont(new Font("Lusitana", Font.PLAIN, 20));
            Add_aContentPane.add(label6);
            label6.setBounds(20, 163, 90, 41);

            //---- label7 ----
            label7.setText("Ripeti Password");
            label7.setFont(new Font("Lusitana", Font.PLAIN, 20));
            Add_aContentPane.add(label7);
            label7.setBounds(20, 210, 150, 41);

            //---- label8 ----
            label8.setText("Telefono");
            label8.setFont(new Font("Lusitana", Font.PLAIN, 20));
            Add_aContentPane.add(label8);
            label8.setBounds(20, 395, 90, 41);

            //---- label9 ----
            label9.setText("Mail");
            label9.setFont(new Font("Lusitana", Font.PLAIN, 20));
            Add_aContentPane.add(label9);
            label9.setBounds(20, 350, 50, 41);

            //---- label10 ----
            label10.setText("Campi Addizionali, possono essere lasciati in bianco");
            label10.setFont(new Font("Lusitana", Font.PLAIN, 20));
            Add_aContentPane.add(label10);
            label10.setBounds(20, 305, 655, 41);
            Add_aContentPane.add(textField1);
            textField1.setBounds(390, 80, 370, 28);
            Add_aContentPane.add(textField2);
            textField2.setBounds(390, 120, 370, 28);
            Add_aContentPane.add(textField3);
            textField3.setBounds(390, 165, 370, 28);
            Add_aContentPane.add(textField4);
            textField4.setBounds(390, 210, 370, 28);
            Add_aContentPane.add(textField5);
            textField5.setBounds(390, 350, 370, 28);
            Add_aContentPane.add(textField6);
            textField6.setBounds(390, 395, 370, 28);

            //---- button3 ----
            button3.setText("Crea Utente");
            Add_aContentPane.add(button3);
            button3.setBounds(540, 450, 230, 80);

            //---- label1 ----
            label1.setText("text");
            label1.setIcon(new ImageIcon(getClass().getResource("/img/Sfondo_no_KG_sfumano.png")));
            Add_aContentPane.add(label1);
            label1.setBounds(new Rectangle(new Point(-10, -10), label1.getPreferredSize()));

            Add_aContentPane.setPreferredSize(new Dimension(800, 600));
            Add_a.setSize(800, 600);
            Add_a.setLocationRelativeTo(null);
        }}
        // JFormDesigner - End of component initialization  //GEN-END:initComponents


    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner Evaluation license 
    private JFrame Add_a;
    private JLabel label3;
    private JLabel label4;
    private JLabel label5;
    private JLabel label6;
    private JLabel label7;
    private JLabel label8;
    private JLabel label9;
    private JLabel label10;
    private JTextField textField1;
    private JTextField textField2;
    private JTextField textField3;
    private JTextField textField4;
    private JTextField textField5;
    private JTextField textField6;
    private JButton button3;
    private JLabel label1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}

EDIT: The very strange thing is that Add_a is not Resizable and the frame create by the program is Resizable! 编辑:非常奇怪的是,Add_a不可调整大小,该程序创建的框架是可调整大小的!

Thanks for all the help! 感谢您的所有帮助!

You've got a lot going on here. 这里有很多事情要做。 The best advice I can give you is to ditch the GUI builder. 我能给您的最好建议是放弃GUI生成器。 You won't learn how to program while using one. 您将不会在使用时学习如何编程。

Your Add_a class extends JFrame. 您的Add_a类扩展了JFrame。 Why? 为什么?

Your Add_a class also contains a JFrame variable named Add_a, which doesn't make a whole lot of sense. 您的Add_a类还包含一个名为Add_a的JFrame变量,这没有什么意义。 Naming a variable the same name as a class is a recipe for disaster and confusion. 将变量命名为与类相同的变量会导致灾难和混乱。

You then set the visibility of the Add_a JFrame variable instead of the Add_a extended JFrame. 然后,您可以设置Add_a JFrame变量而不是Add_a扩展JFrame的可见性。 Not sure what that's supposed to do. 不知道该怎么办。

The answer to this question is to A) stop using a GUI builder and B) stop extending JFrame so you always know which JFrame you're setting to visible. 这个问题的答案是A)停止使用GUI构建器,B)停止扩展JFrame,这样您就始终知道要设置为可见的JFrame。

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

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