简体   繁体   English

使JLabel适合其父级JPanel或为其提供最小大小

[英]make JLabel fit its parent JPanel or give a minimum size to it

I have a gridlayout that consist from jPanels. 我有一个由jPanels组成的gridlayout。 Every JPanel containst a JLabel, they are 9x9. 每个JPanel都包含一个JLabel,它们是9x9。 For this is a chess game, I need to be able to highlight every jPanel or its jLabel upon some events (such as being possible move option or an endangered field). 因为这是一个国际象棋游戏,所以我需要能够在某些事件(例如可能的移动选项或濒临灭绝的区域)上突出显示每个jPanel或其jLabel。 However, I can only highlight fields that have their icons (figures) set, which means an empty jLabel has probably zero dimensions. 但是,我只能突出显示已设置其图标(数字)的字段,这意味着空的jLabel可能具有零尺寸。

This is how I put a figure in jLabel ( it sucks , but it somewhat works): 这就是我在jLabel中放置一个图形的方式( 它很烂 ,但是有些起作用):

public void drawFigureAt(Figure fg, int x, int y) {
    //retrieve the Jlabel from the field array
    JLabel pole = policka_l[y*8+x];
    //Set icon from file
    pole.setIcon(new ImageIcon(fg.imageName()+(fg.color==1?"_white":"_black")+".png"));
    //force redraw or whatever it is
    pole.revalidate();
}

Some of the fields are now highlighted in very transparent black, so I can see that the JLabels ar as large as their icon is. 现在,某些字段以非常透明的黑色突出显示,因此我可以看到JLabels的大小与它们的图标一样大。
This is how I highlight: 这就是我强调的方式:

public void highlightField(int x, int y, Color color) {
    //Get the JLabel obejct
    JLabel pole = policka_l[y*8+x];
    //Set the color as the background
    pole.setBackground(color);
    //What is this?
    pole.setOpaque(true);
    //Another magic
    pole.revalidate();
    repaint();
}

But, as I've said, if JLabel has no icon, it can't be highlighted and that's very sad form me. 但是,正如我所说的,如果JLabel没有图标,则无法突出显示它,这对我来说是非常可悲的。 I's much worse that this is a school project that must work this morning, so using different rendering system is not in question. 更糟糕的是,这是一个必须在今天早上进行的学校项目,因此使用其他渲染系统并不是问题。
I've found some clues how to deal with it, however, they don't work. 我找到了一些如何处理它的线索,但是它们不起作用。 I hoped a lot in this: 我希望很多:

JLabelinstance.setMinimumSize(new Dimension(70, 70));

No effect unfortunatelly. 毫无效果。

So what I want now is to either make the function above work, or even better - configure JLabels to fill their parent JPanels so that highlighting will look nice, not stupid. 因此,我现在想要的是使上面的功能正常工作,或者甚至更好-配置JLabel以填充其父级JPanels,以使突出显示看起来不错,而不是愚蠢的。
Also my figures do not resize with the window at all, so they tend to overflow. 另外,我的图形根本不随窗口调整大小,因此它们倾向于溢出。

I have a gridlayout that consist from jPanels. 我有一个由jPanels组成的gridlayout。

You could: 你可以:

1) Just add the labels directly to the GridLayout. 1)只需将标签直接添加到GridLayout中即可。 Then the label will be the size of the grid 然后标签将是网格的大小
2) Set the layout manager of the panel to be a BorderLayout, then the label will be resized to fill the entire space of the panel. 2)将面板的布局管理器设置为BorderLayout,然后将调整标签的大小以填充面板的整个空间。

 pole.setBackground(color);
 pole.setOpaque(true);
 //pole.revalidate();
 //repaint();

The revalidate() and repaint() methods are not needed. 不需要revalidate()和repaint()方法。 Swing components will automatically repaint themselves when a property changes. 当属性更改时,Swing组件将自动重新绘制自身。

Also my figures do not resize with the window at all, so they tend to overflow. 另外,我的图形根本不随窗口调整大小,因此它们倾向于溢出。

Don't use setSize(). 不要使用setSize()。 You should add the icons to the label and then labels to the grid. 您应该将图标添加到标签,然后将标签添加到网格。 Then pack() the frame and the grid will be sized to hold the largest icon. 然后pack()框架和网格的大小将容纳最大的图标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM