0

両方とも を拡張する 2 つの画面がGameScreenあり、それがJPanel. 私NavigationFrameのクラスは正しく実行されますがInternalSensorsFrame、ボタンの上に灰色の背景の四角形を描画しているため、ボタンが非表示になり、マウスをボタンの上に置いたときにちらつきだけが表示されます。これが私のコードです。1つの例が機能し、もう1つの例が機能しない理由と、それを修正する方法を誰か教えてもらえますか?

public class NavigationFrame extends GameScreen {

...
    public NavigationFrame() {
        //super(parent);
        addKeyListener(new TAdapter());
        //background
        img = new ImageIcon(Home.resources.getClass().getResource(imageName));
        bg = new BufferedImage(
        img.getIconWidth(),
        img.getIconHeight(),
        BufferedImage.TYPE_4BYTE_ABGR);
        Graphics gg = bg.createGraphics();
        img.paintIcon(null, gg, 0,0);
        gg.dispose();
        RenderingHints rh =
                new RenderingHints(RenderingHints.KEY_ANTIALIASING,
               RenderingHints.VALUE_ANTIALIAS_ON);
        rh.put(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);

    gameFont = new Font("Helvetica", Font.BOLD, 14);

    //Set Viewscreen to blockmapview
    if (viewScreen == null){
        setViewScreen(new ScrollShipScreen(this, 0));
    }
    viewScreen.setBounds(150, 50, 500, 500);
    add(viewScreen);

    //Buttons
    btnMainComputer = new JButton("Main Computer");
    btnMainComputer.setBounds(140, 560, 120, 23);
    btnMainComputer.addActionListener(this);
    add(btnMainComputer);

    btnInternalSensors = new JButton("Internal Sensors");
    btnInternalSensors.setBounds(330, 560, 130, 23);
    btnInternalSensors.addActionListener(this);
    add(btnInternalSensors);

    btnNavigation = new JButton("Navigation");
    btnNavigation.setBounds(550, 560, 110, 23);
    btnNavigation.addActionListener(this);
    add(btnNavigation);


    }

    @Override
    public void paintComponent(Graphics g) {
        //background
        g.setColor(Color.decode("#666665"));
        Graphics2D g2d = (Graphics2D) g;
        g2d.fillRect(0, 0, 800, 600);
        g2d.drawImage(bg, 0, 0,img.getIconWidth(), img.getIconHeight(), this);

        //text setup
        g.setColor(Color.white);
        g.setFont(gameFont);


        //Write the strings
        //left bar
        g.drawString(location, 10,60);
....

    }



    //Button Presses
    public void actionPerformed(ActionEvent ae) {   
        JButton o = (JButton) ae.getSource();
....
        else if(o == btnMainComputer){
            Container parent  = getParent();
            parent.remove(Home.getCurrentScreen());
            Home.setCurrentScreen(new MainComputerFrame());
            parent.add(Home.getCurrentScreen());
            //System.out.println("Main Comp");
        }
        else if(o == btnInternalSensors){
            Container parent  = getParent();
            parent.remove(Home.getCurrentScreen());
            Home.setCurrentScreen(new InternalSensorsFrame());
            parent.add(Home.getCurrentScreen());
            //System.out.println("Sensors");

        }
        else if(o == btnNavigation){
            //Does nothing on a navigation Frame
//          remove(Home.getCurrentScreen());
//          Home.setCurrentScreen(new NavigationFrame());
//          Home.frame.add(Home.getCurrentScreen());
        }

        this.requestFocus();
    }

}

そして、ここに非労働者階級があります:

public class InternalSensorsFrame extends GameScreen {
private String imageName = "textures/interface/View Screen Frame Overlay.png";



public InternalSensorsFrame(){
    //super(parent);
    addKeyListener(new TAdapter());

    //background
    img = new ImageIcon(Home.resources.getClass().getResource(imageName));
    bg = new BufferedImage(
    img.getIconWidth(),
    img.getIconHeight(),
    BufferedImage.TYPE_4BYTE_ABGR);
    Graphics gg = bg.createGraphics();
    img.paintIcon(null, gg, 0,0);
    gg.dispose();
    RenderingHints rh =
            new RenderingHints(RenderingHints.KEY_ANTIALIASING,
           RenderingHints.VALUE_ANTIALIAS_ON);
    rh.put(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);

    //Add buttons
    btnMainComputer = new JButton("Main Computer");
    btnMainComputer.setBounds(140, 560, 120, 23);
    btnMainComputer.addActionListener(this);
    add(btnMainComputer);

    btnInternalSensors = new JButton("Internal Sensors");
    btnInternalSensors.setBounds(330, 560, 130, 23);
    btnInternalSensors.addActionListener(this);
    add(btnInternalSensors);

    btnNavigation = new JButton("Navigation");
    btnNavigation.setBounds(550, 560, 110, 23);
    btnNavigation.addActionListener(this);
    add(btnNavigation);

}
@Override
public void paintComponent(Graphics g) {
    //super.paint(g);
    g.setColor(Color.decode("#666665"));
    Graphics2D g2d = (Graphics2D) g;
    g2d.fillRect(0, 0, 800, 600);
    g2d.drawImage(bg, 0, 0,img.getIconWidth(), img.getIconHeight(), this);
    g2d.translate(150, 50);
    Home.ship.shipLayout.paintComponent(g2d);
//        ArrayList<CrewMan> crew = Home.ship.crew;
//        for (int i=0; i<crew.size(); i++){
//          crew.get(i).paint(g2d);
//        }
        g.setColor(Color.white);
        g.drawString("teststring", 10,60);
        //super.paint(g);

        //Toolkit.getDefaultToolkit().sync();
      //  g.dispose();
}

        //Button Presses
        public void actionPerformed(ActionEvent ae) {   

            JButton o = (JButton) ae.getSource();

            if(o == btnMainComputer){
                Container parent  = getParent();
                parent.remove(Home.getCurrentScreen());
                Home.setCurrentScreen(new MainComputerFrame());
                parent.add(Home.getCurrentScreen());
                //System.out.println("Main Comp");
            }
            else if(o == btnInternalSensors){
                Container parent  = getParent();
                parent.remove(Home.getCurrentScreen());
                Home.setCurrentScreen(new InternalSensorsFrame());
                parent.add(Home.getCurrentScreen());
                //System.out.println("Sensors");

            }
            else if(o == btnNavigation){
                Container parent  = getParent();
                parent.remove(Home.getCurrentScreen());
                Home.setCurrentScreen(new NavigationFrame());
                parent.add(Home.getCurrentScreen());
            }

            this.requestFocus();
        }
}
4

2 に答える 2

4

おそらく、このアドバイスはあなたの問題を解決しないかもしれませんが、助けになるかもしれません.

1)

super.paintComponent(g)このメソッドをオーバーライドするときに呼び出します。

@Override
    public void paintComponent(Graphics g) {
        //background
        super.paintComponent(g);
        g.setColor(Color.decode("#666665"));
        Graphics2D g2d = (Graphics2D) g;
        g2d.fillRect(0, 0, 800, 600);
        g2d.drawImage(bg, 0, ...

2)requestFocus()

このメソッドの動作はプラットフォームに依存するため、このメソッドの使用はお勧めできません。代わりに、requestFocusInWindow() の使用をお勧めします。

3)実際にはJPanelであるのに、クラスがフレームであると言っているクラスに適切な名前を使用してください。

4)個人的な提案として、具体的な継承は危険で管理が難しく、継承ツリーのレベルが高すぎるため、あまり使用しないでください。代わりに、このようなものを使用できます。

例:

public class NavigationComponent {

private GameScreen gameScreen;

public NavigationComponent(){
      gameScreen = new GameScreen(){
           @override
          public paintComponent(Graphics g){
              // code here
          }

      };
}

}

5) keyListener を に追加していJPanelます。この作業を行うには、オブジェクトをフォーカス可能にしてフォーカスを合わせる必要がありますKeyBinding。Swing は、keyBinding で使用するように設計されています。キーバインドの使い方

于 2013-10-11T02:07:28.940 に答える
0

投稿後、問題を簡単に解決しました。追加super.paintComponents(g);しても機能しませんでした/違いはありませんでした。結局はグラフィックを処分していたからです。作業コードは次のとおりです。

    @Override
public void paintComponent(Graphics g) {
   // super.paintComponents(g);
    g.setColor(Color.decode("#666665"));
    Graphics2D g2d = (Graphics2D) g;
    g2d.fillRect(0, 0, 800, 600);
    g2d.drawImage(bg, 0, 0,img.getIconWidth(), img.getIconHeight(), this);



    Toolkit.getDefaultToolkit().sync();
//  g.dispose(); //commenting this line out made it work
    }

ただし、ここで提供された回答/コメントに感謝し、残りのコードを強化するためにそれらを処理します/コメントで回答します. ありがとう!

于 2013-10-12T17:43:02.473 に答える