为了账号安全,请及时绑定邮箱和手机立即绑定

设置 JPanel 对键盘透明

设置 JPanel 对键盘透明

慕姐8265434 2022-10-26 16:30:57
我实际上正在为应用程序编写键盘,但遇到了问题。我在创建的 JDialog 中使用了一些空白空间,以便在对话框的右上角放置一个存在按钮。这个空白区域正在为对话框创建一个令人不快的边框。因此,我尝试使用 setOpaque(false) 函数使面板透明,但我一无所获。我是否必须使用绘画功能(我不知道如何使用)?这是我的测试功能,没有我做过的 setOpaque 测试:package test;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import javax.swing.*;public class Test {    public static JFrame frame;    public static void main(String args[]){        frame = new JFrame();        JPanel panel = new JPanel();        panel.setSize(400, 400);        panel.setBackground(Color.CYAN);        panel.setLayout(new GridBagLayout());        JTextField text = new JTextField("coucou");        panel.add(text);        text.addMouseListener(new MouseAdapter() {            public void mouseClicked(MouseEvent e) {                text.setText("");                openKeypad(text);            }        });        frame.setContentPane(panel);        frame.setSize(400, 400);        frame.setUndecorated(true);        frame.setLocation(50,0);        frame.setVisible(true);    }    private static void openKeypad(JTextField text) {        KeyPad pad = new KeyPad(frame, text);    }}
查看完整描述

1 回答

?
拉丁的传说

TA贡献1789条经验 获得超8个赞

使 JFrame 透明的简单示例:


import java.awt.*;

import static java.awt.GraphicsDevice.WindowTranslucency.TRANSLUCENT;

import javax.swing.*;


class TransparentFrame extends JFrame

{

    public TransparentFrame()

    {

        JPanel panel = new JPanel();

        panel.setOpaque( false );

        panel.add( new JButton("Button") );

        add(panel, BorderLayout.SOUTH);


        setUndecorated(true);

        setSize(300, 300);

        //pack();

//      setOpacity(0.5f); // set transparency on frame and components

        setBackground( new Color(0, 0, 0, 64) ); // set transparency on frame only


        setTitle("Transparent Frame Demo");

        setDefaultCloseOperation(DISPOSE_ON_CLOSE);

    }


    public static void main(String args[])

    {

        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

        GraphicsDevice gd = ge.getDefaultScreenDevice();


        //  Exit when translucent windows aren't supported


        if (!gd.isWindowTranslucencySupported(TRANSLUCENT))

        {

            System.err.println( "Translucency is not supported" );

            System.exit(0);

        }


        // Create the GUI on the event-dispatching thread


        SwingUtilities.invokeLater(() -> new TransparentFrame().setVisible(true));

    }

}


查看完整回答
反对 回复 2022-10-26
  • 1 回答
  • 0 关注
  • 80 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信