How to set SWT button foreground color?

前端 未结 4 1810
后悔当初
后悔当初 2020-12-02 02:03

The SWT Button class has a setForeground(Color) method but it seems to have no effect (the method is actually on Button\'s superclass). The javadoc says that th

4条回答
  •  萌比男神i
    2020-12-02 02:40

    This is the code to Implement FOreground Color in Buttons in SWT with allowing Mnemonic key to be Shown also and Enabled by Pressing Alt+"Mnemonic Key";

    Button radioButton=new Button(parent,SWT.RADIO);
    StringBuffer sb = new StringBuffer("I am a Coloured radio button");
    String name=null;
     String S = "I am a Coloured radio button";
    String substr="C";
    int i=S.indexOf(substr);
    sb.insert(i,"&");
    S=sb.toString();
    name=sb.substring(i, i+2);
    name=sb.toString();
    String whiteSpace=" ";
    final String TName=S;
    
    for(int l=0;l<1000;l++)
        whiteSpace=whiteSpace.concat("            ");
    
    radioButton.setText(name+whiteSpace);
    
    radioButton.addPaintListener(new PaintListener(){
     String name=TName;
        @Override
        public void paintControl(PaintEvent e) {
            // TODO Auto-generated method stub
            e.gc.setForeground(hex2Col("ffffcc"));
            int x=21;
            int y=21;
            e.gc.drawText(name, x,y,SWT.DRAW_MNEMONIC | SWT.TRANSPARENT);
    
        }
    
    });
    

    Note: hex2Col is my own method to Convert hex Color Code to Color Type

    Note: Here ALT+C is Mnemonic Key Combination i have Used

提交回复
热议问题