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

时间:2016-09-28 18:02:05

标签: java user-interface

我有一个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 class

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);
    }   
}

1 个答案:

答案 0 :(得分:1)

您可以在HiringPersonInfo类中使用Singleton Pattern来实例化该类的一个实例,然后在提交期间,您可以将该类的实例添加到ArrayList。

Singleton Patterns可用于创建可由该包中的所有类共享的对象的一个​​实例。您可以将其视为OOP的“全局”变量。