gpt4 book ai didi

java - 我怎样才能使黑色在上传的图像之上透明?

转载 作者:行者123 更新时间:2023-11-30 11:25:26 25 4
gpt4 key购买 nike

现在我所拥有的只是创建黑色背景并上传我想要的图像。我想使图像位于透明背景后面。我最初将所有这些都放在单独的类文件中,但将它们全部放在一个主类中

package Flashlight;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Flashlight3 extends JFrame {

public class FlashLabelChange extends JPanel {

Flashlight.FlashDisp flashDisp;

public FlashLabelChange(Flashlight.FlashDisp _flashDisp) {
flashDisp = _flashDisp;
JButton btn1 = new JButton("Start");
add(btn1);
class Button implements ActionListener {

public void actionPerformed(ActionEvent event) {
if (event.getActionCommand().equals("Start")) {
flashDisp.UpdateLabel("Start");
}
}
}
ActionListener button = new Button();
btn1.addActionListener(button);

}
}

public class FlashDisp extends JPanel {

private JLabel lblName;
private String sLabel;
private JLabel lblImage;

public FlashDisp() {
lblImage = new JLabel();
add(lblImage);
lblName = new JLabel("Start?");
add(lblName); //add it to the Frame
}

void UpdateLabel(String _sNew) {
sLabel = _sNew;
lblName.setText(sLabel);
}

void UpdateBackground(String _sNew) {
sLabel = _sNew;
if (sLabel == ("Black")) {
setBackground(Color.black);
lblImage.setIcon(new ImageIcon("Hallway.png"));
}
}
}

public class FlashColour extends JPanel {

FlashDisp flashDisp;

public FlashColour(FlashDisp _flashDisp) {
flashDisp = _flashDisp;
setLayout(new GridLayout(3, 1));
JButton btnDark = new JButton("Dark");
add(btnDark);
class ColourChangeListener implements ActionListener {

public void actionPerformed(ActionEvent event) {
if (event.getActionCommand().equals("Dark")) {
flashDisp.UpdateBackground("Black");
}
}
}
ActionListener colourChangeListener = new ColourChangeListener();
btnDark.addActionListener(colourChangeListener);


}
}

public static void main(String[] args) {
JFrame frame = new JFrame();
FlashMain flashMain = new FlashMain();
frame.setSize(400, 400);
frame.setTitle("FLAAAASH LIGHT");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(flashMain);
frame.setVisible(true);
}
}

最佳答案

我认为您需要使用opaque 方法。

看这里 setOpaque(true/false); Java使用不透明

希望对您有所帮助。

关于java - 我怎样才能使黑色在上传的图像之上透明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20288037/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com