java 选择颜色_java中的颜色选择器的使用

package colorChooser;

import java.awt.Color;

import java.awt.Frame;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

public class ColorChooserPanelTest {

public static void main(String[] args) {

ColorChooserFrame cc=new ColorChooserFrame();

cc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

cc.setVisible(true);

}

}

class ColorChooserFrame extends JFrame{

public ColorChooserFrame(){

setSize(400,300);

add(new ColorChooserPanel());

pack();

}

}

class ColorChooserPanel extends JPanel{

public ColorChooserPanel(){

JButton modalButton=new JButton("Modal");

modalButton.addActionListener(new ModalListener());

add(modalButton);

JButton modalessButton=new JButton("Modaless");

modalessButton.addActionListener(new

ModalessListener());

add(modalessButton);

JButton immediateButton=new JButton("Immediate");

immediateButton.addActionListener(new

ImmediateListener());

add(immediateButton);

}

private class ModalListener implements ActionListener{

public void actionPerformed(ActionEvent event){

Color defaultColor=getBackground();

Color

selected=JColorChooser.showDialog(ColorChooserPanel.this, "Set

background",

defaultColor);

if(selected!=null)setBackground(selected);

}

}

private class ModalessListener implements

ActionListener{

private JDialog dialog;

private JColorChooser chooser;

public ModalessListener(){

chooser=new JColorChooser();

dialog=JColorChooser.createDialog(ColorChooserPanel.this,

"Background",

false, chooser, new ActionListener()//OK button listener

{

public void actionPerformed(ActionEvent event){

setBackground(chooser.getColor());

}

}, null);

}

public void actionPerformed(ActionEvent event){

chooser.setColor(getBackground());

dialog.setVisible(true);

}

}

private class ImmediateListener implements

ActionListener{

private JDialog dialog;

private JColorChooser chooser;

public ImmediateListener(){

chooser=new JColorChooser();

chooser.getSelectionModel().addChangeListener(new

ChangeListener()

{

public void stateChanged(ChangeEvent event){

setBackground(chooser.getColor());

}

});

dialog=new JDialog((Frame)null,false);

dialog.add(chooser);

dialog.pack();

}

public void actionPerformed(ActionEvent event){

chooser.setColor(getBackground());

dialog.setVisible(true);

}

}

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,对于你的问题,我可以给出以下代码示例来实现Java选择菜单选项完成背景颜色更改: ``` import javax.swing.*; import java.awt.*; import java.awt.event.*; public class BackgroundColorChanger extends JFrame implements ActionListener { private JMenuBar menuBar; private JMenu menu; private JMenuItem menuItemRed, menuItemGreen, menuItemBlue, menuItemYellow, menuItemDefault; public BackgroundColorChanger() { super("Background Color Changer"); // 创建菜单选项 menuBar = new JMenuBar(); menu = new JMenu("Background Color"); menuItemRed = new JMenuItem("Red"); menuItemGreen = new JMenuItem("Green"); menuItemBlue = new JMenuItem("Blue"); menuItemYellow = new JMenuItem("Yellow"); menuItemDefault = new JMenuItem("Default"); // 将菜单选项添加到菜单 menu.add(menuItemRed); menu.add(menuItemGreen); menu.add(menuItemBlue); menu.add(menuItemYellow); menu.addSeparator(); menu.add(menuItemDefault); // 将菜单添加到菜单栏 menuBar.add(menu); // 将菜单栏设置到窗口上 setJMenuBar(menuBar); // 添加事件监听 menuItemRed.addActionListener(this); menuItemGreen.addActionListener(this); menuItemBlue.addActionListener(this); menuItemYellow.addActionListener(this); menuItemDefault.addActionListener(this); // 设置窗口的大小、位置和关闭方式 setSize(300, 200); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } @Override public void actionPerformed(ActionEvent e) { // 根据不同的选择,设置不同的背景颜色 if (e.getSource() == menuItemRed) { getContentPane().setBackground(Color.RED); } else if (e.getSource() == menuItemGreen) { getContentPane().setBackground(Color.GREEN); } else if (e.getSource() == menuItemBlue) { getContentPane().setBackground(Color.BLUE); } else if (e.getSource() == menuItemYellow) { getContentPane().setBackground(Color.YELLOW); } else if (e.getSource() == menuItemDefault) { getContentPane().setBackground(null); } } public static void main(String[] args) { new BackgroundColorChanger(); } } ``` 当用户点击菜单选项时,程序会根据选择来设置不同的背景颜色。如果需要设置其他属性,可以根据类库提供的相关方法进行设置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值