Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
163 views
in Technique[技术] by (71.8m points)

java - How to make JFrame transparent while components are visible (solved)

*Use Google Translation

Hello! I'm student studying java swing. And I tried to programming simple program. What I want is a notepad program that toggles between transparent and opaque when you press Ctrl + Alt. Now press Ctrl + Alt to toggle between transparent and opaque. However, the font in transparent mode also changes. I used the SetFont () function to force the font to be set, but it only applies in opaque mode and the setFont function does not work in transparent mode.

If I remove setBackground () from the toggleVisible () function, the font will not change either. My guess seems to change the setBackground font. Anyone who knows how to make a frame transparent without using setBackground? Or I would appreciate it if you could help me solve the problem.


package ffmemo;

import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsDevice;
import static java.awt.GraphicsDevice.WindowTranslucency.TRANSLUCENT;
import java.awt.GraphicsEnvironment;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.BitSet;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;


public class FFMemo {
    public static void main(String[] args){
        new FFMemoFrame();
    }
}
class FFMemoFrame extends JFrame {
    BitSet keyBitSet = new BitSet(2); //0: ctrl key, 1: alt key
    boolean visible = true;
    JTextArea textArea;
    JScrollPane scrollPane;
    Color color;
    
    FFMemoFrame(){
        //PARAM: window title
        super("FFMemo");
        setSize(500, 200);
        setLocation(100, 100);
        setUndecorated(true);
        getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
        
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        color = getBackground();
        textArea = new JTextArea();
        textArea.getDocument().addDocumentListener(new DocumentListener(){
            @Override
            public void changedUpdate(DocumentEvent e){
                warn();
            }

            @Override
            public void insertUpdate(DocumentEvent e) {
                warn();
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                warn();
            }
            public void warn(){
                //System.out.println(textArea.getText());
                repaint();
            }
        });
        
        textArea.append("hello, world!");
        textArea.setLineWrap(true);
        textArea.setVisible(true);
        textArea.setFont(new Font("??", Font.PLAIN, 20));
        textArea.addKeyListener(new KeyAdapter(){

            public void keyPressed(KeyEvent e){
                String s = e.getKeyText(e.getKeyCode());
                if(s.equals("Ctrl")) keyBitSet.set(0, true);
                else if(s.equals("Alt")) keyBitSet.set(1, true);
                if(keyBitSet.get(0) && keyBitSet.get(1)){
                    toggleVisible();
                }
                

            }
            public void keyReleased(KeyEvent e){
                String s = e.getKeyText(e.getKeyCode());
                if(s.equals("Ctrl")) keyBitSet.set(0, false);
                else if(s.equals("Alt")) keyBitSet.set(1, false);
                
            }
        });
        scrollPane = new JScrollPane(textArea);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setVisible(true);
        
        add(scrollPane);
        
        revalidate();
        
    }
    private void toggleVisible(){
        
        if(visible){
            setBackground(new Color(255, 255, 255, 1));
            textArea.setOpaque(false);
            scrollPane.getViewport().setOpaque(false);
            scrollPane.setOpaque(false);
            textArea.setFont(new Font("??", Font.PLAIN, 20));
            
        }
        else{
            setBackground(new Color(255, 255, 255, 255));
            textArea.setOpaque(true);
            scrollPane.getViewport().setOpaque(true);
            scrollPane.setOpaque(true);
            textArea.setFont(new Font("??", Font.PLAIN, 20));
            
        }
        repaint();
        visible = !visible;
    }
}

+) There are runtime screenshot. "|" at second picture is a cursor which moved right as far as possible. and I don't understand why I can't move cursor to right more. Something must be wrong... but I don't know what it is. first second

question from:https://stackoverflow.com/questions/65932326/how-to-make-jframe-transparent-while-components-are-visible-solved

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...