我正在尝试制作一个具有半透明背景但数字完全不透明的小时钟。 setAlpha()方法的任何使用都会导致外壳及其中的所有东西变得半透明。在SWT中,有没有办法即使外壳的Alpha = 100,也强制标签的Alpha = 255?如果没有,在其他相对简单的GUI库中有没有办法做到这一点?

public class SystemClock {
    Label labelHours;
    Label labelMinutes;
    Label labelSeconds;
    Label labelMeridian;
    boolean pause = false;
    Display display;
    Runnable repeat = new Runnable(){
        public void run(){
                if( !shlClock.isDisposed())
                    pullClock();
                    display.timerExec( 1000, this );
        }
    };
    protected Shell shlClock;

    public static void main(String[] args) {
        try {
            SystemClock window = new SystemClock();
            window.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void open() {
        display = Display.getDefault();
        createContents();
        shlClock.open();
        shlClock.layout();
        repeatClock();
        while (!shlClock.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }

    protected void createContents() {
        shlClock = new Shell(SWT.NO_TRIM | SWT.ON_TOP);
        shlClock.setModified(true);
        shlClock.setBackgroundMode(SWT.INHERIT_FORCE);
        shlClock.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                if(e.keyCode == SWT.ESC){
                    System.exit(0);
                }else if(e.keyCode == 80 || e.keyCode == 112){
                    pause = !pause;
                        if(pause){
                            display.timerExec(-1,repeat);
                        }else{
                            display.timerExec(100,repeat);
                        }
                }
            }
        });
        shlClock.setBackground(SWTResourceManager.getColor(0, 0, 0));
        shlClock.setSize(95, 25);
        shlClock.setAlpha(180);
        shlClock.setLocation((1366-95),250);
        shlClock.setText("Clock");
        shlClock.setLayout(null);

        Region overShell = new Region();
        Region minuteShell = new Region();
        Region hoursShell = new Region();
        Region secondShell = new Region();
        Region colon1Shell = new Region();
        Region colon2Shell = new Region();
        Region meridianShell = new Region();

        Rectangle overShellRec = new Rectangle(0,0,95,25);
        Rectangle overShellRecSub = new Rectangle(2,2,91,21);
        Rectangle innerRec = new Rectangle(5,5,86,16);


        overShell.add(overShellRec);
        overShell.subtract(overShellRecSub);
        overShell.add(innerRec);

        shlClock.setRegion(overShell);
        Rectangle size = overShell.getBounds();
        shlClock.setSize(size.width, size.height);

        Label labelColonHoursMinutes = new Label(shlClock, SWT.ALPHA);
        labelColonHoursMinutes.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
        labelColonHoursMinutes.setBounds(20, 5, 3, 15);
        labelColonHoursMinutes.setText(":");

        Label labelColonMinutesSeconds = new Label(shlClock, SWT.ALPHA);
        labelColonMinutesSeconds.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
        labelColonMinutesSeconds.setBounds(41, 5, 3, 15);
        labelColonMinutesSeconds.setText(":");

        labelMinutes = new Label(shlClock, SWT.NONE);
        labelMinutes.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
        labelMinutes.setBounds(26, 5, 14, 12);
        labelMinutes.setText("00");

        labelHours = new Label(shlClock, SWT.RIGHT);
        labelHours.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
        labelHours.setBounds(5, 5, 14, 12);
        labelHours.setText("00");

        labelSeconds = new Label(shlClock, SWT.NONE);
        labelSeconds.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
        labelSeconds.setBounds(47, 5, 14, 12);
        labelSeconds.setText("00");

        labelMeridian = new Label(shlClock, SWT.NONE);
        labelMeridian.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
        labelMeridian.setBounds(65, 5, 21, 12);
        labelMeridian.setText("AM");

    }


public void pullClock(){
        long currentTime = System.currentTimeMillis();
        int hours;
        int minutes;
        int seconds;
        String hoursStr;
        String minutesStr;
        String secondsStr;
        String meridian;
            currentTime/=1000;
            seconds = (int)currentTime%60;
            currentTime/=60;
            minutes = (int)currentTime%60;
            currentTime/=60;
            hours = (int)currentTime%24;
            hours -=4;

            switch(hours){
                case -4: hoursStr="8"; meridian = "PM"; break;
                case -3: hoursStr="9"; meridian = "PM"; break;
                case -2: hoursStr="10"; meridian = "PM"; break;
                case -1: hoursStr="11"; meridian = "PM"; break;
                case  0: hoursStr="12"; meridian = "AM"; break;
                case 12: hoursStr="12"; meridian = "PM"; break;
                case 13: hoursStr="1"; meridian = "PM"; break;
                case 14: hoursStr="2"; meridian = "PM"; break;
                case 15: hoursStr="3"; meridian = "PM"; break;
                case 16: hoursStr="4"; meridian = "PM"; break;
                case 17: hoursStr="5"; meridian = "PM"; break;
                case 18: hoursStr="6"; meridian = "PM"; break;
                case 19: hoursStr="7"; meridian = "PM"; break;
                default: hoursStr=String.valueOf(hours); meridian = "AM";
            }
            secondsStr=String.valueOf(seconds);
            minutesStr=String.valueOf(minutes);
            labelHours.setText(hoursStr);
            labelMinutes.setText((minutes<10) ? "0"+minutesStr:minutesStr);
            labelSeconds.setText((seconds<10) ? "0"+secondsStr:secondsStr);
            labelMeridian.setText(meridian);
        }
public void repeatClock(){
    display.timerExec( 10, repeat);
    }
}


我正在尝试区域,因为这是我能够找到的大多数类似问题所推荐的,但是似乎无法将标签放在透明部分中。

最佳答案

4.5 M5 (search for 'transparent')开始,SWT支持具有透明背景的控件。如果将控件背景设置为透明,则其父级的颜色将可见。

label.setBackground( display.getSystemColor( SWT.COLOR_TRANSPARENT ) );


有关更多详细信息,请参见相应的snippet

08-15 21:17