简体   繁体   English

repaint()方法不会绘制新框架

[英]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. 我有一个JFrame和两个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. 我想删除一个面板,并在按下按钮时将其替换为第二个面板,但是repaint()方法不会刷新框架。 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 这是我的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();
    }
}

And finally my PeriodicTable 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);
    }
}

I have no idea why you are extending MainFrame. 我不知道为什么要扩展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 . 然后使用CardLayout Read the section from the Swing tutorial on How to Use CardLayout for a working example. 阅读Swing教程中有关如何使用CardLayout的部分 ,以获取工作示例。

The tutorial will show you how to better structure your code. 本教程将向您展示如何更好地构建代码。

Your PeriodicTable extends MainFrame . 您的PeriodicTable扩展了MainFrame When creating new PeriodicTable you create with it new MainFrame which has its own instance of JFrame (MainFrame.mainFrame). 创建new PeriodicTable ,将使用它创建新的MainFrame ,它具有自己的JFrame实例(MainFrame.mainFrame)。 You need to add that panel to existing mainFrame in MainMenu 您需要将该面板添加到MainMenu中的现有mainFrame

I suggest removing changing your PeriodicTable class like this: 我建议删除像这样更改您的PeriodicTable类:

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: 并将您的actionPerformed函数更改为以下形式:

    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
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM