从另一个Jframe打开一个Jframe,

时间:2014-08-13 12:00:42

标签: java swing jframe multiple-instances

我有一个奇怪的问题。 我想用多Jframe创建一个gui,当我按下按钮时调用。 我之前使用WindowsBuilder PRO创建了Jframe,但是非常难以使用,因为它做了他想要的而不是我需要的东西! (例如,如果我调整对象的大小,WBP 带来另一个地方) 所以我开始使用JformDesigner,问题是: 我创建了2个Jframe,一个是调用Jform_GUI,另一个是Add_a。 我的程序基本上从Jform_GUI开始,按一个按钮调用Add_a,但如果我使用

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

它不起作用,我得到的是一个没有大小且全部空白的小Jframe。 我尝试了不同的解决方案,但它不起作用。这是我的代码

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

}

我想要调用的第二个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
}
编辑:非常奇怪的是,Add_a不是Resizable,程序创建的框架是Resizable!

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

你在这里有很多事情要做。我能给你的最好建议是放弃GUI构建器。你不会在使用它时学习如何编程。

您的Add_a类扩展了JFrame。为什么呢?

你的Add_a类还包含一个名为Add_a的JFrame变量,它没有多大意义。命名与类同名的变量是一种灾难和混乱的方法。

然后,您可以设置Add_a JFrame变量的可见性,而不是Add_a扩展JFrame。不确定应该做什么。

这个问题的答案是:A)停止使用GUI构建器,B)停止扩展JFrame,以便始终知道您设置的可见的JFrame。