简体   繁体   中英

readding removed panel in jframe

Let me first of all explain me problem,

I have a jpanel pnlBttns in my frame in the first view, which has 2 buttons as you can see in the image.

在此处输入图片说明

When clicked on Edit Video , this jpanel is removed, and i add another jpanel named editPanel to my frame, as shown in the image below,

pnlBttns

Now, here's the problem ,

When I click on the back button, I remove back the editPanel and show the pnlBttns. But what my frame shows is as below, the two buttons are there but with no text.

editPanel .

The problem is strange because when clicked again on edit video button(though its empty), it takes me to editPanel, and shows all keys fine and normal. So why is the pnlBttns not shown properly, its not getting "repainted" properly.

Code ,

sscce.java

import java.awt.Dimension;
import java.awt.*;
import java.io.IOException;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Graphics;
import java.io.File;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import java.net.MalformedURLException;
import javax.swing.JComponent;
import javax.swing.JFrame;
import static java.awt.GraphicsDevice.WindowTranslucency.*;



public class sscce extends JFrame implements ActionListener{

    JFrame frame = new JFrame("My Editor");             //Open window.
    String state = new String("wait");
    //Panels
    JPanel pnlBttns = new JPanel();
    JPanel editPanel = new JPanel();
    JPanel uploadPanel = new JPanel();
    JPanel welcome = new JPanel();
    JPanel f = new JPanel();
    JPanel head = new JPanel();

    //Buttons
    JButton button1 = new JButton("E");
    JButton button2 = new JButton("U");
    JButton cut = new JButton("Cut");
    JButton trim = new JButton("Trim");
    JButton audio = new JButton("Audio");
    JButton done = new JButton("Done");
    JButton play = new JButton();
    JButton stop = new JButton();

    public sscce() {
        //Customize Window to record.
        frame.setLayout(new BorderLayout());
        frame.setLocation(100,100);
        frame.setSize(1200,700);
    // ****On closing window event
        pnlBttns.setLayout(new BorderLayout());

        //Paddings
        editPanel.setBorder(BorderFactory.createCompoundBorder(new EmptyBorder(30, 10, 30, 10), new EmptyBorder(0,0,0,0)));
        uploadPanel.setBorder(BorderFactory.createCompoundBorder(new EmptyBorder(30, 10, 30, 10), new EmptyBorder(0,0,0,0)));
        pnlBttns.setBorder(BorderFactory.createCompoundBorder(new EmptyBorder(80, 10, 340, 10), new EmptyBorder(0,0,0,0)));

        //Set commands for Buttons 
        button1.setToolTipText("Start Editing Video"); 
        button1.setActionCommand("edit");
        button1.addActionListener(this);
        button2.setToolTipText("Upload this Video"); 
        button2.setActionCommand("upload");
        button2.addActionListener(this);
        pnlBttns.add(button1, BorderLayout.NORTH);          
        pnlBttns.add(button2, BorderLayout.SOUTH);
        frame.add(pnlBttns, BorderLayout.EAST);

        //create edit panel
        editPanel.setLayout(new BoxLayout(editPanel, BoxLayout.Y_AXIS));
        done.setToolTipText("Go back");
        done.setActionCommand("done");
        done.addActionListener(this);
        cut.setToolTipText("Cut Video into parts");
        cut.setActionCommand("cut");
        cut.addActionListener(this);
        trim.setToolTipText("Remove parts of Video");
        trim.setActionCommand("trim");
        trim.addActionListener(this);
        audio.setToolTipText("Edit audio of Video");
        audio.setActionCommand("audio");
        audio.addActionListener(this);
        editPanel.add(audio);
        editPanel.add(Box.createRigidArea(new Dimension(0, 10)));
        editPanel.add(cut);
        editPanel.add(Box.createRigidArea(new Dimension(0, 10)));
        editPanel.add(trim);
        editPanel.add(Box.createRigidArea(new Dimension(0, 50)));
        editPanel.add(done);
        frame.add(editPanel, BorderLayout.EAST);
        editPanel.setVisible(false);
        frame.revalidate();
        frame.repaint();
        frame.add(pnlBttns, BorderLayout.EAST);
        //create upload panel.
        addVideo();
    }

    public void addVideo()
        {   head.setLayout(new GridBagLayout());
            f.setLayout(new BorderLayout());
            f.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
            JPanel videoPanel = new JPanel();
            f.add(videoPanel, BorderLayout.CENTER);
            frame.add(f, BorderLayout.CENTER);
            frame.add(head, BorderLayout.PAGE_START);
            frame.setVisible(true);
        }
    public static void main(String[] args){
        new sscce();
    }

    public void actionPerformed(ActionEvent e){


        if ("edit".equals(e.getActionCommand())) {

            state = "edit";
            pnlBttns.setVisible(false);
            editPanel.setVisible(true);
            frame.revalidate();
            frame.repaint();

        } else if ("done".equals(e.getActionCommand())){

                editPanel.setVisible(false);
                pnlBttns.setVisible(true);
                frame.revalidate();
                frame.repaint();

        }   
    }   
}

another version,

To hide pnlBttns,

state = "edit";
frame.remove(pnlBttns);
frame.add(editPanel, BorderLayout.EAST);
frame.revalidate();
frame.repaint();

To show it again,

frame.remove(editPanel);
frame.add(pnlBttns);
frame.revalidate();
frame.repaint();

I have checked and am 100% sure, I am no where updating the text of the two buttons.

What I guess is the problem, may be size of panel.

What you see in black is my video player, its size gets increased when pnlBttns is removed, since editPanel is thinner, when added back, mediaPlayer also overlaps a little with the pnlBttns. Can that be the problem, still the text should have been there. How can i solve this problem?

Thanks for your time and efforts.

Whenever I see code using the remove/add methods I recommend that you should be using a Card Layout instead. It will manage the display of the proper panel.

state = "edit";
frame.remove(pnlBttns);
frame.add(editPanel, BorderLayout.EAST);
frame.revalidate();
frame.repaint();

To show it again,

frame.remove(editPanel);
frame.add(pnlBttns, BorderLayout.EAST);    //May be you need to add this to east
frame.revalidate();
frame.repaint();

PS: My 100th answer.

尝试使用setVisible(false)而不是将其删除,然后当您希望它再次出现时,请使用setVisible(true)

You are right, The Width of the Panel can be the cause, Just don't change the width of the Video Player, If the problem persist,
I would prefer you to use JLayeredPane, and on that pane add both the panels and use setVisible(true) and setVisible(false) for showing or hiding panels, that's an easy way and will not produce errors.
Give me your feedback about the answer.

当我与NetBeans一起使用秋千时,我曾经遇到过同样的问题。.苦苦挣扎了几个小时..我所做的只是重新启动了IDE ..问题就消失了。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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