Java Swing - JPanel 的背景

发布于 2024-08-24 16:34:37 字数 581 浏览 3 评论 0原文

我想设计一个 JPanel ,其颜色编码应如下图所示:


(来源:compendiumblog.com)

如何对 JPanel 的颜色进行编码。我的想法是在主 JPanel 上添加 5 个 JPanel(上面显示的 5 个块)。将每个 JPanel 的背景设置为浅灰色

但是我怎样才能实现如图所示的深色线条呢?

有什么提示或建议吗?

I want to design a JPanel which should have the color coding as shown in the following diagram:


(source: compendiumblog.com)

How can I code the colors of a JPanel. What I think is that add 5 JPanels (for 5 blocks shown above) on a main JPanel. Set the background of each JPanel to light Gray.

But then how can I achieve the dark color lines as shown in the diagram.

Any hints or suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

白馒头 2024-08-31 16:34:37

尝试使用 JTable,然后交替行的颜色。通过这种方式,您可以编写通用 JComponent (AlternatingColorTable) 并像这 4 个面板中的常规 JTable 一样使用它。

也许是这样的:

public class AlternatingColorTable extends JTable {

public AlternatingColorTable () {
    super();
}

public AlternatingColorTable(TableModel tableModel) {
    super(tableModel);
}

/** Extends the renderer to alternate row colors */
public Component prepareRenderer(TableCellRenderer renderer, int row, int col) {
    Component returnComp = super.prepareRenderer(renderer, row, col);

    Color alternateColor = Color.GRAY;
    Color mainColor = Color.DARK_GRAY;

    if (!returnComp.getBackground().equals(getSelectionBackground())) {
        Color background = (row % 2 == 0 ? alternateColor : mainColor );
        returnComp.setBackground(background);
        background = null;
    }
    return returnComp;
}

}

Try using a JTable and then alternating the colors of the row. This way you can write a generic JComponent (AlternatingColorTable) and use it just like a regular JTable in those 4 panels.

Something like this maybe:

public class AlternatingColorTable extends JTable {

public AlternatingColorTable () {
    super();
}

public AlternatingColorTable(TableModel tableModel) {
    super(tableModel);
}

/** Extends the renderer to alternate row colors */
public Component prepareRenderer(TableCellRenderer renderer, int row, int col) {
    Component returnComp = super.prepareRenderer(renderer, row, col);

    Color alternateColor = Color.GRAY;
    Color mainColor = Color.DARK_GRAY;

    if (!returnComp.getBackground().equals(getSelectionBackground())) {
        Color background = (row % 2 == 0 ? alternateColor : mainColor );
        returnComp.setBackground(background);
        background = null;
    }
    return returnComp;
}

}

梦里梦着梦中梦 2024-08-31 16:34:37

只需将每个彩色条本身的面板设置为不同的背景颜色即可。不要忘记使用 setOpaque(true) 使面板显式不透明 - 默认情况下,面板在大多数外观和感觉中都是透明的。

关于美学的注释;我会从每组中的第一行以不同的阴影开始。

Just make each of the colored bars themselves panels with a different background color. Don't forget to make the panels explicitly opaque with setOpaque(true) - panels are transparent by default transparent in most look and feels.

A note on aesthetics; I would start with the first line in each group shaded differently.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文