repaint()方法不会绘制新帧

时间:2017-04-28 17:51:13

标签: java swing jframe jpanel repaint

我看了很多答案,但我仍然找不到解决方案。我有一个JFrame和两个JPanel。我想删除一个面板,并在按下按钮时将其替换为秒,但repaint()方法不刷新框架。请帮忙。

这是我的框架代码:

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

这是我的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();
    }
}

最后是我的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);
    }
}

2 个答案:

答案 0 :(得分:2)

我不知道你为什么要扩展MainFrame。看起来对我没用。

  

我想删除一个面板,并在按下按钮时将其替换为第二个面板

然后使用CardLayout。阅读How to Use CardLayout上的Swing教程中的部分,以获取一个工作示例。

本教程将向您展示如何更好地构建代码。

答案 1 :(得分:2)

PeriodicTable延长MainFrame。在创建new PeriodicTable时,您可以使用新MainFrame创建新JFrame个实例PeriodicTable(MainFrame.mainFrame)。您需要将该面板添加到MainMenu中的现有mainFrame

我建议您删除更改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); } } 类,如下所示:

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 } 功能更改为以下内容:

LIST