gpt4 book ai didi

java - 从另一个 Jframe 打开一个 Jframe,

转载 作者:行者123 更新时间:2023-12-01 12:39:07 28 4
gpt4 key购买 nike

我有一个奇怪的问题。我想创建一个带有多个 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不可调整大小,而程序创建的框架是可调整大小的!

感谢您的帮助!

最佳答案

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

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

您的 Add_a 类还包含一个名为 Add_a 的 JFrame 变量,这没有多大意义。将变量命名为与类相同的名称会导致灾难和困惑。

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

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

关于java - 从另一个 Jframe 打开一个 Jframe,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25285604/

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