所以我在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);
}
这是我设置位置的方法,如何更改它以匹配新中心? 我和一些听众一起尝试过,但没有一点正常。
好的,因为我没有时间使用充满鳗鱼的@Hovercraft的答案,我又尝试了一个componentlistener:
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);
}
答案 0 :(得分:1)
您在评论中说明:
我必须像这样使用它,因为在Login(InternalFrame)之后,主框架处理desktopPane并加载其他元素,如Jlists等。所以,如果我使用组件监听器,我在哪里放置它?在这个方法或其他地方?
不,你没有。
另一种选择:
答案 1 :(得分: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);
}
}