我可以使用什么来代替 JPanel 中的按钮来在所有操作系统中获得相同的外观?
我需要生成一个由不同颜色的正方形组成的矩形网格(比如说 3 x 4)。此外,正方形(网格的单元格)必须包含相同的短文本。目前,我使用按钮作为网格元素来解决问题。更详细地说,我有以下代码:
JPanel fieldPanel = new JPanel();
fieldPanel.setLayout(new GridLayout(nRows, nColumns));
fieldPanel.setMaximumSize(new Dimension(nColumns*cellSize, nRows*cellSize));
for (int i=1; i<=nRows; i++) {
for (int j=1; j<=nColumns; j++) {
JButton btn = new JButton("<html><span style='color:#000000; font-size: 11pt;'>" + label + "</span></html>");
btn.setPreferredSize(new Dimension(cellSize, cellSize));
btn.setHorizontalTextPosition(SwingConstants.LEFT);
btn.setBackground(col);
fieldPanel.add(btn);
}
}
这种方法有一个缺点。我在 Windows 7 中看到不同颜色的按钮,但在 Windows XP 中它们是灰色的。为了解决这个问题,我手动设置了外观:
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
但这也会引起问题。它改变了调用我的程序的主软件的外观(我的程序是“大”软件的一部分)。
所以,我想从根本上解决这个问题。我想用其他东西代替按钮。我认为最稳定的解决方案(保证所有操作系统中相同的外观是使用图像作为网格单元的背景(单调颜色的图像))。但我需要有一种方法将标签放在图像之上。
有人可以告诉我如何做到吗?
I need to generate a rectangular grid (let say 3 by 4) consisting of squares of different colors. Moreover, the squares (cells of the grid) have to contain same short text. At the moment I solve the problem using buttons as elements of the grid. In more details, I have the following code:
JPanel fieldPanel = new JPanel();
fieldPanel.setLayout(new GridLayout(nRows, nColumns));
fieldPanel.setMaximumSize(new Dimension(nColumns*cellSize, nRows*cellSize));
for (int i=1; i<=nRows; i++) {
for (int j=1; j<=nColumns; j++) {
JButton btn = new JButton("<html><span style='color:#000000; font-size: 11pt;'>" + label + "</span></html>");
btn.setPreferredSize(new Dimension(cellSize, cellSize));
btn.setHorizontalTextPosition(SwingConstants.LEFT);
btn.setBackground(col);
fieldPanel.add(btn);
}
}
This approach has a disadvantage. I see buttons of different colors on Windows 7, but they are gray in Windows XP. To solve this problem I set the look and feel manually:
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
But this also cause problems. It change the appearance of the main software that calls my program (my program is a part of a "big" software).
So, I want to solve the problem radically. I want to replace buttons by something else. And I think that the most stable solution (that guaranty the same appearance in all operating systems is to use images as background for the cell of the grid (images of monotonic colors)). But than I need to have a way to put labels on top of the images.
Can anybody, please, tell me how it can be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用以下方法显示在图像上居中的文本:
或者您可以使用 后台面板条目。
You can display the text centered over the image by using:
Or you can use one of the suggestions from the Background Panel entry.
另一种方法是实现
Icon
接口,如在此 示例也演示了 @camickr 建议的变体。结果是多功能的文本放置和对绘画的严格控制。Another approach is to implement the
Icon
interface, as seen in this example that also demonstrates a variation on @camickr's suggestion. The result is versatile text placement and stringent control over painting.这个例子怎么样
How about this example