如何使用 'ADD' 按钮重复添加 JPanel?如何在 ActionListener() 中调用 JPanel?
How to add a JPanel repeatedly using an 'ADD' button? How to call a JPanel in the ActionListener()?
我想使用“添加”按钮重复工作经历 JPanel(内容为灰色),然后使用“删除”按钮将其删除。This is the GUI
我还想知道如何临时存储文本字段的值。阵列会有帮助吗?
这是代码
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);
先退一步。您需要学习和理解一个关键概念 - 职责分离。
您需要将您的功能分离和隔离成单独的 类,这将使开发重复功能变得更加容易、快速和简单。
您还应该专注于将 "data" 的管理与 "user interface" 的管理分开,以便用户界面代表您的数据并用于(在适用的情况下)更新数据.
根据您当前的设计,无法 "select" 员工历史记录面板,因此 "generic" 删除操作毫无意义 - 您会删除哪个?相反,该操作实际上属于员工历史记录面板本身,但添加和删除这些组件的责任完全属于一个单独的控制器。
让我们从一个基本概念开始,您首先需要一些数据...
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
,可能是 "read-only" 和 "read-write" 访问权限,但为了简洁起见,我们将坚持这一点。
接下来,我们需要一些方法来显示它...
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);
}
}
同样,这实际上并没有做任何事情,它只是将责任委托给其他人。
nb: 这是一个基本的例子,同样,你可以传入某种控制器作为委托,但结果基本相同
好吧,好吧,但这一切是如何运作的?好吧,您只需要将所有功能结合在一起...
所以这是添加和删除功能的可能实现
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);
}
}
}
说了那么多,去读一读How to Use Tables and How to Use Lists
Will an array be helpful?
不,不是真的。这将限制您可以显示的历史元素的数量。相反,我会使用 ArrayList
来管理历史对象的实例,如上所示
我想使用“添加”按钮重复工作经历 JPanel(内容为灰色),然后使用“删除”按钮将其删除。This is the GUI 我还想知道如何临时存储文本字段的值。阵列会有帮助吗?
这是代码
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);
先退一步。您需要学习和理解一个关键概念 - 职责分离。
您需要将您的功能分离和隔离成单独的 类,这将使开发重复功能变得更加容易、快速和简单。
您还应该专注于将 "data" 的管理与 "user interface" 的管理分开,以便用户界面代表您的数据并用于(在适用的情况下)更新数据.
根据您当前的设计,无法 "select" 员工历史记录面板,因此 "generic" 删除操作毫无意义 - 您会删除哪个?相反,该操作实际上属于员工历史记录面板本身,但添加和删除这些组件的责任完全属于一个单独的控制器。
让我们从一个基本概念开始,您首先需要一些数据...
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
,可能是 "read-only" 和 "read-write" 访问权限,但为了简洁起见,我们将坚持这一点。
接下来,我们需要一些方法来显示它...
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);
}
}
同样,这实际上并没有做任何事情,它只是将责任委托给其他人。
nb: 这是一个基本的例子,同样,你可以传入某种控制器作为委托,但结果基本相同
好吧,好吧,但这一切是如何运作的?好吧,您只需要将所有功能结合在一起...
所以这是添加和删除功能的可能实现
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);
}
}
}
说了那么多,去读一读How to Use Tables and How to Use Lists
Will an array be helpful?
不,不是真的。这将限制您可以显示的历史元素的数量。相反,我会使用 ArrayList
来管理历史对象的实例,如上所示