gpt4 book ai didi

java - 从 Java 类调用对象以在单独的表单上显示的代码不起作用,为什么?

转载 作者:行者123 更新时间:2023-12-01 22:14:55 24 4
gpt4 key购买 nike

我在 NetBeans 中创建了一个空白的 Swing GUI。然后,我创建了一个单独的 Java 类 (empClass),其中包含我想要在 Swing 表单(员工)上显示的所有对象(按钮、文本框、标签等)。表单的设计将有一个面板,其中包含一系列标签和文本框,用于运算符(operator)工信息。面板外部有两个按钮,一个位于表单顶部作为标题的标签,以及一个 JList。我已经创建了类中的所有对象,并且根据在线研究,将类中的对象调用到 Swing 窗体上的所有必要编码都是正确的,但是当我运行应用程序时,窗体保持空白,并且不显示在其他类。我怎样才能让它发挥作用?

下面是 Swing Form(员工)中调用 Java 类的代码:

public class employees extends javax.swing.JFrame {


public employees() {
initComponents();

this.setPreferredSize(new Dimension(720,520));

this.add(ec.getpanel());


}

empClass ec = new empClass();

这是java类(empClass)的完整编码:

package SV_Operations;

import java.awt.*;
import javax.swing.*;

public class empClass
{
public JButton bfunction = new javax.swing.JButton("Add");
public JButton cancel = new javax.swing.JButton("Cancel");
public JList empList = new javax.swing.JList();
public JLabel picon = new javax.swing.JLabel();
public ImageIcon icon = new ImageIcon(empClass.class.getResource("/Pictures/Carbon.jpg"));
public JPanel panel = new javax.swing.JPanel();
public JLabel lbltitle = new javax.swing.JLabel("Admin Page");
public JLabel lblemp = new javax.swing.JLabel("Employee ID: ");
public JTextField txtempid = new javax.swing.JTextField();
public JLabel lblidno = new javax.swing.JLabel("ID Number:");
public JTextField txtidno = new javax.swing.JTextField();
public JLabel lblname = new javax.swing.JLabel("Name:");
public JTextField txtname = new javax.swing.JTextField();
public JLabel lblsurname = new javax.swing.JLabel("Surname: ");
public JTextField txtsurname = new javax.swing.JTextField();
public JLabel lblcell = new javax.swing.JLabel("Cellphone no: ");
public JTextField txtcell = new javax.swing.JTextField();
public JLabel lblalt = new javax.swing.JLabel("Alternative no: ");
public JTextField txtalt = new javax.swing.JTextField();
public JLabel lblemail = new javax.swing.JLabel("Emial: ");
public JTextField txtemail = new javax.swing.JTextField();
public JLabel lbladdress = new javax.swing.JLabel("Address: ");
public JTextField txtaddress = new javax.swing.JTextField();
public JLabel lblpass = new javax.swing.JLabel("Password: ");
public JTextField txtpass = new javax.swing.JTextField();


public empClass()
{

}


public JPanel getpanel()
{
return panel;
}

public void layoutUI()
{

panel.setLayout(null);
panel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
panel.setBounds(230, 80, 450, 310);
panel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
panel.setLayout(null);
panel.setVisible(true);
panel.add(panel);

lbltitle.setFont(new java.awt.Font("Excelerate", 1, 42));
lbltitle.setBounds(60, 0, 620, 70);
lbltitle.setForeground(Color.RED);


lblemp.setFont(new java.awt.Font("Alexis", 1, 18));
lblemp.setBounds(45, 30, 170, 13);
lblemp.setForeground(Color.RED);
lblemp.setVisible(true);
panel.add(lblemp);

txtempid.setBounds(200, 20, 230, 30);
txtempid.setVisible(true);
panel.add(txtempid);

lblidno.setFont(new java.awt.Font("Alexis", 1, 18));
lblidno.setBounds(72, 60, 170, 13);
lblidno.setForeground(Color.RED);
lblidno.setVisible(true);
panel.add(lblidno);

txtidno.setBounds(200, 50, 230, 30);
txtidno.setVisible(true);
panel.add(txtidno);

lblname.setFont(new java.awt.Font("Alexis", 1, 18));
lblname.setBounds(131, 90, 180, 13);
lblname.setForeground(Color.RED);
lblname.setVisible(true);
panel.add(lblname);

txtname.setBounds(200, 80, 230, 30);
txtname.setVisible(true);
panel.add(txtname);

lblsurname.setFont(new java.awt.Font("Alexis", 1, 18));
lblsurname.setBounds(85, 120, 180, 13);
lblsurname.setForeground(Color.RED);
lblsurname.setVisible(true);
panel.add(lblsurname);

txtsurname.setBounds(200, 110, 230,13);
txtsurname.setVisible(true);
panel.add(txtsurname);

lblcell.setFont(new java.awt.Font("Alexis", 1, 18));
lblcell.setBounds(34, 150, 190, 13);
lblcell.setForeground(Color.RED);
lblcell.setVisible(true);
panel.add(lblcell);

txtcell.setBounds(200, 140, 230, 30);
txtcell.setVisible(true);
panel.add(txtcell);

lblalt.setFont(new java.awt.Font("Alexis", 1, 18));
lblalt.setBounds(18, 180, 190, 13);
lblalt.setForeground(Color.RED);
lblalt.setVisible(true);
panel.add(lblalt);

txtalt.setBounds(200, 170, 230, 30);
txtalt.setVisible(true);
panel.add(txtalt);

lblemail.setFont(new java.awt.Font("Alexis", 1, 18));
lblemail.setBounds(129, 210, 190, 13);
lblemail.setForeground(Color.RED);
lblemail.setVisible(true);
panel.add(lblemail);

txtemail.setBounds(200, 200, 230, 30);
txtemail.setVisible(true);
panel.add(txtemail);

lbladdress.setFont(new java.awt.Font("Alexis", 1, 18));
lbladdress.setBounds(92, 240, 190, 13);
lbladdress.setForeground(Color.RED);
lbladdress.setVisible(true);
panel.add(lbladdress);

txtaddress.setBounds(200, 260, 230, 30);
txtaddress.setVisible(true);
panel.add(txtaddress);

lblpass.setFont(new java.awt.Font("Alexis", 1, 18));
lblpass.setBounds(75, 270, 190, 13);
lblpass.setForeground(Color.RED);
lblpass.setVisible(true);
panel.add(lblpass);

txtpass.setBounds(200, 260, 230, 30);
txtpass.setVisible(true);
panel.add(txtpass);

picon.setBounds(0, 0, 450, 310);
picon.setIcon(icon);
picon.setVisible(true);
panel.add(picon);

empList.setBounds(20, 80, 190, 300);
empList.setModel(new javax.swing.AbstractListModel()
{
String[]strings = {"EMP123", "EMP124", "EMP125"};
public int getSize() {return strings.length;}
public Object getElementAt(int i) {return strings [i];}
});
empList.setVisible(true);


bfunction.setBounds(230, 400, 150, 39);
bfunction.setVisible(true);


bfunction.addActionListener(new java.awt.event.ActionListener()
{
@Override
public void actionPerformed(java.awt.event.ActionEvent evt)
{
b1ActionPerformed(evt);
}
});

cancel.setBounds(530, 400, 150, 39);
cancel.setVisible(true);


cancel.addActionListener(new java.awt.event.ActionListener()
{
@Override
public void actionPerformed(java.awt.event.ActionEvent evt)
{
cancelActionPerformed(evt);
}
});


}

private void b1ActionPerformed(java.awt.event.ActionEvent evt)
{
//Left blank for now
}

private void cancelActionPerformed(java.awt.event.ActionEvent evt)
{
System.exit(0);
}
}

最佳答案

这是因为您忘记调用 empClass 类的构造函数中的方法。

所以你的构造函数将是:

public empClass()
{
layoutUI(); // you forgot this line!
}

但是使用 null 布局根本不合适。专业人士不使用空布局。当您使用 null 作为布局管理器时,您将失去的最重要的功能是调整框架大小。请参阅 GridBagLayout 教程,我非常喜欢它,因为我发现它是最可定制的。您还可以了解其他布局管理器,如 GroupLayout、BorderLayout、FlowLayout、GridLayout 等,以及混合它们以创建专业设计的方式。

祝你好运

关于java - 从 Java 类调用对象以在单独的表单上显示的代码不起作用,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31268224/

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