当前位置: 首页 > 知识库问答 >
问题:

内部框架不起作用

令狐经武
2023-03-14

嗨,我在容器内创建了三个帧,每个帧都有三个按钮,执行最小值,最大值和关闭功能。令人惊讶的是,只有一帧在工作,其余三帧不起作用。你能不能整理一下。

      package Project;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.*;
import java.beans.PropertyVetoException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane;
import javax.swing.plaf.basic.BasicInternalFrameUI;

public class Test4 {
      JInternalFrame inf ;
      DesktopPane pane;

    public static void main(String[] args) {
        new Test4();
    }

    private int xpos = 0;
    private int ypos = 0;

    public Test4() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception exp) {
                    exp.printStackTrace();
                }
                pane = new DesktopPane();
                pane.add(newInternalFrame());
                pane.add(newInternalFrame());
                pane.add(newInternalFrame());

                JFrame frame = new JFrame();
                frame.add(pane);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(400, 400);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

            }
        });
    }

    public JInternalFrame newInternalFrame() {
      inf= new JInternalFrame("Blah", true, true, true, true);
        inf.setLocation(xpos, ypos);
        inf.setSize(200, 100);
        inf.setVisible(true);

        xpos += 50;
        ypos += 50;

        JPanel jp = new JPanel();
        JLabel jl=new JLabel("panel"+xpos);

        JButton jb = new JButton("_");
        JButton jb2 = new JButton("[]");
        JButton jb3 = new JButton("X");

        inf.setLayout(new GridLayout(2, 2));
jp.add(jl);
        jp.add(jb);
        jp.add(jb2);
        jp.add(jb3);

        inf.add(jp);
        jb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                try {
                    if (inf.getLayer() == JDesktopPane.FRAME_CONTENT_LAYER) {
                        pane.remove(inf);
                        pane.add(inf, JDesktopPane.DEFAULT_LAYER);
                        pane.revalidate();
                        pane.repaint();
                    }
                    inf.pack();
                    inf.setIcon(true);
                } catch (PropertyVetoException ex) {
                    ex.printStackTrace();
                }

            }
        });
        jb2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                try {
                    if (inf.isMaximum()) {//restore
                        inf.pack();
                    } else {//maximize
                        inf.setMaximum(true);

                    }
                    pane.remove(inf);
                    pane.add(inf, JDesktopPane.FRAME_CONTENT_LAYER);
                    pane.revalidate();
                    pane.repaint();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });
        jb3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                try {
                    inf.dispose();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }

            }
        });


        BasicInternalFrameTitlePane titlePane = (BasicInternalFrameTitlePane) ((BasicInternalFrameUI) inf.getUI()).getNorthPane();
        inf.remove(titlePane);
        return inf;
    }

    public class DesktopPane extends JDesktopPane {

        @Override
        public void doLayout() {
            super.doLayout();
            List<Component> icons = new ArrayList<Component>(25);
            int maxLayer = 0;

            for (Component comp : getComponents()) {
                if (comp instanceof JInternalFrame.JDesktopIcon) {
                    icons.add(comp);
                    maxLayer = Math.max(getLayer(comp), maxLayer);
                }
            }

            maxLayer++;
            int x = 0;
            for (Component icon : icons) {

                int y = getHeight() - icon.getHeight();
                icon.setLocation(x, y);
                x += icon.getWidth();
                setLayer(icon, maxLayer);

            }
        }
     /*   public void doLayout() {
            super.doLayout();
            List<Component> icons = new ArrayList<Component>(25);
            for (Component comp : getComponents()) {
                if (comp instanceof JInternalFrame.JDesktopIcon) {
                    icons.add(comp);
                }
            }

            int x = 0;
            for (Component icon : icons) {

                int y = getHeight() - icon.getHeight();
                icon.setLocation(x, y);
                x += icon.getWidth();

            }
        }*/
    }
}

共有2个答案

亢嘉茂
2023-03-14

只需添加以下行

final JInternalFrame inf= new JInternalFrame("Blah", true, true, true, true);

代替inf=new JInternalFrame("Blah", true, true, true, true);

并从main中删除此JInternalFrame inf

松智勇
2023-03-14
package Project;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.*;
import java.beans.PropertyVetoException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane;
import javax.swing.plaf.basic.BasicInternalFrameUI;

public class Test4 {

    private JDesktopPane pane;

    public static void main(String[] args) {
        new Test4();// there was a little change here
    }
    private int xpos = 0;
    private int ypos = 0;

    public Test4() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception exp) {
                    exp.printStackTrace();
                }
                pane = new Test4.DesktopPane() {
                    @Override
                    public Dimension getPreferredSize() {
                        return new Dimension(400, 400);
                    }
                };
                pane.add(newInternalFrame());
                pane.add(newInternalFrame());
                pane.add(newInternalFrame());

                JFrame frame = new JFrame();
                frame.add(pane);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

            }
        });
    }

    public JInternalFrame newInternalFrame() {
        final JInternalFrame inf = new JInternalFrame("Blah", true, true, true, true);
        inf.setLocation(xpos, ypos);
        inf.setSize(200, 100);
        inf.setVisible(true);

        xpos += 50;
        ypos += 50;

        JPanel jp = new JPanel();
        JLabel jl = new JLabel("panel" + xpos);

        JButton jb = new JButton("_");
        JButton jb2 = new JButton("[]");
        JButton jb3 = new JButton("X");

        inf.setLayout(new GridLayout(2, 2));
        jp.add(jl);
        jp.add(jb);
        jp.add(jb2);
        jp.add(jb3);

        inf.add(jp);
        jb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                try {
                    if (inf.getLayer() == JDesktopPane.FRAME_CONTENT_LAYER) {
                        pane.remove(inf);
                        pane.add(inf, JDesktopPane.DEFAULT_LAYER);
                        pane.revalidate();
                        pane.repaint();
                    }
                    inf.pack();
                    inf.setIcon(true);
                } catch (PropertyVetoException ex) {
                    ex.printStackTrace();
                }

            }
        });
        jb2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                try {
                    if (inf.isMaximum()) {//restore
                        inf.pack();
                    } else {//maximize
                        inf.setMaximum(true);

                    }
                    pane.remove(inf);
                    pane.add(inf, JDesktopPane.FRAME_CONTENT_LAYER);
                    pane.revalidate();
                    pane.repaint();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });
        jb3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                try {
                    inf.dispose();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }

            }
        });


        BasicInternalFrameTitlePane titlePane = (BasicInternalFrameTitlePane) ((BasicInternalFrameUI) inf.getUI()).getNorthPane();
        inf.remove(titlePane);
        return inf;
    }

    public class DesktopPane extends JDesktopPane {

        @Override
        public void doLayout() {
            super.doLayout();
            List<Component> icons = new ArrayList<Component>(25);
            int maxLayer = 0;

            for (Component comp : getComponents()) {
                if (comp instanceof JInternalFrame.JDesktopIcon) {
                    icons.add(comp);
                    maxLayer = Math.max(getLayer(comp), maxLayer);
                }
            }

            maxLayer++;
            int x = 0;
            for (Component icon : icons) {

                int y = getHeight() - icon.getHeight();
                icon.setLocation(x, y);
                x += icon.getWidth();
                setLayer(icon, maxLayer);
            }
        }
    }
}
 类似资料:
  • 是否可以在JavaFX中制作相同的内部帧?

  • 我正在构建一个Quarkus应用程序,它使用resteasy处理http请求,并使用restclient调用另一个api,我需要传播一个头并动态添加另一个头,所以我添加了一个实现ClientHeadersFactory的类。 代码如下: 我的问题是配置的注入不起作用。我使用和两种方法都进行了尝试,如的javadoc所述。我也试图使类不抽象,但它没有改变任何东西。 MicroServicesConf

  • 问题内容: 我正在尝试在作为Jenkins Pipeline一部分运行的docker容器内执行一些git查询。在docker容器外面,sshsgent工作正常,我可以访问我的SCM没问题。在容器内部,我收到主机密钥验证问题。 有人可以帮助我解决我所犯的错误吗? 首先whoami呼叫输出: docker容器输出中的第二个调用(和回显): 问题答案: 主机密钥验证失败 容器中的SSH连接无法验证主机(

  • 我正在使用和。 我想锁定JDesktopPane内部的内部框架。 建议需要更改的方法或属性。

  • 问题内容: 我究竟做错了什么?为什么$ path在函数外部正确打印,但是在函数内部无法访问? 问题答案: 因为它没有在函数中定义。 有几种方法可以解决此问题: 1)使用亚历克斯所说的话,告诉函数它是一个全局变量: 2)将其定义为常量: 3)如果特定于该函数,则将其传递给该函数: 根据功能的实际工作原理,其中之一会起作用。

  • 问题内容: 这是显示问题的小提琴。http://jsfiddle.net/Erk4V/1/ 如果我在ng-if内部有一个ng-model,就会出现该模型无法正常工作的情况。 我想知道这是一个错误还是误解了正确的用法。 问题答案: 与其他指令一样,该指令将创建子范围。请参见下面的脚本(或此jsfiddle) 因此,您的复选框将更改子作用域的内部,而不更改外部父作用域。 请注意,如果要修改父作用域中的