JFrame中文本出现白点
White spots appear with text in JFrame
看起来像:
如图所示,文字上有白点。
我的代码是:
import javax.swing.*;
import java.awt.*;
public class SubsFrame extends JFrame {
private String text;
private JLabel label;
private byte mode;
private static SubsFrame instance;
public static void build(long mode) throws Exception{
if(instance == null) {
instance = new SubsFrame(mode);
}else{
throw new Exception("Another instance already exists!");
}
}
public static void buildSilent(long mode) throws Exception{
if(instance == null)
instance = new SubsFrame(mode);
}
public static void showFrame(){
instance.setVisible(true);
}
public static void hideFrame(){
instance.setVisible(false);
}
public static void destroy() {
instance.dispose();
instance = null;
}
public static void setOpacity(double opacity){
instance.setBackground(new Color(1.0f,1.0f,1.0f,(float)opacity));
}
private SubsFrame(long mode) throws Exception{
if(JSubsConstants.isValidMode(mode))
this.mode = (byte)mode;
else
throw new Exception("Invalid Mode Selected!");
if(!this.isAlwaysOnTopSupported()){
throw new Exception("Always on top is not supported!");
}
label = new JLabel("Hello World! Welcome to this app!");
label.setFont(new Font("Consolas", Font.PLAIN, 25));
this.setLayout(new FlowLayout());
this.add(label);
this.setDefaultCloseOperation(HIDE_ON_CLOSE);
this.setAlwaysOnTop(true);
this.setUndecorated(true);
if(this.mode == JSubsConstants.AUTO_SIZED_FRAME_AUTO_POSITIONED){
handleFrameSize();
handleFrameLocation();
}
else if(this.mode == JSubsConstants.AUTO_SIZED_FRAME_MANUAL_POSITIONED){
handleFrameSize();
}
}
private void handleFrameLocation() {
this.setLocation(250, 350);
}
private void handleFrameSize() {
this.pack();
this.setSize(this.getWidth() + 20, this.getHeight() + 10);
}
}
为什么会出现这些,我该如何删除它们?
我想这些与字体本身有关,而不是 Java,但我不确定。我尝试了一些其他字体,结果相同。
如果您需要更多信息,请问我。
我的“怀疑”是试图弄清楚合成应该如何工作的算法在文本抗锯齿方面遇到了问题。
通常当我想做这些事情时,我让 window 完全透明并将逻辑移到另一个容器,这似乎做得更好,一般来说并消除了由平台及其处理透明的方式 windows - 但这就是我。
import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JWindow;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class TranscluentWindow {
public static void main(String[] args) {
new TranscluentWindow();
}
public TranscluentWindow() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}
JWindow frame = new JWindow();
frame.setAlwaysOnTop(true);
frame.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
SwingUtilities.getWindowAncestor(e.getComponent()).dispose();
}
}
});
frame.setBackground(new Color(0, 0, 0, 0));
frame.add(new TranslucentPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TranslucentPane extends JPanel {
private float opacity = 1;
public TranslucentPane() {
setOpaque(false);
setLayout(new BorderLayout());
JLabel label = new JLabel("Hello World! Welcome to this app!");
label.setForeground(Color.RED);
label.setBorder(new EmptyBorder(8, 8, 8, 8));
add(label);
JSlider slider = new JSlider();
slider.setMinimum(0);
slider.setMaximum(100);
slider.setValue(100);
slider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
setOpacity(slider.getValue() / 100f);
}
});
add(slider, BorderLayout.SOUTH);
}
public void setOpacity(float opacity) {
this.opacity = opacity;
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setComposite(AlphaComposite.SrcOver.derive(opacity));
g2d.setColor(getBackground());
g2d.fillRect(0, 0, getWidth(), getHeight());
g2d.dispose();
}
}
}
看起来像:
如图所示,文字上有白点。
我的代码是:
import javax.swing.*;
import java.awt.*;
public class SubsFrame extends JFrame {
private String text;
private JLabel label;
private byte mode;
private static SubsFrame instance;
public static void build(long mode) throws Exception{
if(instance == null) {
instance = new SubsFrame(mode);
}else{
throw new Exception("Another instance already exists!");
}
}
public static void buildSilent(long mode) throws Exception{
if(instance == null)
instance = new SubsFrame(mode);
}
public static void showFrame(){
instance.setVisible(true);
}
public static void hideFrame(){
instance.setVisible(false);
}
public static void destroy() {
instance.dispose();
instance = null;
}
public static void setOpacity(double opacity){
instance.setBackground(new Color(1.0f,1.0f,1.0f,(float)opacity));
}
private SubsFrame(long mode) throws Exception{
if(JSubsConstants.isValidMode(mode))
this.mode = (byte)mode;
else
throw new Exception("Invalid Mode Selected!");
if(!this.isAlwaysOnTopSupported()){
throw new Exception("Always on top is not supported!");
}
label = new JLabel("Hello World! Welcome to this app!");
label.setFont(new Font("Consolas", Font.PLAIN, 25));
this.setLayout(new FlowLayout());
this.add(label);
this.setDefaultCloseOperation(HIDE_ON_CLOSE);
this.setAlwaysOnTop(true);
this.setUndecorated(true);
if(this.mode == JSubsConstants.AUTO_SIZED_FRAME_AUTO_POSITIONED){
handleFrameSize();
handleFrameLocation();
}
else if(this.mode == JSubsConstants.AUTO_SIZED_FRAME_MANUAL_POSITIONED){
handleFrameSize();
}
}
private void handleFrameLocation() {
this.setLocation(250, 350);
}
private void handleFrameSize() {
this.pack();
this.setSize(this.getWidth() + 20, this.getHeight() + 10);
}
}
为什么会出现这些,我该如何删除它们?
我想这些与字体本身有关,而不是 Java,但我不确定。我尝试了一些其他字体,结果相同。
如果您需要更多信息,请问我。
我的“怀疑”是试图弄清楚合成应该如何工作的算法在文本抗锯齿方面遇到了问题。
通常当我想做这些事情时,我让 window 完全透明并将逻辑移到另一个容器,这似乎做得更好,一般来说并消除了由平台及其处理透明的方式 windows - 但这就是我。
import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JWindow;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class TranscluentWindow {
public static void main(String[] args) {
new TranscluentWindow();
}
public TranscluentWindow() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}
JWindow frame = new JWindow();
frame.setAlwaysOnTop(true);
frame.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
SwingUtilities.getWindowAncestor(e.getComponent()).dispose();
}
}
});
frame.setBackground(new Color(0, 0, 0, 0));
frame.add(new TranslucentPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TranslucentPane extends JPanel {
private float opacity = 1;
public TranslucentPane() {
setOpaque(false);
setLayout(new BorderLayout());
JLabel label = new JLabel("Hello World! Welcome to this app!");
label.setForeground(Color.RED);
label.setBorder(new EmptyBorder(8, 8, 8, 8));
add(label);
JSlider slider = new JSlider();
slider.setMinimum(0);
slider.setMaximum(100);
slider.setValue(100);
slider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
setOpacity(slider.getValue() / 100f);
}
});
add(slider, BorderLayout.SOUTH);
}
public void setOpacity(float opacity) {
this.opacity = opacity;
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setComposite(AlphaComposite.SrcOver.derive(opacity));
g2d.setColor(getBackground());
g2d.fillRect(0, 0, getWidth(), getHeight());
g2d.dispose();
}
}
}