切换按钮背景颜色变化

JToggle Button background color change

这是我的代码,其中我实现了一个名为“start/stop”的按钮“

这是我的代码......

//Buttons for start and stop 
btnStartStop = new JToggleButton("Start/Stop");
//btnStartStop.setEnabled(false);
btnStartStop.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        if (chk_start) {
              tout_textPane.setText("Stop Magnetic Levitation Project");
              btnStartStop.setText("start");
              btnStartStop.setBackground(new Color(170, 255, 0));
              btnStartStop.setForeground(Color.WHITE);
              btnStartStop.setSelected(false);
              CommandHandler(".plot");
              chk_start = false;
          } else {
                 tout_textPane.setText("Starting Magnetic Levitation Project");
                 btnStartStop.setText("stop");
                 btnStartStop.setBackground(Color.RED);
                 btnStartStop.setForeground(Color.WHITE);
                 btnStartStop.setSelected(true); 
                 chk_start = true;
              }
        CommandHandler(".isr");
        }
    });
btnStartStop.setBounds(27, 10, 210, 21);
panel_ctrl_i.add(btnStartStop);

I tried both command btnStartStop.setBorderPainted(false); btnStartStop.setOpaque(false); But still not working

对我来说效果很好,不,你似乎无法摆脱“选定”的突出显示,这是由外观控制的东西,那完全是一团糟

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;

public class Main {
    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GradientButton btn = new GradientButton();
            btn.setOpaque(true);
            btn.setBorderPainted(false);
            btn.setBackground(Color.RED);
            add(btn);

            btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (btn.isSelected()) {
                        btn.setBackground(Color.GREEN);
                    } else {
                        btn.setBackground(Color.RED);
                    }
                }
            });
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.dispose();
        }

    }

    private static class GradientButton extends JToggleButton {
        private GradientButton() {
            super("Gradient Button");
            setContentAreaFilled(false);
            setFocusPainted(false); // used for demonstration
        }

        @Override
        protected void paintComponent(Graphics g) {
            final Graphics2D g2 = (Graphics2D) g.create();
            g2.setPaint(new GradientPaint(
                    new Point(0, 0),
                    Color.WHITE,
                    new Point(0, getHeight()),
                    Color.PINK.darker()));
            g2.fillRect(0, 0, getWidth(), getHeight());
            g2.dispose();

            super.paintComponent(g);
        }
    }
}

你也应该在 jbutton change background 上搜索一下,因为它基本上是同一个问题