我是新手,我想制作一种表格,您可以通过单击左侧的按钮导航到不同的面板,以填写右侧出现的表格(面板),我为此使用GridBagLayout。我试图在单击JButton时删除当前面板,然后添加我通过将JPanel扩展到类而创建的相应面板。例如,如果我单击accountDetailsButton,则希望打开accountDetails面板,有点像切换选项卡。我想用accountDetails面板替换在该位置打开的面板,并且由于可能打开了任何选项卡,因此我需要根据坐标而不是组件名称来交换面板。我不知道该怎么做,因为我想根据框架上的坐标删除和添加组件,并且这些组件的名称可能未知。这是代码。 提前非常感谢!
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FormWindow {
private CustomerProfile customerProfile;
private AccountDetails accountDetails;
private LoanDetails loanDetails;
private DocumentUpload documentUpload;
private JPanel mainPanel;
private JLabel logo;
private JButton customerProfileButton;
private JButton accountDetailsButton;
private JButton loanDetailsButton;
private JButton documentUploadButton;
private JPanel bufferPanel;
public static void main(String[] args) {
JFrame frame = new JFrame("FormWindow");
frame.setContentPane(new FormWindow().mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
FormWindow() {
mainPanel = new JPanel();
bufferPanel = new JPanel();
mainPanel.setLayout(new GridBagLayout());
customerProfile = new CustomerProfile();
accountDetails = new AccountDetails();
loanDetails = new LoanDetails();
documentUpload = new DocumentUpload();
// fieldForm.setLayout(new GridBagLayout());
GridBagConstraints gbc;
gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 0;
gbc.gridheight = 5;
gbc.fill = GridBagConstraints.BOTH;
bufferPanel.add(customerProfile);
mainPanel.add(bufferPanel, gbc);
final GridBagConstraints gbcForms = gbc;
logo = new JLabel();
logo.setText("LOGO");
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(5, 5, 10, 5);
mainPanel.add(logo, gbc);
customerProfileButton = new JButton();
customerProfileButton.setText("Customer Profile");
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(5, 5, 5, 5);
mainPanel.add(customerProfileButton, gbc);
accountDetailsButton = new JButton();
accountDetailsButton.setText("Account Details");
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 2;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(5, 5, 5, 5);
mainPanel.add(accountDetailsButton, gbc);
loanDetailsButton = new JButton();
loanDetailsButton.setText("Loan Details");
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 3;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(5, 5, 5, 5);
mainPanel.add(loanDetailsButton, gbc);
documentUploadButton = new JButton();
documentUploadButton.setText("Document Upload");
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 4;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(5, 5, 5, 5);
mainPanel.add(documentUploadButton, gbc);
//adding action listeners to buttons
customerProfileButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
}
});
accountDetailsButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
}
});
loanDetailsButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
}
});
documentUploadButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
}
});
}
}
当单击相应的按钮时,我希望在框架的右侧显示相应的面板。例如,如果我单击accountDetailsButton,我想用accountDetails面板替换以前可见的面板(accountDetails的代码具有扩展JPanel的accountDetails类)。这样做是为了获得以某种方式在选项卡之间切换的感觉。