JLabel在Mac上具有透明背景,但是在Windows上却没有?

时间:2018-06-28 18:00:22

标签: java

我正在为我的教堂提出申请,而我的JLabel上有一些阴影,我正在尝试使Windows计算机上的背景透明,但无法正常工作。它可以在Mac上完美运行,我尝试使用Google谷歌搜索并使用setOpague进行伪造,但没有任何建议吗?

问题图片: Mac

Windows

代码:

//Made by Trey Carey | 6.25.18
//Credit to Daniel Kihlgren for the original idea
//Credit to Kevin Blenman for UI work

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

public class choosingScreen {
	static String versionNumber = new String("1.0"); //Version Number
	static String applicationName = new String("Lower Thirds SDV " + versionNumber); //Application Name
	
	public static void main(String[] args) throws IOException {
		createChoosingWindow();
	}

	static void createChoosingWindow() throws IOException {
		JFrame mainFrame = new JFrame(applicationName);
		
		//Images to Buffer
		URL icon = loginScreen.class.getResource("/images/SDV-Icon.png");
		JLabel backgroundImage = new JLabel(new ImageIcon(loginScreen.class.getResource("/images/Main_BKG.png")));
		JLabel logo = new JLabel(new ImageIcon(loginScreen.class.getResource("/images/POK Logo.png")));
		JLabel copyrightImage = new JLabel(new ImageIcon(loginScreen.class.getResource("/images/Copyright.png")));
		JLabel lowerThirdsLogo = new JLabel(new ImageIcon(loginScreen.class.getResource("/images/LowerThirds_Logo.png")));
		JButton lowerThirdsButton = new JButton(new ImageIcon(loginScreen.class.getResource("/images/Lower Thirds.png")));
		JButton lyricsButton = new JButton(new ImageIcon(loginScreen.class.getResource("/images/Lyrics.png")));
		
		BufferedImage iconImage = ImageIO.read(icon);
		
		mainFrame.add(backgroundImage);
		
		backgroundImage.add(lowerThirdsLogo);
		lowerThirdsLogo.setSize(lowerThirdsLogo.getPreferredSize());
		lowerThirdsLogo.setLocation(150, 20);
		
		backgroundImage.add(logo);
		logo.setSize(logo.getPreferredSize());
		logo.setLocation(270, 525);
		
		backgroundImage.add(copyrightImage);
		copyrightImage.setSize(copyrightImage.getPreferredSize());
		copyrightImage.setLocation(600, 550);
		
		backgroundImage.add(lowerThirdsButton);
		lowerThirdsButton.setSize(lowerThirdsButton.getPreferredSize());
		lowerThirdsButton.setLocation(200, 200);
		lowerThirdsButton.setText("");
		lowerThirdsButton.setBorder(BorderFactory.createEmptyBorder());
		
		backgroundImage.add(lyricsButton);
		lyricsButton.setSize(lyricsButton.getPreferredSize());
		lyricsButton.setLocation(200, 300);
		lyricsButton.setText("");
		lyricsButton.setBorder(BorderFactory.createEmptyBorder());
		
		//LYRICS ACTION LISTENER
		lyricsButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO add code for making Lyrics work
				
			}
	
		});
		
		//LOWER THIRDS ACTION LISTENER
		lowerThirdsButton.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				//TODO make code for working lower thirds here
			}
			
		});
		
		mainFrame.setResizable(false);
		mainFrame.setIconImage(iconImage);
		mainFrame.pack();
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mainFrame.setLocationRelativeTo(null);
		mainFrame.setVisible(true);
	}
	
	

}

这里是images

1 个答案:

答案 0 :(得分:0)

this question之后, 尝试在JButton lyricsButton = ...行之后添加以下行:

        lowerThirdsButton.setContentAreaFilled(false);
        lowerThirdsButton.setBorder(BorderFactory.createEmptyBorder());
        lyricsButton.setContentAreaFilled(false);
        lyricsButton.setBorder(BorderFactory.createEmptyBorder());

您已经可以用线条将按钮的边框进一步调低。您可以将其删除,因为它们已无济于事。

然后您可能会发现按钮偏离中心。鉴于图像的宽度为381像素,背景图像的宽度为801像素,而您将图像放置在距左侧200像素,因此距右侧220像素的位置,这可能是可以预期的。将它们放在距左侧210像素处应该可以解决此问题。

对代码进行此更改会使按钮边框在Windows上运行时消失。我不能说这些更改会对Mac产生什么影响,因为我没有Mac。