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

275
Visualizações
repaint() method does not paint new frame

I have looked at a lot of answers but i still cannot find a solution. I have a JFrame and two JPanels. I want to remove the one panel and replace it with the second when a button is pressed, but the repaint() method does not refresh the frame. Please help.

Here is my code for the frame:

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);
    }
}

This is the code for my MainMenu panel

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();
    }
}

And finally my PeriodicTable panel

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

I have no idea why you are extending MainFrame. Looks unnecessary to me.

I want to remove the one panel and replace it with the second when a button is pressed

Then use a CardLayout. Read the section from the Swing tutorial on How to Use CardLayout for a working example.

The tutorial will show you how to better structure your code.

over 4 years ago · Santiago Trujillo Relatório

0

Your PeriodicTable extends MainFrame. When creating new PeriodicTable you create with it new MainFrame which has its own instance of JFrame (MainFrame.mainFrame). You need to add that panel to existing mainFrame in MainMenu

I suggest removing changing your PeriodicTable class like this:

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);
    }
}

and change your actionPerformed function to something like this:

    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