簡體   English   中英

在jframe中讀取已刪除的面板

[英]readding removed panel in jframe

首先讓我解釋一下我的問題,

我在第一個視圖的框架中有一個jpanel pnlBttns,您可以在圖像中看到2個按鈕。

在此處輸入圖片說明

當單擊Edit Video時 ,該jpanel被刪除,我將另一個名為editPanel的jpanel添加到我的框架中,如下圖所示,

pnlBttns

現在, 這是問題所在

當我單擊“后退”按鈕時,我移回了editPanel並顯示pnlBttns。 但是我的框架顯示如下,這兩個按鈕在那里但沒有文字。

editPanel

這個問題很奇怪,因為再次單擊“編輯視頻”按鈕(盡管它是空的)時,需要我來編輯面板,並顯示所有正常和正常的按鍵。 那么為什么pnlBttns不能正確顯示,而不能正確“重繪”。

代碼

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

        }   
    }   
}

另一個版本

要隱藏pnlBttns,

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

為了再次顯示,

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

我已經檢查並100%確定,我在哪里沒有更新兩個按鈕的文本。

我猜是問題所在,可能是面板大小。

黑色顯示的是我的視頻播放器,刪除pnlBttns時,它的大小會增加,因為editPanel更薄,重新添加時,mediaPlayer也與pnlBttns有點重疊。 可能是問題所在,但文本仍然應該在那里。 我怎么解決這個問題?

感謝您的時間和精力。

每當我看到使用remove / add方法的代碼時,我建議您應該改用Card Layout 它將管理適當面板的顯示。

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

為了再次顯示,

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

PS:我的第100個答案。

嘗試使用setVisible(false)而不是將其刪除,然后當您希望它再次出現時,請使用setVisible(true)

沒錯,面板寬度可能是原因,請不要更改視頻播放器的寬度,如果問題仍然存在,
我希望您使用JLayeredPane,然后在該窗格上添加兩個面板,並使用setVisible(true)和setVisible(false)來顯示或隱藏面板,這是一種簡單的方法,不會產生錯誤。
給我您對答案的反饋。

當我與NetBeans一起使用秋千時,我曾經遇到過同樣的問題。.苦苦掙扎了幾個小時..我所做的只是重新啟動了IDE ..問題就消失了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM