java - 为什么我的文本颜色不再变为白色?

标签 java swing jframe jlabel linear-gradients

text color

图片值一千字。
添加渐变背景后,我使用JLabel 创建了我的 GUI。
我使用 setPaint(gr) 将颜色更改为所需的渐变,然后再次使用 setPaint(Color.white) 将颜色更改为白色。
然后我使用 setupGUI() 创建所有你看到的按钮。
然而,它们是灰色的,而不是白色的。
如何让它变白

public class ScreenRecorder extends JFrame{
    public ScreenRecorder(){
        setupGUI();
        this.getContentPane().add(b);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setResizable(false);
        this.pack();
    }

    /*This method is used to add components into each other, setting layout, etc*/
    public void setupGUI(){
        play.setPreferredSize(new Dimension(24,24));
        Box controls = Box.createHorizontalBox();
        controls.add(play);
        controls.add(Box.createHorizontalStrut(10));
        controls.add(timerLabel);
        controls.add(Box.createHorizontalStrut(200));
        controls.add(locationLabel);
        b.add(controls);
    }
    /* Creating the gradient background */
    protected class Background extends JPanel{
        @Override
        protected void paintComponent(Graphics g){
            Graphics2D g2D = (Graphics2D) g;
            int w = this.getWidth();
            int h = this.getHeight();
            Color color1 = Color.black;
            Color color2 = Color.DARK_GRAY.brighter();
            GradientPaint gr = new GradientPaint(0,0,color1,0,w/2,color2);
            g2D.setPaint(gr); //Color changes to the gradient color
            g2D.fillRect(0, 0, w, h);
            g2D.setPaint(Color.white); //Color changes to white for JLabel, but doesn't actually change
        }
    }
    /*Creating the combined Play and Pause button using AbstractAction 
     * The displayed icon must change from that of play to pause on click and vice versa*/
    protected class PlayAction extends AbstractAction{
        public PlayAction(){
            this.putValue(AbstractAction.LARGE_ICON_KEY, playIcon);
            this.putValue(SHORT_DESCRIPTION, "Click to Record Video");
        }
        @Override
        public void actionPerformed(ActionEvent e){
            if(isPlay == true){
                isPlay = false;
                play.getAction().putValue(LARGE_ICON_KEY, pauseIcon);
                play.getAction().putValue(SHORT_DESCRIPTION,"Click to Pause Recording");
                ScreenRecorder.this.setState(JFrame.ICONIFIED);
                play.repaint();
            }else{
                isPlay = true;
                play.getAction().putValue(LARGE_ICON_KEY, playIcon);
                play.getAction().putValue(SHORT_DESCRIPTION,"Click to Start Recording");
                play.repaint();
            }
        }
        boolean isPlay = true; //true represents recorder is ready, false means it is currently recording
        Icon playIcon = (Icon) new ImageIcon("src/images/play.png");
        Icon pauseIcon = (Icon) new ImageIcon("src/images/pause.png");
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run(){
                new ScreenRecorder();
            }
        });
    }
    Background b = new Background();
    JButton play = new JButton(new PlayAction());
    JLabel timerLabel = new JLabel("00:00",JLabel.CENTER);
    JLabel locationLabel = new JLabel("Into: ",JLabel.LEFT);
}

最佳答案

enter image description here

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

public class JavaGui129 extends JFrame{
    public JavaGui129(){
        setupGUI();
        this.getContentPane().add(b);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setResizable(false);
        this.pack();
    }

    /*This method is used to add components into each other, setting layout, etc*/
    public void setupGUI(){
        //play.setPreferredSize(new Dimension(24,24));
        Box controls = Box.createHorizontalBox();
        controls.add(play);
        controls.add(Box.createHorizontalStrut(10));
        // Red letter day..
        timerLabel.setForeground(Color.RED);
        controls.add(timerLabel);
        controls.add(Box.createHorizontalStrut(200));
        locationLabel.setForeground(Color.GREEN);
        controls.add(locationLabel);
        b.add(controls);
    }
    /* Creating the gradient background */
    protected class Background extends JPanel{
        @Override
        protected void paintComponent(Graphics g){
            // very important! 
            super.paintComponent(g);
            Graphics2D g2D = (Graphics2D) g;
            int w = this.getWidth();
            int h = this.getHeight();
            Color color1 = Color.black;
            Color color2 = Color.DARK_GRAY.brighter();
            GradientPaint gr = new GradientPaint(0,0,color1,0,w/2,color2);
            g2D.setPaint(gr); //Color changes to the gradient color
            g2D.fillRect(0, 0, w, h);
            // irrelevant now
            //g2D.setPaint(Color.white); //Color changes to white for JLabel, but doesn't actually change
        }

        @Override 
        public Dimension getPreferredSize() {
            return (new Dimension(400,100));
        }
    }
    /*Creating the combined Play and Pause button using AbstractAction 
     * The displayed icon must change from that of play to pause on click and vice versa*/
    protected class PlayAction extends AbstractAction{
        public PlayAction(){
            this.putValue(AbstractAction.LARGE_ICON_KEY, playIcon);
            this.putValue(SHORT_DESCRIPTION, "Click to Record Video");
        }
        @Override
        public void actionPerformed(ActionEvent e){
            if(isPlay == true){
                isPlay = false;
                play.getAction().putValue(LARGE_ICON_KEY, pauseIcon);
                play.getAction().putValue(SHORT_DESCRIPTION,"Click to Pause Recording");
                JavaGui129.this.setState(JFrame.ICONIFIED);
                play.repaint();
            }else{
                isPlay = true;
                play.getAction().putValue(LARGE_ICON_KEY, playIcon);
                play.getAction().putValue(SHORT_DESCRIPTION,"Click to Start Recording");
                play.repaint();
            }
        }
        boolean isPlay = true; //true represents recorder is ready, false means it is currently recording
        Icon playIcon = (Icon) new ImageIcon("src/images/play.png");
        Icon pauseIcon = (Icon) new ImageIcon("src/images/pause.png");
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run(){
                new JavaGui129();
            }
        });
    }
    Background b = new Background();
    JButton play = new JButton(new PlayAction());
    JLabel timerLabel = new JLabel("00:00",JLabel.CENTER);
    JLabel locationLabel = new JLabel("Into: ",JLabel.LEFT);
}

关于java - 为什么我的文本颜色不再变为白色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13928279/

相关文章:

java - 试图在 Java 中制作一个模态 "Generating Report"对话框

java - 使用装饰器模式装饰 JLabel

java - 填写表单后从 JFrame 获取数据

java - 从 C 驱动器以 Java 代码加载图像

Java 数组反转不起作用

Java Calendar.DAY_OF_YEAR 有时差一个

java - 给出 0 到 15 之间的值,返回 4 CheckBoxes 之间的正确选择

java - 我可以在后台运行 Autoit 脚本来上传文件吗

java - 动态改变JDialog的宽度

java - 使用 for 循环将 JPanel 添加到 JFrame