Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

270
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda