gpt4 book ai didi

java - 在 JPanel 中使用 GlassPane

转载 作者:行者123 更新时间:2023-12-02 09:26:53 25 4
gpt4 key购买 nike

我想使用 glassPane 函数在加载数据期间停用面板中的组件。我知道玻璃板仅适用于框架。在我的应用程序中,我无法访问该框架。我只想停用框架中的单个面板。

我尝试通过重写面板的paint或paintComponent方法在面板上绘制矩形。问题是底层组件仍然可用。

谁能给我建议如何解决这个问题?

问候

数据库

最佳答案

一种可能性 - 使用 JLayeredPane 包围您的 JPanel,然后激活位于其上方的 JPanel 层,让它窃取焦点和 MouseListener。例如:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;

import javax.swing.*;

public class MyGlassPaneWrapper extends JLayeredPane {
private JPanel glassPanel = new JPanel();

public MyGlassPaneWrapper(JComponent myPanel) {
glassPanel.setOpaque(false);
glassPanel.setVisible(false);
glassPanel.addMouseListener(new MouseAdapter() {});
glassPanel.setFocusable(true);

myPanel.setSize(myPanel.getPreferredSize());
add(myPanel, JLayeredPane.DEFAULT_LAYER);
add(glassPanel, JLayeredPane.PALETTE_LAYER);

glassPanel.setPreferredSize(myPanel.getPreferredSize());
glassPanel.setSize(myPanel.getPreferredSize());
setPreferredSize(myPanel.getPreferredSize());
}

public void activateGlassPane(boolean activate) {
glassPanel.setVisible(activate);
if (activate) {
glassPanel.requestFocusInWindow();
glassPanel.setFocusTraversalKeysEnabled(false);
}
}

private static void createAndShowGui() {
final MyPanel myPanel = new MyPanel();
final MyGlassPaneWrapper myGlassPaneWrapper = new MyGlassPaneWrapper(myPanel);
final String activateGlassPane = "Activate GlassPane";
final String deactivateGlassPane = "Deactivate GlassPane";

JToggleButton toggleButton = new JToggleButton(activateGlassPane);
toggleButton.addItemListener(new ItemListener() {

@Override
public void itemStateChanged(ItemEvent e) {
AbstractButton source = (AbstractButton) e.getSource();
if (e.getStateChange() == ItemEvent.SELECTED) {
source.setText(deactivateGlassPane);
myGlassPaneWrapper.activateGlassPane(true);
} else {
source.setText(activateGlassPane);
myGlassPaneWrapper.activateGlassPane(false);
}
}
});
JPanel buttonPanel = new JPanel();
buttonPanel.add(toggleButton);

JFrame frame = new JFrame("MyGlassPane");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(myGlassPaneWrapper);
frame.getContentPane().add(buttonPanel, BorderLayout.PAGE_END);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

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

class MyPanel extends JPanel {

public MyPanel() {
add(new JTextField(5));
add(new JComboBox<String>(new String[]{"Monday", "Tuesday", "Wednesday"}));
add(new JButton(new AbstractAction("Push Me") {

@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(MyPanel.this, "Button Pushed");
}
}));
}
}

关于java - 在 JPanel 中使用 GlassPane,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29145472/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com