我有一个带有JLabel的JPanel,它经常加载一张新图片。我编程它调整大小以适应每次加载后的图片。但是,我希望用户能够将面板调整到一定的大小并保持这种状态(当前重新加载无法永久调整大小可能会加剧)。我的代码不起作用:
public class Grapher extends JFrame{
private JScrollPane scrollPane;
private static JPanel topPanel;
private static JLabel label;
private static ImageIcon image;
private static boolean dontResize = false;
private static boolean firstTime = true;
public Grapher(){
setTitle( "some graph" );
setBackground( Color.gray );
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel );
label = new JLabel();
scrollPane = new JScrollPane();
scrollPane.getViewport().add( label );
topPanel.add( scrollPane, BorderLayout.CENTER );
topPanel.addComponentListener(new Listeners());
topPanel.setName("Soar Grapher Panel");
}
public void display(String fileName){
//steal focus the first time an image is displayed
if(image == null)
setVisible(true);
image = new ImageIcon(fileName);
image.getImage().flush();
boolean temp = dontResize;
setSize(getPreferredSize());
dontResize = temp;
System.out.println("afterWards: " + dontResize);
label.setIcon(image);
label.repaint();
}
@Override
public Dimension getPreferredSize(){
if(dontResize){
System.err.println("not resizing!");
return getSize();
}
if (image == null)
return new Dimension(100,100);
else
return new Dimension(image.getIconWidth() > getWidth() + 40 ? image.getIconWidth() + 40 : getWidth(),
image.getIconHeight() > getHeight() + 60 ? image.getIconHeight() + 60 : getHeight());
}
private class Listeners implements ComponentListener{
@Override
public void componentHidden(ComponentEvent arg0) {}
@Override
public void componentMoved(ComponentEvent arg0) {}
@Override
public void componentResized(ComponentEvent arg0) {
System.out.println("componentResized: " + dontResize);
//TODO make it not resize after the user resizes it themselves.
System.err.println(arg0.getComponent().getName());
System.err.println("resized!");
if(!firstTime)
dontResize = true;
firstTime = false;
}
@Override
public void componentShown(ComponentEvent arg0) {}
}
}
我正在尝试在程序调整大小时保存dontResize布尔值,但不幸的是,监听器似乎在resize()
函数之前激活。如何区分用户调整大小和程序调整大小?
EDIT_的 _ __ _ __ 在程序化调整大小期间删除监听器似乎是要走的路;然而,除了用户调整大小之外,似乎随机调用了监听器,搞乱了一切。这就是我所拥有的:
private static ComponentListener listeners = new Listeners();
private static boolean resizeOkay = true;
...
[inside the display() function]
if(resizeOkay){
removeComponentListener(listeners);
System.err.println("No handler should be called between here");
setSize(getPreferredSize());
System.err.println("and here.");
addComponentListener(listeners);
}
label.setIcon(image);
label.repaint();
[inside the listener]
@Override
public void componentResized(ComponentEvent arg0) {
System.err.println("resize handler");
resizeOkay = false;
}
如果没有任何用户调整大小,上面的代码会打印出来:
No handler should be called between here
and here.
resize handler
resize handler
怎么叫它?这可以防止窗口被调整大小。
答案 0 :(得分:2)
如何区分用户调整大小和程序化大小调整?
对于程序化大小调整,代码类似于:
panel.removeListeners(...);
doPanelResizing();
panel.addListeners();
这就是程序可以控制什么样的监听器被解雇。
答案 1 :(得分:1)
if (firstTime) {
setResizable(false);
}
答案 2 :(得分:1)
@Nate Glenn使用setResisable()方法作为@StanislavL的建议(+1)。它将禁用用户调整大小,同时您仍然可以通过编程方式调整大小。自己尝试一下,例如。将此主要内容添加到您的代码并运行它,看看该框架的大小为200,200,您将无法调整其大小。
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
JFrame f = new Grapher();
f.setSize(400, 300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(false);
f.setSize(200,200);
f.setVisible(true);
}
});
}
否则,如果您的问题实际上更具体,那么请尝试重新解释您的问题吗?