首先让我解释一下我的问题,
我在第一个视图的框架中有一个jpanel pnlBttns,您可以在图像中看到2个按钮。
单击“编辑视频”时,将删除此jpanel,然后将另一个名为editPanel的jpanel添加到我的框架中,如下图所示,
现在,这是问题所在
当我单击“后退”按钮时,我移回了editPanel并显示pnlBttns。但是我的框架显示如下,这两个按钮在那里但没有文字。
。
问题很奇怪,因为再次单击“编辑视频”按钮(尽管它是空的)时,需要我来编辑面板,并显示所有正常和正常的按键。那么为什么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有点重叠。可能是问题所在,但文本仍然应该在那里。我怎么解决这个问题?
感谢您的时间和精力。
最佳答案
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个答案。