2011-04-23 5 views
5

私はJavaで小さなGUIゲームを作成しました。そしてある時点では、glassPaneを使用してすべてのmouseinputを一時的にブロックしています。前にglassPaneを問題なく使用しましたが、今回はマウス入力をブロックしません。だから、glassPaneを有効にしている間にcontentPaneにあるボタンを押し続けることができます。私はそれにペイントしたものを見ることができるので、有効になっていると確信しています。ここでglassPaneは入力をブロックしていません

です実行可能な短いコードは、問題を示しています

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Toolkit; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 


public class GuiGame { 

    private JPanel contentPane; 
    private JButton button; 
    private JFrame frame; 
    private JPanel glassPane; 
    private Dimension screenSize; 

    public static void main(String[] args) { 
     GuiGame gui = new GuiGame(); 
     gui.createGUI(); 
    } 

    public void createGUI() 
    { 
     frame = new JFrame("BadGuiGame!"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setResizable(false); 
     screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
     contentPane = new JPanel(); 
     contentPane.setPreferredSize(new Dimension(400, 400)); 
     contentPane.setBackground(Color.WHITE); 
     contentPane.setLayout(null); 
     frame.setContentPane(contentPane); 
     frame.pack(); 

     glassPane = new JPanel(); 
     glassPane.setOpaque(false); 
     glassPane.setLayout(null); 
     JLabel glassLabel = new JLabel("Glass Enabled"); 
     glassLabel.setBounds(160, 50, 80, 20); 
     glassPane.add(glassLabel); 
     frame.setGlassPane(glassPane); 

     int buttonWidth = frame.getWidth()/2; 
     int buttonHeight = frame.getHeight()/4; 
     int xButton = (frame.getWidth() - buttonWidth)/2; 
     int yButton = frame.getHeight()/2; 
     button = new JButton("NEXT LEVEL!"); 
     button.setFocusable(false); 
     button.setEnabled(true); 
     button.setBounds(xButton, yButton, buttonWidth, buttonHeight); 
     contentPane.add(button); 

     int x = (screenSize.width - frame.getWidth())/2; 
     int y = (screenSize.height - frame.getHeight())/2; 
     frame.setLocation(x, y); 
     frame.setVisible(true); 
     glassPane.setVisible(true); 
    } 
} 
+2

+1 [SSCCE](http://sscce.org/) – AbdullahC

答えて

1

私はあなたのglassPaneへのMouseListenerを追加してみてください、そして、すべてのMouseEventsにしたいイベントを消費し、そのよう

public void mouseClicked(MouseEvent e) { 
    e.consume(); 
} 
として
+1

ありがとう!私は 'comsume()'メソッドも必要ありませんでした。それは新しい問題を作り出しましたが。私はglassPaneにJComboboxを持っています。マウスリストを追加したので、リストをスクロールしたり、リスト内の何かをマウスでもう選択することはできません。 – Jesse

関連する問題