gpt4 book ai didi

java - 根据 jcombobox 中的值加载不同的 JPanel

转载 作者:行者123 更新时间:2023-12-01 04:45:54 25 4
gpt4 key购买 nike

我有一个 JFrame,其中包含一个带有 JComboBox cmbTipo(values=Aluno 和 Funcionario) 的 JPanel“painelGeral”,我还有另外 2 个 JPanel:painelAluno 和 painelFuncionario,我希望其中一个显示在“painelGeral”下方取决于我的 JComboBox cmbTipo 中所选的项目。更准确地说,如果我的 JComboBox(cmbTipo) 选择的值是 'Aluno',那么 painelAluno 将显示在 painelGeral 下,如果我的 JComboBox(cmbTipo) 选择的值是 'Funcionario',那么 painelFuncionario 将显示在 painelGeral 下这是我迄今为止尝试执行的代码:

package view;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class MembrosView extends JFrame implements ActionListener {
private JButton btnAdicionar, btnCancelar;
private JLabel lblTipo, lblNome, lblEmail, lblCelular, lblRa, lblCurso,
lblHabilitado, lblCpf, lblEndereco, lblNumero, lblComplemento;
private JTextField txtNome, txtEmail, txtCelular, txtRa, txtCpf,
txtEndereco, txtNumero, txtComplemento;
private JPanel painelPrincipal, painelBotoes, painelGeral, painelAluno,
painelFuncionario;
private String[] tipos = { "Aluno", "Funcionário" };
private JComboBox cmbTipo, cmbCurso;
private JCheckBox ccbHabilitado;

public MembrosView() {
super("Manutenção de Membros");
btnAdicionar = new JButton("Adicionar");
btnCancelar = new JButton("Cancelar");

// geral
lblNome = new JLabel("Nome:");
lblEmail = new JLabel("Email:");
lblCelular = new JLabel("Celular:");
lblTipo = new JLabel("Tipo:");

// aluno
lblRa = new JLabel("RA:");
lblCurso = new JLabel("Curso:");
lblHabilitado = new JLabel("Habilitado:");

// professor
lblCpf = new JLabel("CPF:");
lblEndereco = new JLabel("Endereço");
lblNumero = new JLabel("Número:");
lblComplemento = new JLabel("Complemento:");

// geral
txtNome = new JTextField(20);
txtEmail = new JTextField(20);
txtCelular = new JTextField(20);

// aluno
txtRa = new JTextField(20);
cmbTipo = new JComboBox(tipos);
ccbHabilitado = new JCheckBox();
cmbCurso = new JComboBox();
// habilitado é checkbox

// professor
txtCpf = new JTextField(20);
txtEndereco = new JTextField(20);
txtNumero = new JTextField(20);
txtComplemento = new JTextField(20);

// botoes
btnAdicionar.addActionListener(this);
btnCancelar.addActionListener(this);
cmbTipo.addActionListener(this);

painelPrincipal = new JPanel();
painelAluno = new JPanel();
painelFuncionario = new JPanel();
painelBotoes = new JPanel();
painelGeral = new JPanel();

painelGeral.setLayout(new GridLayout(4, 2));
painelAluno.setLayout(new GridLayout(3, 2));
painelPrincipal.setLayout(new BorderLayout());
painelBotoes.setLayout(new GridLayout(4, 4));

painelGeral.add(lblTipo);
painelGeral.add(cmbTipo);
painelGeral.add(lblNome);
painelGeral.add(txtNome);
painelGeral.add(lblEmail);
painelGeral.add(txtEmail);
painelGeral.add(lblCelular);
painelGeral.add(txtCelular);

painelAluno.add(lblRa);
painelAluno.add(txtRa);
painelAluno.add(lblCurso);
painelAluno.add(cmbCurso);
painelAluno.add(lblHabilitado);
painelAluno.add(ccbHabilitado);

painelFuncionario.add(lblCpf);
painelFuncionario.add(txtCpf);
painelFuncionario.add(lblEndereco);
painelFuncionario.add(txtEndereco);
painelFuncionario.add(lblNumero);
painelFuncionario.add(txtNumero);
painelFuncionario.add(lblComplemento);
painelFuncionario.add(txtComplemento);

painelAluno.setVisible(true);
painelFuncionario.setVisible(true);

painelPrincipal.add(painelGeral, BorderLayout.NORTH);

this.setContentPane(painelPrincipal);
this.setSize(400, 200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
MembrosView mv = new MembrosView();

}

// se o tipo funcionario for ativado, mostra o painelFuncionario logo abaixo
// do painel Geral
// o mesmo pra aluno
// tentativas que eu fiz mas não deram certo
@Override
public void actionPerformed(ActionEvent e) {
if (cmbTipo.getSelectedIndex() == 1) {
painelPrincipal.setSize(400, 600);

painelPrincipal.repaint();
painelAluno.repaint();
painelPrincipal.add(painelAluno, BorderLayout.SOUTH);
}

cmbTipo.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
// int i = (int)
// e.getItemSelectable().getSelectedObjects()[0];
int i = cmbTipo.getSelectedIndex();
if (i == 1) {
painelPrincipal.setSize(400, 600);
painelPrincipal.add(painelAluno, BorderLayout.SOUTH);
painelPrincipal.repaint();
}
}
}
});

}
}

最佳答案

在您的ActionListener中,您正在调用

painelPrincipal.repaint();

在添加新面板painelAluno之前。这应该在添加并且面板已重新验证后发生:

painelPrincipal.add(painelAluno, BorderLayout.SOUTH);
painelPrincipal.revalidate();
painelPrincipal.repaint();

除此之外,当您使用布局管理器时,painelPrincipal.setSize(...) 没有任何效果。上面的代码片段应该足够了。

关于java - 根据 jcombobox 中的值加载不同的 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15846026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com