gpt4 book ai didi

java - 在 jframe 中读取已删除的面板

转载 作者:行者123 更新时间:2023-11-29 10:02:57 26 4
gpt4 key购买 nike

首先让我解释一下我的问题,

在第一个 View 中,我的框架中有一个 jpanel pnlBttns,它有 2 个按钮,如图所示。

enter image description here

当点击 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 有一点重叠。这可能是问题所在,但文本仍然应该存在。我该如何解决这个问题?

感谢您的时间和努力。

最佳答案

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个回答。

关于java - 在 jframe 中读取已删除的面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18132212/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com