Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
429 views
in Technique[技术] by (71.8m points)

java - 单击按钮时如何显示颜色选择器?(How to display a color selector when clicking a button?)

I'm making a program that draws some shapes and fills them with color.

(我正在编写一个程序,绘制一些形状并用颜色填充它们。)

I need to change the 'line' color, and want the user to be able to select the color.

(我需要更改“线条”颜色,并希望用户能够选择颜色。)

How can I, when clicking a button "Choose Color", have a set of colours appear below the button?

(单击按钮“选择颜色”时,如何在按钮下方显示一组颜色?)

Is it possible for the selector to be embedded in the UI below the button (and not pop up in a window)?

(选择器是否可以嵌入在按钮下方的UI中(而不在窗口中弹出)?)

I want to display a color selector like in Paint.

(我想显示一个颜色选择器,如在Paint中。)

  ask by Tito translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use the JColorChooser like this:

(您可以像这样使用JColorChooser :)

Color newColor = JColorChooser.showDialog(null, "Choose a color", Color.RED);
  • The first argument is the parent java.awt.Component instance.

    (第一个参数是父java.awt.Component实例。)

    Could also be null .

    (也可以为null 。)

  • The second argument is the title for the dialog.

    (第二个参数是对话框的标题。)

  • The third argument is the color it should select as default.

    (第三个参数是它应该选择的默认颜色。)

The dialog returns the selected color if the user presses ok or null if he clicked on cancel .

(如果用户按下ok ,对话框将返回选定的颜色;如果单击cancel则对话框将返回null 。)

See this page for more information: http://docs.oracle.com/javase/tutorial/uiswing/components/colorchooser.html .

(有关更多信息,请参见本页: http : //docs.oracle.com/javase/tutorial/uiswing/components/colorchooser.html 。)

Edit: include ColorChooser into existing contentpane

(编辑:将ColorChooser包含到现有contentpane中)

The above code shows how to create a pop up with for the JColorChooser , but it is also possible to "include" it into the existing contentpane.

(上面的代码显示了如何使用JColorChooser创建一个弹出窗口,但是也可以将其“包含”到现有的内容窗格中。)

This is the code to initialize both components ( JButton and JColorChooser ):

(这是初始化两个组件( JButtonJColorChooser )的代码:)

button = new JButton("Choose color");
button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        toggleColorChooser(); // show and hide the color chooser
    }
});
button.setBounds(10, 11, 150, 23);
contentPane.add(button);

colorChooser = new JColorChooser(Color.BLACK); // default color is black
colorChooser.setBorder(null);
colorChooser.getSelectionModel().addChangeListener(new ChangeListener() {
    public void stateChanged(ChangeEvent e) {
        colorChanged(); // change background color of "button"
    }
});

The button will be added immediately, but the color chooser not yet.

(该按钮将立即添加,但颜色选择器尚未添加。)

It will be added in the toggleColorChooser method:

(它将添加到toggleColorChooser方法中:)

protected void toggleColorChooser() {
    if (toggled) {
        contentPane.remove(colorChooser);
    } else {
        colorChooser.setBounds(button.getX(), button.getY() + 20, 600, 300);
        colorChooser.setVisible(true);
        contentPane.add(colorChooser);
    }
    toggled = !toggled;
    contentPane.validate();
    contentPane.repaint();
}

The color chooser will be added to the panel beneath the button.

(颜色选择器将添加到按钮下方的面板中。)

You may change the bounds if you have a different layout or if you're using a layout manager.

(如果您使用其他布局或使用布局管理器,则可以更改边界。)

As you can see, you'll need a variable called toggled .

(如您所见,您将需要一个名为toggled的变量。)

Just add it as class variable:

(只需将其添加为类变量:)

private boolean toggled = false;

The last method will be called it the user selects a color on the color chooser.

(用户在颜色选择器上选择一种颜色时,将调用最后一个方法。)

It will change the background color of the button:

(它将更改按钮的背景颜色:)

protected void colorChanged() {
    button.setBackground(colorChooser.getSelectionModel().getSelectedColor());
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...