听/处理JPanel活动 [英] Listening/Handling JPanel events
问题描述
我有一个Java Swing的问题,我无法解决,也许你可以帮助我。这里是:
- 我有一个JFrame使用BorderLayout和许多JPanels。
- 时间我需要安装一个新的屏幕(即从主菜单,当搜索按钮被点击时,进入搜索菜单),我只是删除位于中心的组件(JPanel),并将新的屏幕新的JPanel)在中心。
- 这样,每次我想要添加一个新屏幕时,我不会调用我的所有页眉和页脚对象。
除了这个小小的问题,一切都可以正常运行:我想在每次安装新的JPanel或更换时触发一些方法到现有的JPanel(一般来说,每次JPanel出现)。
为了做到这一点,我试图实现ComponentListener的componentShown(ComponentEvent e)方法,并将一个ComponentListener添加到我放在我的JFrame中心的JPanel ,它没有工作。之后,我做了一些研究,发现这个componentShown(@ComponentListener)方法只有当JPanel的可见性改变(从不可见到可见或相反)时才有效。不幸的是,我没有改变JPanel的可见性,只是用另一个替换它:删除当前的,并添加新的。下面的代码说明了如何更换JPanels。
//获取位于我们的JFrame中心的JPanel
JPanel currentView =(JPanel)myFrame.getContentPane ).getComponent(2);
if(currentView!= null)
{
//从JPanel
中删除它myFrame.getContentPane()。remove(currentView);
}
//添加新的JPanel
myFrame.getContentPane()。add(otherView,BorderLayout.CENTER);
//打包JFrame并显示
myFrame.pack();
所以这里是我所拥有的。我真的很感激,如果你能帮助我。
我认为这个问题对应于 HierarchyListener ,用于比较
import java.awt。*;
import java.awt.event。*;
import javax.swing。*;
public class ContainerListener extends JFrame {
private static final long serialVersionUID = 1L;
public ContainerListener(){
super(Test);
setContentPane(new TestPanel());
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String [] parameters){
EventQueue.invokeLater(new Runnable(){
@Override
public void run(){
ContainerListener containerListener = new ContainerListener();
}
});
}
私有类TestPanel扩展JPanel {
private static final long serialVersionUID = 1L;
TestPanel(){
setLayout(new FlowLayout(FlowLayout.LEFT));
add(new JButton(new AbstractAction(Add label){
private static final long serialVersionUID = 1L;
private int n = 0;
@Override
public void actionPerformed(ActionEvent event){
TestPanel.this.add(new JLabel(Label+ ++ n));
validate();
}
}));
addHierarchyListener(new HierarchyListener(){
@Override
public void hierarchyChanged(HierarchyEvent e){
System.out.println(Components Change:+ e .getChanged());
如果((e.getChangeFlags()& HierarchyEvent.DISPLAYABILITY_CHANGED)!= 0){
if(e.getComponent()。isDisplayable()){
System.out.println(Components:+ e.getChanged());
} else {
System.out.println(Components:+ e.getChanged());
}
}
}
});
addContainerListener(new ContainerAdapter(){
@Override
public void componentAdded(ContainerEvent event){
System.out.println(componentAdded:+ event。 getChild()+containerName+被添加);
}
});
}
@Override
public Dimension getPreferredSize(){
return new Dimension(400,400);
}
}
}
Good evening ladies and gentlemen,
I have a problem with Java Swing that I cannot solve, maybe you can help me. Here it is:
- I have one JFrame which uses BorderLayout, and many JPanels.
- Every time I need to put up a new screen (i.e. from the Main Menu, when Search button is clicked, go to the Search Menu), I simply remove the component (JPanel) which is located in the center, and put the new screen (new JPanel) in the center instead.
- This way, I don't call all my header and footer objects every time I want to put up a new screen.
Everything works fine with this system except this little problem: I want to trigger some methods everytime I put up a new JPanel or change back to an existing JPanel (generally speaking, everytime a JPanel appears).
In order to do that, I tried to implement ComponentListener's componentShown(ComponentEvent e) method, and added a ComponentListener to a JPanel which I put up in the center of my JFrame, and it did NOT work. After this, I did some research and found out that this componentShown (@ComponentListener) method only works when the visibilty of the JPanel is changed (from invisible to visible or the opposite). Unfortunately, I'm not changing the visibility of a JPanel, just replacing it with another one: removing the current one, and adding the new one. Below code illustrates how I replace the JPanels.
// Get the JPanel located in the center of our JFrame
JPanel currentView = (JPanel) myFrame.getContentPane().getComponent( 2 );
if ( currentView != null )
{
// Remove it from the JPanel
myFrame.getContentPane().remove( currentView );
}
// Add the new JPanel
myFrame.getContentPane().add( otherView, BorderLayout.CENTER );
// Pack the JFrame and show it
myFrame.pack();
So here is what I have. I would really appreciate it if you could help me out.
I think that this issue corresponding with HierarchyListener, for comparing
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ContainerListener extends JFrame {
private static final long serialVersionUID = 1L;
public ContainerListener() {
super("Test");
setContentPane(new TestPanel());
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] parameters) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
ContainerListener containerListener = new ContainerListener();
}
});
}
private class TestPanel extends JPanel {
private static final long serialVersionUID = 1L;
TestPanel() {
setLayout(new FlowLayout(FlowLayout.LEFT));
add(new JButton(new AbstractAction("Add label") {
private static final long serialVersionUID = 1L;
private int n = 0;
@Override
public void actionPerformed(ActionEvent event) {
TestPanel.this.add(new JLabel("Label " + ++n));
validate();
}
}));
addHierarchyListener(new HierarchyListener() {
@Override
public void hierarchyChanged(HierarchyEvent e) {
System.out.println("Components Change: " + e.getChanged());
if ((e.getChangeFlags() & HierarchyEvent.DISPLAYABILITY_CHANGED) != 0) {
if (e.getComponent().isDisplayable()) {
System.out.println("Components: " + e.getChanged());
} else {
System.out.println("Components: " + e.getChanged());
}
}
}
});
addContainerListener(new ContainerAdapter() {
@Override
public void componentAdded(ContainerEvent event) {
System.out.println("componentAdded : " + event.getChild() + "containerName" + " was added");
}
});
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
}
}
这篇关于听/处理JPanel活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!