Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在另一个JComponent上添加半传输JPanel_Java_Swing - Fatal编程技术网

Java 如何在另一个JComponent上添加半传输JPanel

Java 如何在另一个JComponent上添加半传输JPanel,java,swing,Java,Swing,我正在尝试向应用程序添加功能。我的目标是用一个半传输JPanel(它将在中间包含一个JXBusyLabel)覆盖JFrame的JC组件 假设我们有这样的东西: 点击后,我们希望有以下JPanel,但是semitrasparent(这意味着它应该或多或少地允许查看它的内容): 我试图用玻璃窗玻璃,但没有成功。任何关于如何实现这一目标的建议都将不胜感激。 另外,我注意到JXBusyLabel的背景是不同的,如果它的工作方式与半透视jpanel背景相同,那就更好了。 蛋糕上的樱桃是把加载轮放在JP

我正在尝试向应用程序添加功能。我的目标是用一个半传输JPanel(它将在中间包含一个JXBusyLabel)覆盖JFrame的JC组件

假设我们有这样的东西:

点击后,我们希望有以下JPanel,但是semitrasparent(这意味着它应该或多或少地允许查看它的内容):

我试图用玻璃窗玻璃,但没有成功。任何关于如何实现这一目标的建议都将不胜感激。 另外,我注意到JXBusyLabel的背景是不同的,如果它的工作方式与半透视jpanel背景相同,那就更好了。 蛋糕上的樱桃是把加载轮放在JPAND的中间。< /P> 一旦我们解决了每个问题,我将立即更新下面的代码。 任何关于如何实现这一目标的建议都将不胜感激

以下是专门编写的示例代码:

public class Main {


    public static void main(String[] args)  {       
        SwingUtilities.invokeLater(new TestApp());      
    }

}

class TestApp implements Runnable {
    @Override
    public void run(){  
        JFrame mainWindow = new JFrame("MyFrame");
        mainWindow.setLayout(new BorderLayout());
        mainWindow.setMinimumSize(new Dimension(300,300));

//      mainWindow.add(initView(),BorderLayout.CENTER);
      mainWindow.add(initLoadingPanel(),BorderLayout.CENTER);
        mainWindow.add(initButton(),BorderLayout.PAGE_END);

        mainWindow.setVisible(true);
    }

    private JPanel initView() {
        JPanel viewPort = new JPanel();
        viewPort.setLayout(new BoxLayout(viewPort, BoxLayout.Y_AXIS));
        viewPort.add(new JLabel("label 1"));
        viewPort.add(new JLabel("label 2"));
        viewPort.add(new JLabel("label 3"));
        viewPort.add(new JLabel("label ..."));
        viewPort.add(new JLabel("label N"));
        return viewPort;
    }

    private JPanel initLoadingPanel() {
        JPanel trasparentPanel = new JPanel();
        trasparentPanel.setOpaque(true);
        trasparentPanel.setBackground(new Color(200,230,250,100));
        trasparentPanel.add(initLoadingWheel());
        return trasparentPanel;
    }

    private Component initLoadingWheel() {
        JXBusyLabel loadingWheel = new JXBusyLabel(new Dimension(80,80));
        loadingWheel.setBackground(new Color(200,230,250,100));
        loadingWheel.setOpaque(true);
        loadingWheel.setBusy(true);
        return loadingWheel;
    }

    private JButton initButton() {
        JButton button = new JButton("Start & Stop Loading");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });
        return button;
    }
}
我试图用玻璃窗玻璃,但没有成功

对我来说很好。阅读上的Swing教程并下载工作示例。发布演示问题的
SSCCE

您还可以查看以获取简单的API。当前代码仅显示文本,但您可以轻松修改类以显示图像,因为代码仅使用JLabel作为文本

您也可以使用一个装饰面板

阅读上的Swing教程并下载工作日志 例如
[…]还可以查看 API
您也可以使用装饰板来装饰面板

正如Camickr所建议的,玻璃面板可以工作。

结果如下:

以下代码表示GlassPane的距离,必须将其设置为JFrame的GlassPane

@SuppressWarnings("serial")
public class GlassPane extends JComponent implements KeyListener {

    private static Cursor WAIT_CURSOR = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);

    private JXBusyLabel wheel;

    public GlassPane()  {
        super.setOpaque( false );
        super.setLayout( new GridBagLayout() );
        super.setBackground(initBackground());
        initWheel();
        addEventCatchers();
    }

    private void addEventCatchers() {
        addMouseListener( new MouseAdapter() {} );
        addMouseMotionListener( new MouseMotionAdapter() {} );
        addKeyListener(this);
        setFocusTraversalKeysEnabled(false);        
    }

    private Color initBackground() {
        Color base = UIManager.getColor("inactiveCaptionBorder");
        Color background = new Color(base.getRed(), base.getGreen(), base.getBlue(), 128);
        return background;
    }

    private void initWheel() {
        wheel = new JXBusyLabel();
        add(wheel, new GridBagConstraints());
    }

    /*
     *  The component is transparent but we want to paint the background
     *  to give it the disabled look.
     */
    @Override
    protected void paintComponent(Graphics g)   {
        g.setColor( getBackground() );
        g.fillRect(0, 0, getSize().width, getSize().height);
    }

    /*
     *  Make the glass pane visible, start the wheel and change the cursor to the wait cursor
     */
    public void activate(boolean toggle) {
        wheel.setVisible(toggle);
        wheel.setBusy(toggle);
        super.setVisible( toggle );
        setCursor(getCursor());
        if(super.isVisible()){
            requestFocusInWindow();
        }
    }

    @Override
    public Cursor getCursor(){
        Cursor cursor = this.isVisible() ? GlassPane.WAIT_CURSOR : null; 
        return cursor;
    }

    /*
    *  Implement the KeyListener to consume events
    */
    public void keyPressed(KeyEvent e)  {
        e.consume();
    }
    public void keyReleased(KeyEvent e) {
        e.consume();
    }

    public void keyTyped(KeyEvent e) {}

}
以下是可用于测试上述类的主要方法:

public class Main {

    public static GlassPane glass;
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("GlassPaneDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(200,200));

        Container contentPane = frame.getContentPane();
        contentPane.setLayout(new BorderLayout());
        contentPane.add(new JLabel("asdasda"),BorderLayout.CENTER);
        GlassPane myGlassPane = new GlassPane();
        JButton myButton =  initButton(myGlassPane);

        contentPane.add(myButton,BorderLayout.PAGE_END);
        frame.setGlassPane(myGlassPane);
        frame.pack();
        frame.setVisible(true);
    }

    private static JButton initButton(final GlassPane myGlassPane) {
        JButton button = new JButton("Start & Stop Loading");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                    myGlassPane.activate(!myGlassPane.isVisible());
            }
        });
        return button;
    }

}

谢谢你的提示,我明天早上会查看并更新这篇文章!你能帮我做些半透明材料吗?
正如卡米克建议的那样,玻璃面板可以工作。
-那为什么我的回答不被接受呢。教程(或我的链接)再次为您指出了正确的方向<代码>使用带有玻璃窗格中“居中”选项的flowLayout,仅使车轮水平居中-不要使用flowLayout。您是否查看了
禁用的窗格玻璃
,以了解它的用途?我给你一个链接是有原因的。
加载滚轮背景与JPanel的背景不同
-使用透明背景可能会导致绘制瑕疵。请参阅以获取更多信息和一些建议。如果将来有人来到这里,他们希望看到解决方案,而不是建议,我会将我自己的回答检查为“已接受”。如果你想为你的自私获得绿色标记,请为社区随意下手。而且,在所有问题解决之前,我不会接受我的答案。感谢您提出的新建议,我将检查它们并更新答案。我没有检查禁用的玻璃窗格,现在执行此操作,代码将更新
他们希望看到解决方案,而不是建议
-为您提供了多个解决方案,以允许您为您的问题选择最佳解决方案。提供的每个链接都为您提供了解决问题的不同方法。我们不是来为您编写代码的,因为我们不知道完整的需求,所以我们为您指出了正确的方向,并允许您为您的完整需求选择最佳的解决方案。