English 中文(简体)
JFrame去除JPanels,并添加一个新的JPanel
原标题:JFrame removing JPanels and adding a new JPanel

我很奇怪的是,有一位辛勤工兵,他们发出一份吉卜赛人的申请,我凌驾于SingWorker为改变JFrame的内容而做的工作。 我想基本上删除所有内容,并根据从服务器中恢复的价值,在联合阵线上增加一个新的成员小组。

现在,我面临的问题是,当我援引以下办法时,我并没有从联合阵线中删除任何内容,也没有改变框架中的内容。

//TODO: Investigate why JFrame content pane won t repaint.
f.removeAll();
//Pass the frame f reference only into MainDisplay, it doesn t actually do anything apart from allowing a class to add a JMenuBar on the JFrame.
f.add(new MainDisplay(f)); 
f.getContentPane().invalidate();
f.getContentPane().validate();
f.getContentPane().repaint();

现行办法 下面是这样,但我会改变联合阵线的内容,然后把新内容加起来。

f.dispose();
f=new ApplicationFrame();

我在呼吁重新启动联合阵线的同时,仔细研究了以前在这里和在谷歌和一些国家使用有效()或无效()方面的答复。

任何建议/帮助都会受到高度赞赏。

Edit:我认为,我会再ug笑,因为还必须有别的错误。

最佳回答

例如

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MyFrame extends JFrame {

    private static final long serialVersionUID = 1L;

    public MyFrame() {
        final JPanel parentPanel = new JPanel();
        parentPanel.setLayout(new BorderLayout(10, 10));

        final JPanel childPanel1 = new JPanel();
        childPanel1.setBackground(Color.red);
        childPanel1.setPreferredSize(new Dimension(300, 40));

        final JPanel childPanel2 = new JPanel();
        childPanel2.setBackground(Color.blue);
        childPanel2.setPreferredSize(new Dimension(800, 600));

        JButton myButton = new JButton("Add Component ");
        myButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                parentPanel.remove(childPanel1);
                parentPanel.add(childPanel2, BorderLayout.CENTER);
                parentPanel.revalidate();
                parentPanel.repaint();
                pack();
            }
        });
        setTitle("My Empty Frame");
        setLocation(10, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        parentPanel.add(childPanel1, BorderLayout.CENTER);
        parentPanel.add(myButton, BorderLayout.SOUTH);
        add(parentPanel);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                MyFrame myFrame = new MyFrame();
            }
        });
    }
}
问题回答

You are trying to repaint()/validate() the ContentPane. Did you try doing same on the JFrame? You can also try JFrame#pack().

修改你的法典

f.setContentPane(new MainDisplay(f)); 
f.getContentPane().invalidate();
f.getContentPane().validate();
f.getContentPane().repaint();

您可使用<代码>Frame. Pack()再次为我工作。 或尝试以下方法:

Frame.setOpaque(false);
Frame.setEnabled(false);
Frame.setVisible(false);
Frame.removeAll();




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签