如何使用'ADD'按钮重复添加 JPanel?如何在ActionListener()中调用JPanel?



我想通过使用"添加"按钮重复"工作历史"JPanel(内容为灰色(,并使用"删除"按钮将其删除。这是图形用户界面 我还想知道如何临时存储文本字段的值。数组会有帮助吗?

这是代码

JLabel lblEmploymentHistory = new JLabel("Employment History:");
lblEmploymentHistory.setBounds(46, 145, 128, 14);
frame.getContentPane().add(lblEmploymentHistory);
JPanel panelemp = new JPanel();
panelemp.setBounds(46, 170, 435, 63);
panelemp.setBackground(Color.GRAY);
frame.getContentPane().add(panelemp);
JLabel lblRoleHeld = new JLabel("Role Held:");
panelemp.add(lblRoleHeld);
txtRole = new JTextField();
txtRole.setText("role");
panelemp.add(txtRole);
txtRole.setColumns(10);
JLabel lblDuration = new JLabel("Duration:");
panelemp.add(lblDuration);
txtDuration = new JTextField();
txtDuration.setText("duration");
panelemp.add(txtDuration);
txtDuration.setColumns(10);
JLabel lblEmployer = new JLabel("Employer:");
panelemp.add(lblEmployer);
txtEmployer = new JTextField();
txtEmployer.setText("employer");
panelemp.add(txtEmployer);
txtEmployer.setColumns(10);
JButton Addnewemphis = new JButton("Add");
Addnewemphis.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
panelemp.add(Addnewemphis);
JButton btnRemove = new JButton("Remove");
btnRemove.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
panelemp.add(btnRemove);

先退后一步。你需要学习和理解一个关键的概念——责任分离。

您需要将功能分离并隔离到各个类中,这样可以更轻松地快速简单地开发重复的功能。

您还应该专注于将"数据"的管理与"用户界面"分开,以便用户界面是数据的表示形式,并用于(如果适用(更新数据。

根据您当前的设计,无法"选择"员工历史记录面板,因此具有"通用"删除操作毫无意义 - 您将删除哪一个? 相反,该操作实际上属于员工历史记录面板本身,但添加和删除这些组件的责任完全由单独的控制器承担。

让我们从一个基本概念开始,您首先需要一些数据...

public class EmployeeHistory {
private String role;
private String duration;
private String employer;
public EmployeeHistory() {
}
public EmployeeHistory(String role, String duration, String employer) {
this.role = role;
this.duration = duration;
this.employer = employer;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getDuration() {
return duration;
}
public void setDuration(String duration) {
this.duration = duration;
}
public String getEmployer() {
return employer;
}
public void setEmployer(String employer) {
this.employer = employer;
}
}

就个人而言,我更喜欢interface,可能是"只读"和"读写"访问,但为了简洁起见,我们将坚持使用。

接下来,我们需要一些方法来显示它...

public class HistoryPane extends JPanel {
private final JTextField txtRole;
private final JTextField txtDuration;
private final JTextField txtEmployer;
private final JButton removeButton;
private EmployeeHistory history;
public HistoryPane(EmployeeHistory history) {
// This is what you should use when you want to populate
// the view or properties of the UI are changed
this.history = history;
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
JLabel lblRoleHeld = new JLabel("Role Held:");
add(lblRoleHeld, gbc);
gbc.gridx++;
txtRole = new JTextField();
txtRole.setText("role");
add(txtRole, gbc);
txtRole.setColumns(10);
gbc.gridx = 0;
gbc.gridy++;
JLabel lblDuration = new JLabel("Duration:");
add(lblDuration, gbc);
gbc.gridx++;
txtDuration = new JTextField();
txtDuration.setText("duration");
add(txtDuration, gbc);
txtDuration.setColumns(10);
gbc.gridx = 0;
gbc.gridy++;
JLabel lblEmployer = new JLabel("Employer:");
add(lblEmployer, gbc);
gbc.gridx++;
txtEmployer = new JTextField();
txtEmployer.setText("employer");
add(txtEmployer, gbc);
txtEmployer.setColumns(10);
gbc.gridx = 0;
gbc.gridy++;
gbc.gridwidth = GridBagConstraints.REMAINDER;
removeButton = new JButton("Remove");
add(removeButton, gbc);
}
public EmployeeHistory getHistory() {
return history;
}
public void addActionListener(ActionListener listener) {
removeButton.addActionListener(listener);
}
public void removeActionListener(ActionListener listener) {
removeButton.removeActionListener(listener);
}
}

nb:我没有费心用数据填写视图,我相信你可以把它弄出来

这里要注意的是,removeButton实际上并没有做任何事情,责任被委托给另一方

好的,我们可以删除,但是我们如何添加? 好吧,您需要另一个组件...

public class ActionPane extends JPanel {
private JButton btn;
public ActionPane() {
setLayout(new GridBagLayout());
btn = new JButton("Add");
add(btn);
}
public void addActionListener(ActionListener listener) {
btn.addActionListener(listener);
}
public void removeActionListener(ActionListener listener) {
btn.removeActionListener(listener);
}
}

同样,这实际上并没有做任何事情,它只是将责任委托给其他人。

注意:这是一个基本的例子,同样,你可以传入某种控制器,它将充当委托,但结果基本相同。

好的,好的,但是这一切是如何工作的呢? 好吧,您只需要将所有功能放在一起即可...

因此,这是添加和删除功能的可能实现

ActionPane actionPane = new ActionPane();
actionPane.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Layout constraints
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
// The actually history data
EmployeeHistory history = new EmployeeHistory();
// This is a model to manage the individual histories, making
// it easier to manage
histories.add(history);
// The history view...
HistoryPane pane = new HistoryPane(history);
// The remove action...
pane.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Remove the action to improve the chances of been
// garbage collected
pane.removeActionListener(this);
// Remove the history from our model
histories.remove(pane.getHistory());
// Remove the view
contentPane.remove(pane);
contentPane.revalidate();
contentPane.repaint();
}
});
// Add the view (this is a little trick ;))
contentPane.add(pane, gbc, contentPane.getComponentCount() - 1);
contentPane.revalidate();
contentPane.repaint();
}
});

可运行示例

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JPanel contentPane;
private List<EmployeeHistory> histories;
public TestPane() {
histories = new ArrayList<>(25);
setLayout(new BorderLayout());
contentPane = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weighty = 1;
contentPane.add(new JPanel(), gbc);
JScrollPane scrollPane = new JScrollPane(contentPane);
add(scrollPane);
ActionPane actionPane = new ActionPane();
actionPane.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
EmployeeHistory history = new EmployeeHistory();
histories.add(history);
HistoryPane pane = new HistoryPane(history);
pane.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
pane.removeActionListener(this);
histories.remove(pane.getHistory());
contentPane.remove(pane);
contentPane.revalidate();
contentPane.repaint();
}
});
contentPane.add(pane, gbc, contentPane.getComponentCount() - 1);
contentPane.revalidate();
contentPane.repaint();
}
});
add(actionPane, BorderLayout.SOUTH);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
}
public class ActionPane extends JPanel {
private JButton btn;
public ActionPane() {
setLayout(new GridBagLayout());
btn = new JButton("Add");
add(btn);
}
public void addActionListener(ActionListener listener) {
btn.addActionListener(listener);
}
public void removeActionListener(ActionListener listener) {
btn.removeActionListener(listener);
}
}
public class EmployeeHistory {
private String role;
private String duration;
private String employer;
public EmployeeHistory() {
}
public EmployeeHistory(String role, String duration, String employer) {
this.role = role;
this.duration = duration;
this.employer = employer;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getDuration() {
return duration;
}
public void setDuration(String duration) {
this.duration = duration;
}
public String getEmployer() {
return employer;
}
public void setEmployer(String employer) {
this.employer = employer;
}
}
public class HistoryPane extends JPanel {
private final JTextField txtRole;
private final JTextField txtDuration;
private final JTextField txtEmployer;
private final JButton removeButton;
private EmployeeHistory history;
public HistoryPane(EmployeeHistory history) {
// This is what you should use when you want to populate
// the view or properties of the UI are changed
this.history = history;
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
JLabel lblRoleHeld = new JLabel("Role Held:");
add(lblRoleHeld, gbc);
gbc.gridx++;
txtRole = new JTextField();
txtRole.setText("role");
add(txtRole, gbc);
txtRole.setColumns(10);
gbc.gridx = 0;
gbc.gridy++;
JLabel lblDuration = new JLabel("Duration:");
add(lblDuration, gbc);
gbc.gridx++;
txtDuration = new JTextField();
txtDuration.setText("duration");
add(txtDuration, gbc);
txtDuration.setColumns(10);
gbc.gridx = 0;
gbc.gridy++;
JLabel lblEmployer = new JLabel("Employer:");
add(lblEmployer, gbc);
gbc.gridx++;
txtEmployer = new JTextField();
txtEmployer.setText("employer");
add(txtEmployer, gbc);
txtEmployer.setColumns(10);
gbc.gridx = 0;
gbc.gridy++;
gbc.gridwidth = GridBagConstraints.REMAINDER;
removeButton = new JButton("Remove");
add(removeButton, gbc);
}
public EmployeeHistory getHistory() {
return history;
}
public void addActionListener(ActionListener listener) {
removeButton.addActionListener(listener);
}
public void removeActionListener(ActionListener listener) {
removeButton.removeActionListener(listener);
}
}
}

现在,说了这么多,去阅读如何使用表格和如何使用列表

数组会有帮助吗?

不,不是真的。 这将限制您可以显示的历史元素的数量。 相反,我会使用ArrayList来管理历史对象的实例,如上所示

最新更新