0

所以我在 JFrame 上有一个 desktopPane,在 desktopPane 上是一个 JInternalFrame。现在我将 internalFrame 的位置设置为 desktopPane 的中心,但是当我调整整个 Frame 的大小时,InternalFrame 保持在原来的位置并且不会将位置更改为新的中心。

public void openLogin(){
    JInternalFrame iFrame = new LoginScreen(appMain);
    desktopPane =new JDesktopPane();
    add(desktopPane);
    frameSize = getSize();
    desktopPane.setSize(frameSize);
    Dimension desktopSize = desktopPane.getSize();
    Dimension jInternalFrameSize = iFrame.getSize();
    iFrame.setLocation((desktopSize.width - jInternalFrameSize.width)/2, (desktopSize.height- jInternalFrameSize.height)/2);
    desktopPane.add(iFrame);
}

这是我设置位置的方法,如何更改它以匹配新中心?我和一些听众一起尝试过,但没有任何效果。


好的,所以因为我没有时间使用来自充满 Eels 的 @Hovercraft 的答案,所以我再次尝试了一个组件监听器:

public void openLogin(){
    JInternalFrame iFrame = new LoginScreen(appMain);
    desktopPane =new JDesktopPane();
    add(desktopPane);
    frameSize = getSize();
    newSize=frameSize;
    desktopPane.addComponentListener(new ComponentListener() {

        @Override
        public void componentShown(ComponentEvent e) {

        }

        @Override
        public void componentResized(ComponentEvent e) {
            newSize = getSize();
        }

        @Override
        public void componentMoved(ComponentEvent e) {

        }

        @Override
        public void componentHidden(ComponentEvent e) {

        }
    });
    if(newSize != frameSize){
        desktopPane.setSize(newSize);
        Dimension desktopSize = desktopPane.getSize();
        Dimension jInternalFrameSize = iFrame.getSize();
        iFrame.setLocation((desktopSize.width - jInternalFrameSize.width)/2, (desktopSize.height- jInternalFrameSize.height)/2);
        desktopPane.add(iFrame);

    }
    else{
        desktopPane.setSize(frameSize);
        Dimension desktopSize = desktopPane.getSize();
        Dimension jInternalFrameSize = iFrame.getSize();
        iFrame.setLocation((desktopSize.width - jInternalFrameSize.width)/2, (desktopSize.height- jInternalFrameSize.height)/2);
        desktopPane.add(iFrame);

    }

}

它甚至不会触发侦听器。@Hovercraft,我会试试你的东西,但不是今天,非常感谢你!

好的,我发现我失败了,它现在可以工作了:

public void openLogin(){
    iFrame = new LoginScreen(appMain);
    desktopPane =new JDesktopPane();
    add(desktopPane);
    frameSize = getSize();
    newSize=frameSize;
    addComponentListener(new ComponentListener() {

        @Override
        public void componentShown(ComponentEvent e) {

        }

        @Override
        public void componentResized(ComponentEvent e) {
            newSize = getSize();
            System.out.println("blaaaa");
            desktopPane.setSize(newSize);
            Dimension desktopSize = getSize();
            Dimension jInternalFrameSize = iFrame.getSize();
            iFrame.setLocation((desktopSize.width - jInternalFrameSize.width)/2, (desktopSize.height- jInternalFrameSize.height)/2);

        }

        @Override
        public void componentMoved(ComponentEvent e) {

        }

        @Override
        public void componentHidden(ComponentEvent e) {

        }
    });

    desktopPane.setSize(frameSize);
    Dimension desktopSize = desktopPane.getSize();
    Dimension jInternalFrameSize = iFrame.getSize();
    iFrame.setLocation((desktopSize.width - jInternalFrameSize.width)/2, (desktopSize.height- jInternalFrameSize.height)/2);
    desktopPane.add(iFrame);



}
4

2 回答 2

1

你在评论中说:

我必须像这样使用它,因为在 Login(InternalFrame) 之后,主框架会处理 desktopPane 并加载 Jlists 等其他元素。所以如果我使用组件侦听器,我应该在哪里放置它?在这个方法里面还是在别的地方?

不,你没有。

  • 也许更好的是使用CardLayout来交换视图。
  • 这样您就可以先显示您的登录JPanel,
  • 您可以通过为包装 JPanel 提供一个 GridBagLayout,然后以默认方式将您的登录 JPanel 添加到其中(没有 GridBagConstraints),将这个 JPanel 居中在另一个 JPanel 中
  • 然后使用 JPanel 将此包装 JPanel 添加到您的 CardLayout 中。
  • 然后在成功登录后,切换到工作 JPanel。

另外的选择:

  • 使用模态对话框(例如模态 JDialog 或 JOptionPane)进行登录。
  • 在主 GUI 中显示介绍 JPanel
  • 在此上方显示对话框,再次使其成为模态,以便用户在处理完对话框之前无法与主 GUI 交互。
  • 当对话框不再可见时,程序流程返回调用代码。
  • 在这里您可以检查输入到对话框中的结果,该对话框将告诉您是否从介绍 JPanel 切换到您的工作 JPanel。
  • 同样,使用 CardLayout 来帮助您交换。
于 2014-08-15T12:37:37.880 回答
0

你与ComponentListener. 在componentResized(...)方法中你需要调整JInternalFrame.

请记住,如果用户在桌面上移动 JInternalFrame,如果调整 JFrame 的大小,它将自动重新定位。这可能是不可取的。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JInternalFrameDemo implements Runnable
{
  private JInternalFrame iFrame;
  private JDesktopPane desktop;

  public static void main(String[] args)
  {
    SwingUtilities.invokeLater(new JInternalFrameDemo());
  }

  public void run()
  {
    desktop = new JDesktopPane();
    desktop.setOpaque(true);
    desktop.addComponentListener(new ComponentListener()
    {
      public void componentResized(ComponentEvent e)
      {
        adjustInternalFrameLocation();
      }

      public void componentMoved(ComponentEvent e) {}
      public void componentShown(ComponentEvent e) {}
      public void componentHidden(ComponentEvent e) {}
    });

    JFrame frame = new JFrame("Demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 300);
    frame.setContentPane(desktop);
    frame.setVisible(true);

    iFrame = new JInternalFrame("Centered InternalFrame");
    iFrame.setSize(200, 150);
    iFrame.setVisible(true);
    desktop.add(iFrame);

    adjustInternalFrameLocation();
  }

  private void adjustInternalFrameLocation()
  {
    Dimension desktopDim = desktop.getSize();
    Dimension iFrameDim = iFrame.getSize();

    int x = (desktopDim.width - iFrameDim.width) / 2;
    int y = (desktopDim.height - iFrameDim.height) / 2;

    iFrame.setLocation(x, y);
  }
}
于 2014-08-15T13:35:46.990 回答