我可以使用什么来代替 JPanel 中的按钮来在所有操作系统中获得相同的外观?

发布于 2024-10-17 08:10:22 字数 1068 浏览 3 评论 0原文

我需要生成一个由不同颜色的正方形组成的矩形网格(比如说 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

内心旳酸楚 2024-10-24 08:10:22

但是我需要一种方法将标签放在图像之上。

您可以使用以下方法显示在图像上居中的文本:

label.setIcon(...);
label.setText("Text over Image");
label.setHorizontalTextPosition(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.CENTER);

或者您可以使用 后台面板条目。

But than I need to have a way to put labels on top of the images.

You can display the text centered over the image by using:

label.setIcon(...);
label.setText("Text over Image");
label.setHorizontalTextPosition(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.CENTER);

Or you can use one of the suggestions from the Background Panel entry.

江湖正好 2024-10-24 08:10:22

我认为最稳定的解决方案......是使用图像作为背景......

另一种方法是实现 Icon 接口,如在此 示例也演示了 @camickr 建议的变体。结果是多功能的文本放置和对绘画的严格控制。

I think that the most stable solution … is to use images as background…

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.

不弃不离 2024-10-24 08:10:22

这个例子怎么样

package test;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class PanelBG {
    JFrame f;
    public PanelBG() {
        f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        int nRows = 3;
        int nColumns = 4;
        int cellSize = 50;
        JPanel fieldPanel = new JPanel();
        fieldPanel.setLayout(new GridLayout(nRows, nColumns));
        fieldPanel.setMaximumSize(new Dimension(nColumns*cellSize, nRows*cellSize));
        Color colors[]={
                new Color(115, 114, 83),
                new Color(120, 130, 83),
                new Color(140, 140, 83),
                new Color(160, 180, 83),
                new Color(170, 190, 83),
                new Color(80, 190, 73),
                new Color(100, 120, 83),
                new Color(150, 240, 83),
                new Color(180, 150, 83),
                new Color(210, 190, 83),
                new Color(230, 11, 83),
                new Color(255, 255, 255)
        };
        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.getText() + "</span></html>");
              //btn.setPreferredSize(new Dimension(cellSize, cellSize));
              //btn.setHorizontalTextPosition(SwingConstants.LEFT);
              //btn.setBackground(new Color(54,36,5));
              //fieldPanel.add(btn);
               JPanel p = new JPanel();
               p.setPreferredSize(new Dimension(cellSize, cellSize));
               JLabel l = new JLabel("Jlabel");
               p.add(l);
               p.setBackground(colors[i*j-1]);
               fieldPanel.add(p);
           }
        }
        f.getContentPane().add(fieldPanel);
        f.pack();
        f.setVisible(true);
    }
    public static void main(String[] args) {
        new PanelBG();
    }
}

How about this example

package test;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class PanelBG {
    JFrame f;
    public PanelBG() {
        f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        int nRows = 3;
        int nColumns = 4;
        int cellSize = 50;
        JPanel fieldPanel = new JPanel();
        fieldPanel.setLayout(new GridLayout(nRows, nColumns));
        fieldPanel.setMaximumSize(new Dimension(nColumns*cellSize, nRows*cellSize));
        Color colors[]={
                new Color(115, 114, 83),
                new Color(120, 130, 83),
                new Color(140, 140, 83),
                new Color(160, 180, 83),
                new Color(170, 190, 83),
                new Color(80, 190, 73),
                new Color(100, 120, 83),
                new Color(150, 240, 83),
                new Color(180, 150, 83),
                new Color(210, 190, 83),
                new Color(230, 11, 83),
                new Color(255, 255, 255)
        };
        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.getText() + "</span></html>");
              //btn.setPreferredSize(new Dimension(cellSize, cellSize));
              //btn.setHorizontalTextPosition(SwingConstants.LEFT);
              //btn.setBackground(new Color(54,36,5));
              //fieldPanel.add(btn);
               JPanel p = new JPanel();
               p.setPreferredSize(new Dimension(cellSize, cellSize));
               JLabel l = new JLabel("Jlabel");
               p.add(l);
               p.setBackground(colors[i*j-1]);
               fieldPanel.add(p);
           }
        }
        f.getContentPane().add(fieldPanel);
        f.pack();
        f.setVisible(true);
    }
    public static void main(String[] args) {
        new PanelBG();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文