我想用一个透明的背景图标制作JButton,它可以响应三种模式并更改其相应的图标:
我已经设置了这些图标,但是当我每次翻转变成黑色和黑色时,以及当我按下按钮时,背景变得非常难看(绘制一些文字或其他按钮背景等)。
如果你知道这个问题,请告诉我这里有什么问题。 有关详细信息,我上传了一些图片:
正常时 按下或翻转时 多次翻转时 按下或翻转时我只是希望没有翻转或按下时背景图标正常,翻转时变为浅蓝色,按下时变成全蓝色,我已经设置了这些图标,我使用的是Intellij IDE GUI表单设计器。
已添加代码:
public class TransparentTest extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
TransparentTest frame = new TransparentTest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public TransparentTest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
JPanel contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
//----------------------------------------------------------
JLayeredPane layeredPane = new JLayeredPane();
contentPane.add(layeredPane, BorderLayout.CENTER);
//----------------------------------------------------------
JPanel mailJPanel = new JPanel();
mailJPanel.setBackground(Color.RED);
mailJPanel.setBounds(0, 0, 422, 243);
layeredPane.add(mailJPanel);
//----------------------------------------------------------
JPanel bottomJPanel = new JPanel();
//panel_1.setOpaque(false);
bottomJPanel.setBounds(0, 195, 422, 48);
layeredPane.setLayer(bottomJPanel, 1);
layeredPane.add(bottomJPanel);
// set background
bottomJPanel.setBackground(Color.BLUE);
Color color = bottomJPanel.getBackground();
// set transparency
bottomJPanel.setBackground(new Color(color.getRed(), color.getGreen(), color.getBlue(), 64));
// set layout
FlowLayout fl_bottomJPanel = new FlowLayout();
fl_bottomJPanel.setHgap(0);
fl_bottomJPanel.setVgap(0);
bottomJPanel.setLayout(fl_bottomJPanel);
//----------------------------------------------------------
JButton button = new JButton("");
button.setAlignmentX(Component.CENTER_ALIGNMENT);
button.setPressedIcon(new ImageIcon(TransparentTest.class.getResource("/start-3.png")));
button.setRolloverIcon(new ImageIcon(TransparentTest.class.getResource("/start-2.png")));
button.setHorizontalTextPosition(SwingConstants.CENTER);
//button.setOpaque(false);
button.setContentAreaFilled(false);
button.setBorderPainted(false);
button.setFocusPainted(false);
button.setIcon(new ImageIcon(TransparentTest.class.getResource("/start-1.png")));
// add to bottom panel
bottomJPanel.add(button);
}
}