Form layout to call JColorChooser

2019-07-30 01:47发布

I'm writing a Java Swing program that needs to enable the user to select 8 different colors from a Settings form. This is quite straightforward as far as it goes with JColorChooser, but I'm wondering is there a recommended way to lay out the form. This is what I'm currently contemplating:

  1. For each color,

  2. Display a square in the current selected color,

  3. Display to the right of that, a button that will bring up the JColorChooser to change the selected color.

However, I suspect the 8 buttons will make the form look clunky if not downright ugly (particularly if more get added later). An alternative approach might be to make the square double as the button; in that case, how to visually indicate it's clickable?

Is there an alternative recommended method?

2条回答
啃猪蹄的小仙女
2楼-- · 2019-07-30 01:52

Here is a ColorButton that I use in the pscode API.

package org.pscode.ui.widget;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.*;

/** Provides a button for popping a color chooser.
The Color can be obtained by calling ColorButton.getBackground().
@author Andrew Thompson. */
public class ColorButton extends JButton implements ActionListener {

    private JColorChooser chooseColor;
    private String htmlColor;

    private String text;

    public ColorButton(String label) {
        super(label);
        text = label;
        chooseColor = new JColorChooser();
        addActionListener( this );
    }

    @Override
    public void setText(String text) {
        super.setText(
            "<html><body style='text-align: left;'><center>" +
            "<p>" +
            text +
            "<br>" +
            "<span style='width: 5px; height: 5px; " +
            "border: solid 1px black; background-color: " +
            htmlColor +
            "; color: " +
            htmlColor +
            ";'>AA</span>  " +
            "</center></body></html>");
        this.text = text;
    }

    public void actionPerformed(ActionEvent ae) {
        Color c = chooseColor.showDialog(
            this,
            "Choose Color",
            getBackground() );
        if (c!=null) {
            setBackground(c);
        }
    }

    @Override
    public void setBackground(Color bg) {
        super.setBackground( new Color(bg.getRed(), bg.getGreen(), bg.getBlue()) );
        int r = bg.getRed();
        int g = bg.getGreen();
        int b = bg.getBlue();

        htmlColor = "rgb(" + r + "," + g + "," + b + ")";
        setText(text);
    }
}

Screenshot

Screenshot of typical usage.

Color scheme chooser using ColorButtons

In Other News..

@rwallace: how did you get them to be all the same shape (regardless of length of caption)..

That part of the layout (for the 5 buttons and check box) is using a GridLayout. You might also alter the size of the buttons by:

  1. Setting the preferred size (dangerous to presume we can second guess it).
  2. Adding some padding in the style for the body element of the HTML.

"..and with the slightly rounded corners and shiny effect?"

DukeBox (from which that screenshot was obtained) uses the native PLAF - I'm running Windows 7.

查看更多
forever°为你锁心
3楼-- · 2019-07-30 02:04

As an alternative, consider implementing the Icon interface and using the icon on the button. Together, PieceButton and ColorIcon comprise an example.

查看更多
登录 后发表回答