gpt4 book ai didi

java - 从一个类获取文本字段信息并将其显示在另一类的 JTextArea 上

转载 作者:行者123 更新时间:2023-12-01 09:27:00 27 4
gpt4 key购买 nike

我有一个类BasicInfoWindow,它获取用户信息,例如姓名、地址等。然后我有另一个类ReviewandSubmit,它显示用户从JTextArea 中的BasicInfoWindow 输入的文本。我正在使用卡片布局在每个面板之间切换。我不知道如何从 BasicInfoWindow 发送信息并从 ReviewandSubmit 接收信息。这是到目前为止我的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main extends JPanel
{
private static void createAndShowGUI()
{
final Main test = new Main();

JPanel buttonPanel = new JPanel(new BorderLayout());

JPanel southPanel = new JPanel();
buttonPanel.add(southPanel, BorderLayout.SOUTH);

final JButton btnNext = new JButton("NEXT");
final JButton btnPrev = new JButton("PREVIOUS");

buttonPanel.add(btnNext, BorderLayout.EAST);
buttonPanel.add(btnPrev, BorderLayout.WEST);

btnNext.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
test.nextCard();
}

});

btnPrev.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
test.prevCard();
}

});

JFrame frame = new JFrame("Employment Application");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(test);
frame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
//frame.setSize(750,500);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
}

public static void main(String [] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
createAndShowGUI();
}
});
}

private CardLayout cardLayout = new CardLayout();
private JPanel cardShowingPanel = new JPanel(cardLayout);

public Main()
{
BasicInfoWindow window1 = new BasicInfoWindow();
cardShowingPanel.add(window1, "1");

EmploymentHistoryWindow window2 = new EmploymentHistoryWindow();
cardShowingPanel.add(window2, "2");

EducationAndAvailbleWindow window3 = new EducationAndAvailbleWindow();
cardShowingPanel.add(window3, "3");

ReviewAndSubmitWindow window4 = new ReviewAndSubmitWindow();
cardShowingPanel.add(window4, "4");


setLayout(new BorderLayout());

add(cardShowingPanel, BorderLayout.CENTER);
}

public void nextCard()
{
cardLayout.next(cardShowingPanel);
}

public void prevCard()
{
cardLayout.previous(cardShowingPanel);
}

public void showCard(String key)
{
cardLayout.show(cardShowingPanel, key);
}


}

BasicInfo类省略了一些方法

public class BasicInfoWindow extends JPanel
{
private JTextField txtName, txtAddress, txtCity, txtState, txtZipCode, txtPhoneNumber, txtEmail;
private JComboBox cbDate, cbYear, cbMonth;
private JLabel labelName, labelAddress, labelCity, labelState, labelZipCode, labelPhoneNumber, labelEmail, labelDOB;
private JButton btnClear;

public BasicInfoWindow()
{
createView();
}

private void createView()
{
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
add(panel);

northPanel(panel);

centerPanel(panel);

southPanel(panel);
}

public ArrayList<HiringPersonInfo> sendInfo()
{
String name = txtName.getText();
String address = txtAddress.getText();
String city = txtCity.getText();
String state = txtState.getText();
String zip = txtZipCode.getText();
String phone = txtPhoneNumber.getText();
String email = txtEmail.getText();
String DOB = cbMonth.getSelectedItem() + " " + cbDate.getSelectedItem() + " " + cbYear.getSelectedItem();

HiringPersonInfo addNewInfo = new HiringPersonInfo(name, address, city, state, zip, phone, email, DOB);

ArrayList<HiringPersonInfo> personInfo = new ArrayList();
personInfo.add(addNewInfo);

return personInfo;

}

ReviewAndSubmit 类

public class ReviewAndSubmitWindow extends JPanel
{
private JButton btnSubmit;

public ReviewAndSubmitWindow()
{
createView();
}

private void createView()
{
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
add(panel);

northPanel(panel);
centerPanel(panel);
southPanel(panel);
}

private void northPanel(JPanel panel)
{
JPanel northPanel = new JPanel();
panel.add(northPanel, BorderLayout.NORTH);

JLabel labelMessage = new JLabel("Review and Submit");
labelMessage.setFont(new Font("Serif", Font.BOLD, 25));
northPanel.add(labelMessage, BorderLayout.CENTER);
}

private void centerPanel(JPanel panel)
{
JPanel centerPanel = new JPanel();

JTextArea showReview = new JTextArea();
showReview.setLineWrap(true);
showReview.setWrapStyleWord(true);
showReview.setEditable(false);
JScrollPane scrollPane = new JScrollPane(showReview);
scrollPane.setPreferredSize(new Dimension(600, 385));
centerPanel.add(scrollPane, BorderLayout.CENTER);

BasicInfoWindow getInfo = new BasicInfoWindow();
showReview.append(getInfo.sendInfo().toString());

panel.add(centerPanel);
}

private void southPanel(JPanel panel)
{
JPanel southPanel = new JPanel();
panel.add(southPanel, BorderLayout.SOUTH);

btnSubmit = new JButton("SUBMIT");
// creates a new file
southPanel.add(btnSubmit);
}
}

最佳答案

您可以在 HiringPersonInfo 类中使用单例模式来实例化该类的一个实例,然后在提交过程中您可以将该类的实例添加到 ArrayList 中。

单例模式可用于创建可由该包中的所有类共享的对象实例。您可以将其视为面向 OOP 的“全局”变量。

关于java - 从一个类获取文本字段信息并将其显示在另一类的 JTextArea 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39754620/

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