Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

274
Vistas
El método repaint () no pinta un nuevo marco

He mirado muchas respuestas pero todavía no puedo encontrar una solución. Tengo un JFrame y dos JPanels. Quiero eliminar un panel y reemplazarlo con el segundo cuando se presiona un botón, pero el método repintar () no actualiza el marco. Por favor ayuda.

Aquí está mi código para el marco:

 import javax.swing.*; import java.awt.*; import static javax.swing.JFrame.EXIT_ON_CLOSE; public class MainFrame { static JFrame mainFrame; int height = 650; int width = 1042; public MainFrame() { mainFrame = new JFrame(); mainFrame.setBounds(0, 0, width, height); mainFrame.setDefaultCloseOperation(EXIT_ON_CLOSE); mainFrame.setResizable(false); } public static void main(String[] args) { new MainMenu(); mainFrame.setVisible(true); } }

Este es el código de mi panel MainMenu

 import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import static java.awt.Color.CYAN; import static java.awt.Color.red; public class MainMenu extends MainFrame { public MainMenu() { components(); } //variable decleration JPanel menuPanel; JLabel title; JButton periodicTable; private void components() { int buttonW = 500; int buttonH = 50; //creating panel menuPanel = new JPanel(); menuPanel.setLayout(null); menuPanel.setBackground(CYAN); //creating title label title = new JLabel("Application Title", SwingConstants.CENTER); title.setFont(new Font("Calibri Body", 0, 50)); title.setBounds(width / 3 - buttonW / 2, 50, buttonW, buttonH + 10); //creating periodic table button periodicTable = new JButton(); periodicTable.setText("Periodic Table"); periodicTable.setBounds(width / 3 - buttonW / 2, 50 + buttonH + 60, buttonW, buttonH); periodicTable.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { periodicTableActionPerformed(event); } }); //adding components to panel menuPanel.add(title); menuPanel.add(periodicTable); //adding panel to MainFrame mainFrame.add(menuPanel); } private void periodicTableActionPerformed(ActionEvent event) { mainFrame.remove(menuPanel); mainFrame.repaint(); new PeriodicTable(); mainFrame.repaint(); } }

Y finalmente mi panel PeriodicTable

 import javax.swing.*; import java.awt.*; public class PeriodicTable extends MainFrame { public PeriodicTable() { periodicComponents(); } JPanel ptPanel; private void periodicComponents() { ptPanel = new JPanel(); ptPanel.setLayout(null); ptPanel.setBackground(Color.RED); mainFrame.add(ptPanel); } }
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

No tengo idea de por qué está extendiendo MainFrame. Me parece innecesario.

Quiero quitar un panel y reemplazarlo con el segundo cuando se presiona un botón

Luego usa un CardLayout . Lea la sección del tutorial de Swing sobreCómo usar CardLayout para ver un ejemplo de trabajo.

El tutorial le mostrará cómo estructurar mejor su código.

over 4 years ago · Santiago Trujillo Denunciar

0

Tu tabla PeriodicTable amplía MainFrame . Al crear una new PeriodicTable , crea con ella un nuevo MainFrame que tiene su propia instancia de JFrame (MainFrame.mainFrame). Debe agregar ese panel al marco principal existente en MainMenu

Sugiero eliminar el cambio de su clase de PeriodicTable de esta manera:

 import javax.swing.*; import java.awt.*; public class PeriodicTable extends JPanel // Not MainFrame, but new custom panel { public PeriodicTable() { periodicComponents(); } private void periodicComponents() { // You don't need ptPanel anymore, because `this` is JPanel setLayout(null); setBackground(Color.RED); } }

y cambie su función actionPerformed a algo como esto:

 private void periodicTableActionPerformed(ActionEvent event) { mainFrame.remove(menuPanel); // Remove old panel mainFrame.add(new PeriodicTable()); // Create and add to existing mainFrame mainFrame.repaint(); // Just one repaint at the end // I think it will work even without repaint, because add and remove should schedule repainting as well }
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda