我有一份就业历史GUI。如果用户想要添加更多历史记录,他们会单击添加按钮并在当前窗格下方,它将添加另一个用户表单,用户可以在其中输入更多信息。我不知道该怎么做。此外,我有一个返回上一帧的按钮,但我希望用户能够看到他们在前一帧中输入的内容,而不是创建一个新的帧。任何帮助将不胜感激。到目前为止,这是我的代码:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class EmploymentHistory extends JFrame {
private JButton btnClear, btnNext, btnPrevious, btnAddMoreEmployment;
private JLabel labelEmployer, labelPosition, labelStart, labelEnd, labelDuties,
labelReason, labelPhoneNum;
private JTextField txtEmployer, txtPosition, txtStart, txtEnd, txtPhoneNum;
private JTextArea areaDuties, areaReason;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new EmploymentHistory().setVisible(true);
}
});
}
public EmploymentHistory() {
createView();
setTitle("Employment History");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(600, 500);
setMinimumSize(new Dimension(600, 500));
setLocationRelativeTo(null);
setResizable(false);
}
private void createView() {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
getContentPane().add(panel);
// add scroll pane to move down
northPanel(panel);
centerPanel(panel);
southPanel(panel);
}
private void northPanel(JPanel panel) {
JPanel northPanel = new JPanel();
northPanel.setBackground(Color.GRAY);
panel.add(northPanel, BorderLayout.NORTH);
JLabel labelEmployment = new JLabel("Employment Information");
labelEmployment.setFont(new Font("Serif", Font.BOLD, 25));
northPanel.add(labelEmployment, BorderLayout.CENTER);
}
private void centerPanel(JPanel panel) {
JPanel centerPanel = new JPanel();
centerPanel.setBackground(Color.yellow);
centerPanel.setSize(300, 300);
GridBagLayout layout = new GridBagLayout();
centerPanel.setLayout(layout);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.LINE_END;
labelEmployer = new JLabel("Employer: ");
labelEmployer.setFont(new Font("Serif", Font.PLAIN, 15));
centerPanel.add(labelEmployer, gbc);
gbc.gridy++;
labelPosition = new JLabel("Position: ");
labelPosition.setFont(new Font("Serif", Font.PLAIN, 15));
centerPanel.add(labelPosition, gbc);
gbc.gridy++;
labelPhoneNum = new JLabel("Employer's Phone Number: ");
labelPhoneNum.setFont(new Font("Serif", Font.PLAIN, 15));
centerPanel.add(labelPhoneNum, gbc);
gbc.gridy++;
labelStart = new JLabel("Start Date: ");
labelStart.setFont(new Font("Serif", Font.PLAIN, 15));
centerPanel.add(labelStart, gbc);
gbc.gridy++;
labelEnd = new JLabel("End Date: ");
labelEnd.setFont(new Font("Serif", Font.PLAIN, 15));
centerPanel.add(labelEnd, gbc);
gbc.gridy++;
labelDuties = new JLabel("Duties: ");
labelDuties.setFont(new Font("Serif", Font.PLAIN, 15));
centerPanel.add(labelDuties, gbc);
gbc.gridy++;
labelReason = new JLabel("Reason for leaving: ");
labelReason.setFont(new Font("Serif", Font.PLAIN, 15));
centerPanel.add(labelReason, gbc);
gbc.gridy++;
//reseting
gbc.gridx = 1;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.LINE_START;
txtEmployer = new JTextField(25);
txtEmployer.setHorizontalAlignment(JTextField.CENTER);
txtEmployer.setHorizontalAlignment(SwingConstants.LEFT);
centerPanel.add(txtEmployer, gbc);
gbc.gridy++;
txtPosition = new JTextField(25);
txtPosition.setHorizontalAlignment(JTextField.CENTER);
txtPosition.setHorizontalAlignment(SwingConstants.LEFT);
centerPanel.add(txtPosition, gbc);
gbc.gridy++;
txtPhoneNum = new JTextField(25);
txtPhoneNum.setHorizontalAlignment(JTextField.CENTER);
txtPhoneNum.setHorizontalAlignment(SwingConstants.LEFT);
centerPanel.add(txtPhoneNum, gbc);
gbc.gridy++;
txtStart = new JTextField(25);
txtStart.setHorizontalAlignment(JTextField.CENTER);
txtStart.setHorizontalAlignment(SwingConstants.LEFT);
centerPanel.add(txtStart, gbc);
gbc.gridy++;
txtEnd = new JTextField(25);
txtEnd.setHorizontalAlignment(JTextField.CENTER);
txtEnd.setHorizontalAlignment(SwingConstants.LEFT);
centerPanel.add(txtEnd, gbc);
gbc.gridy++;
areaDuties = new JTextArea();
areaDuties.setLineWrap(true);
areaDuties.setWrapStyleWord(true);
JScrollPane scrollPane = new JScrollPane(areaDuties);
scrollPane.setPreferredSize(new Dimension(315, 90));
centerPanel.add(scrollPane, gbc);
gbc.gridy++;
areaReason = new JTextArea();
areaReason.setLineWrap(true);
areaReason.setWrapStyleWord(true);
JScrollPane scrollPaneReason = new JScrollPane(areaReason);
scrollPaneReason.setPreferredSize(new Dimension(315, 90));
centerPanel.add(scrollPaneReason, gbc);
gbc.gridy++;
btnAddMoreEmployment = new JButton("Add More");
btnAddMoreEmployment.addActionListener(new ButtonAddMoreActionListener());
centerPanel.add(btnAddMoreEmployment, gbc);
gbc.gridy++;
panel.add(centerPanel);
}
private void southPanel(JPanel panel) {
JPanel southPanel = new JPanel();
southPanel.setBackground(Color.GREEN);
panel.add(southPanel, BorderLayout.SOUTH);
btnPrevious = new JButton("PREVIOUS");
btnPrevious.addActionListener(new ButtonPreviousActionLisener());
southPanel.add(btnPrevious);
btnClear = new JButton("CLEAR");
southPanel.add(btnClear);
btnNext = new JButton("NEXT");
southPanel.add(btnNext);
}
private class ButtonAddMoreActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
getContentPane().add(panel);
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setPreferredSize(new Dimension(315, 90));
panel.add(scrollPane);
centerPanel(panel);
}
}
private class ButtonPreviousActionLisener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
BasicInfo goBasic = new BasicInfo();
goBasic.setVisible(true);
}
}
}