さまざまなクラスを使用してプロジェクトをコーディングしていますが、メインメニューを呼び出すボタンを押すと行き詰まりました。再開したコードは次のとおりです。
私の主なクラス:
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
Dimension res = Toolkit.getDefaultToolkit().getScreenSize();
public void run(){
Principal frame = new Principal("Program");
frame.setSize(res.width,res.height);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
frame.setVisible(true);
}
});`
さて、私のプリンシパル:
public class Principal extends JFrame implements ActionListener {
private DetallesPrincipal DetallesPrincipal;
private ClientesNuevo ClientesNuevo;
private ClientesModificar ClientesModificar;
Container p;
private static final long serialVersionUID = 1L;
final JMenuItem Clientes1,Clientes2,Clientes3;
final JMenuBar MenuBarMain = new JMenuBar();
public Principal (String titulo){
super(titulo);
setLayout(new BorderLayout());
final JMenuBar MenuBarMain = new JMenuBar();
MenuBarMain.setBackground(new Color(177,178,182));
final JMenu MenuMainClientes;
MenuMainClientes = new JMenu("Clientes");
DetallesPrincipal = new DetallesPrincipal();
ClientesNuevo = new ClientesNuevo();
p = getContentPane();
MenuBarMain.add(MenuMainClientes);
Clientes1 = new JMenuItem("Nueva ficha");
MenuMainClientes.add(Clientes1);
Clientes1.addActionListener(this);
p.add(MenuBarMain, BorderLayout.PAGE_START);
p.add(DetallesPrincipal, BorderLayout.CENTER);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource()==Clientes1){
p.removeAll();
p.repaint();
p.add(ClientesNuevo, BorderLayout.CENTER);
p.revalidate();
p.repaint();
}
私のクライアントヌエボ:
public class ClientesNuevo extends JPanel implements ActionListener {
private static final long serialVersionUID = 1L;
DetallesPrincipal m;
GridBagConstraints gc = new GridBagConstraints();
public ClientesNuevo() {
Dimension size = getPreferredSize();
size.width = 250;
setPreferredSize(size);
setBackground(new Color(111,114,123));
TitledBorder Principal2 = BorderFactory.createTitledBorder("Te encuentras en clientes");
Principal2.setTitleColor(new Color(100,100,100));
Principal2.setTitleFont(new Font("Arial",0,24));
Principal2.setTitlePosition(TitledBorder.BOTTOM);
Principal2.setTitleJustification(TitledBorder.LEFT);
setBorder(Principal2);
JLabel Titulo1 = new JLabel("Nueva ficha cliente");
Titulo1.setFont(new Font("Tahoma",0,36));
JLabel Nombre = new JLabel("Nombre/Razón social ");
JLabel Apellidos = new JLabel("Apellidos ");
JLabel Cif = new JLabel("CIF/DNI ");
JLabel Poblacion = new JLabel("Población ");
JLabel Provincia = new JLabel("Provincia");
JLabel Cpostal = new JLabel("Código postal");
JLabel Telefono = new JLabel("Teléfono");
JLabel Movil = new JLabel("Móvil");
JLabel Notas = new JLabel("Notas");
JTextField NombreText = new JTextField(25);
JTextField ApellidosText = new JTextField(25);
JTextField CifText = new JTextField(15);
JTextField PoblacionText = new JTextField(15);
JTextField ProvinciaText = new JTextField(20);
JTextField CpostalText = new JTextField(15);
JTextField TelefonoText = new JTextField(20);
JTextField MovilText = new JTextField(20);
JTextField NotasText = new JTextField(30);
JButton Aceptar = new JButton("Aceptar");
JButton Cancelar = new JButton("Cancelar");
Aceptar.addActionListener(this);
setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
gc.gridx = 0;
gc.gridy = 0;
gc.weighty = 1;
add(Titulo1, gc);
gc.anchor = GridBagConstraints.LINE_START;
gc.gridx = 0;
gc.gridy = 1;
add(Nombre, gc);
gc.gridx = 1;
gc.gridy = 1;
add(NombreText, gc);
gc.gridx = 0;
gc.gridy = 2;
add(Apellidos, gc);
gc.anchor = GridBagConstraints.LINE_START;
gc.gridx = 1;
gc.gridy = 2;
add(ApellidosText, gc);
gc.anchor = GridBagConstraints.LINE_START;
gc.weightx = 1;
gc.weighty = 1;
gc.gridx = 0;
gc.gridy = 3;
add(Cif, gc);
gc.anchor = GridBagConstraints.LINE_START;
gc.gridx = 1;
gc.gridy = 3;
add(CifText, gc);
gc.gridx = 0;
gc.gridy = 4;
add(Poblacion, gc);
gc.gridx = 1;
gc.gridy = 4;
add(PoblacionText, gc);
gc.gridx = 2;
gc.gridy = 4;
add(Provincia, gc);
gc.gridx = 2;
gc.gridy = 7;
add(Aceptar, gc);
gc.gridx = 3;
gc.gridy = 7;
add(Cancelar, gc);
}
public void actionPerformed(ActionEvent e) {
removeAll();
repaint();
m = new DetallesPrincipal();
add(m,gc);
revalidate();
repaint();
}
}
そのウォールテキストで申し訳ありませんが、私はかなり迷っています.DetallesPrincipalクラスのコンポーネントを呼び出したいだけです.このようなプログラムを管理するためのより良いオプションは何ですか?