⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 editcolor.java

📁 此类为”替换对话框“所需的类
💻 JAVA
字号:
/*************************************************************************************
* 类名: EditColor                                                               
*                                                                    
* 功能: 此类的主要功能为“颜色选择对话框”类,主要用于用户自定义颜色使用 
*                                                                
**************************************************************************************/
package edit.com;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;

class EditColor extends JPanel {
    private JPanel centerPanel;
    private JPanel southPanel;
    private JDialog dialog;
    private JButton[] buttons = new JButton[2]; //“OK"和”CANCEL"按钮
    private JLabel[] labels = new JLabel[4];
    private JSlider[] sliders = new JSlider[4]; //颜色滑动条
    private JTextField[] texts = new JTextField[4];//颜色滑动条想对应的文本输入框
    private JTextArea showText;
    private JRadioButton[] selectColorButtons = new JRadioButton[2] ;//前景和背景单选按钮
    private JRadioButton[] colorButtons = new JRadioButton[2];//“RGB”颜色模式和黑白颜色模式单选按钮
    private JButton setButton;
    private EditColorTools tool = new EditColorTool();
    private ColorSet colorSet = new ColorSet();
    private EditColorState state = new EditColorState(); //一些反映类状态的类成员变量的封装

    public EditColor() {
        setLayout( new BorderLayout() );

        southPanel = new JPanel();
        buttons[0] = tool.createButton( new OkButtonAction( "   O k   " ) ,
                                        Color.orange );
        buttons[1] = tool.createButton( new CancelButtonAction( "Cancel" ) ,
                                        Color.orange );
        tool.addSouthPanel( buttons , southPanel );
        add( southPanel , BorderLayout.SOUTH );

        centerPanel = new JPanel();
        addCenterPanel( centerPanel );
    }

    //生成“颜色选择对话框”    
    public boolean showDialog( Component parent , String title ) {
        state.setFlag( false );

        Frame owner = null;
        if ( parent instanceof Frame ) {
            owner = ( Frame )parent;
        }else {
         owner = ( Frame )SwingUtilities.getAncestorOfClass( Frame.class , parent );
        }

        if ( dialog == null || dialog.getOwner() != owner ) {
            owner = null;
            dialog = new JDialog( owner , true );
            dialog.getContentPane().add( EditColor.this );
            dialog.setResizable( false );
            dialog.setSize( 300 , 450 );
        }

        dialog.setTitle( title );
        dialog.show();
        return state.getFlag();
    }
    
    public ColorSet getValues()	{
        return colorSet;
    }

/*****************************************
private class or private function defined
******************************************/
    private void addCenterPanel( JPanel aPanel ) {
        aPanel.setLayout( new GridBagLayout() );
        GridBagConstraints constraints = new GridBagConstraints();

        showText = new JTextArea( 6 , 12 );
        showText.setBackground( Color.black );
        showText.setEditable( false );
        tool.addToCenterWithConstraint( showText , constraints , aPanel ,
                                        1 , 0 , 1 , 1 , GridBagConstraints.NONE ,
                                        GridBagConstraints.WEST );

        Box temBox = tool.createVerticalBox( 12 );
        tool.addToCenterWithConstraint( temBox , constraints , aPanel ,
                                        1 , 1 ,2 , 1 , GridBagConstraints.NONE ,
                                        GridBagConstraints.WEST );

        int x = 0;
        int y = 2;
        int width = 1;
        int height = 1;
        int fill = GridBagConstraints.NONE;
        int anchor = GridBagConstraints.WEST;
        String[] labelNames = { " R " , " G " , " B " , " S " };

        //滑动条前标签的生成
        for ( int i = 0 ; i < 4 ; i++ ) {
            labels[i] = new JLabel( labelNames[i] );
            tool.addToCenterWithConstraint( labels[i] , constraints , aPanel ,
                                            x , y++ , width , height , fill , anchor );
        }

        Box temBox2 = tool.createVerticalBox( 7 );
        tool.addToCenterWithConstraint( temBox2 , constraints , aPanel ,
                                        0 , 7 , 3 , 1 , GridBagConstraints.NONE ,
                                        GridBagConstraints.WEST );

        //背景选择单选按钮的生成
        String[] selectText = { "Background" , "Foreground" };
        selectColorButtons = tool.createColorButtons( selectText );
        tool.addBackgroundListener( selectColorButtons , state );
        Box selectColorBox = tool.createColorButtonsBox( selectColorButtons , 13 );
        selectColorButtons[0].setSelected( true );
        tool.addToCenterWithConstraint( selectColorBox , constraints , aPanel ,
                                        0 , 8 , 3 , 1 , GridBagConstraints.NONE , GridBagConstraints.WEST );

        //“RGB”颜色模式和黑白颜色模式单选按钮的生成
        String[] colorText = { "Rgb_Modal" , "S_Modal" };
        colorButtons = tool.createColorButtons( colorText );
        tool.addColorTextListener( sliders , texts , showText , colorButtons ,
                                   state );
        Box colorBox = tool.createColorButtonsBox( colorButtons , 18 );
        colorButtons[0].setSelected( true );
        tool.addToCenterWithConstraint( colorBox , constraints , aPanel ,
                                        0 , 9 , 3 , 1 , GridBagConstraints.NONE , GridBagConstraints.WEST );

        x = 1;
        y = 2;
        width = 1;
        height = 1;
        fill = GridBagConstraints.NONE;
        anchor = GridBagConstraints.WEST;

        //滑动条的生成        
        for ( int i = 0; i < 4; i++ ) {
            sliders[i] = tool.createSlider( sliders , texts , showText , state );

            tool.addToCenterWithConstraint( sliders[i] , constraints , aPanel ,
                                            x , y++ , width , height , fill , anchor );
        }

        sliders[3].setEnabled( false );

        //“颜色选择对话框”中的”颜色选择按钮区域“的生成
        ButtonPanel buttonPanel = new ButtonPanel( sliders        ,
                                                   texts          ,
                                                   EditColor.this ,
                                                   showText       ,
                                                   colorButtons );
        Box buttonBox = Box.createVerticalBox();
        buttonBox.add( buttonPanel );
        tool.addToCenterWithConstraint( buttonBox , constraints , aPanel ,
                                        2 , 0 , 1 , 1 , GridBagConstraints.NONE , GridBagConstraints.WEST );

        x = 2;
        y = 2;
        width = 1;
        height = 1;
        fill = GridBagConstraints.NONE;
        anchor = GridBagConstraints.WEST;

        //滑动条相对应文本输入框的生成
        for ( int i = 0 ; i < 4 ; i++ ) {
            texts[i] = tool.createText();
            tool.addToCenterWithConstraint( texts[i] , constraints , aPanel ,
                                            x , y++ , width , height , fill , anchor );
	    }

        texts[3].setEditable( false );
        texts[3].setEnabled( false );
        add( aPanel , BorderLayout.CENTER );
    }

//private class defined
    //”OK“按钮所用的Action
    private class OkButtonAction extends AbstractAction	{
        public OkButtonAction( String name ) {
            putValue( Action.NAME , name );
        }

        public void actionPerformed( ActionEvent e ) {
            state.setFlag( true );

            colorSet.setType( state.getColorType() );
            colorSet.setRS( state.getRsModal() );
            colorSet.setR( state.getRofRGBS() );
            colorSet.setG( state.getGofRGBS() );
            colorSet.setB( state.getBofRGBS() );
            colorSet.setS( state.getSofRGBS() );

            dialog.setVisible( false );
        }
    }

    //"Cancel"按钮所用的Action
    private class CancelButtonAction extends AbstractAction {
        public CancelButtonAction( String name ) {
            putValue( Action.NAME , name );
        }

        public void actionPerformed( ActionEvent e ) {
            dialog.setVisible( false );
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -