开发者

Java Swing - Background of a JPanel

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

Java Swing - Background of a JPanel

(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?


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;
}

}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜