0

我有两个都扩展的屏幕,GameScreen这又扩展了一个JPanel. 我的NavigationFrame课程正常执行,但我InternalSensorsFrame在按钮上绘制了灰色背景矩形,以便按钮被隐藏,并且当您将鼠标悬停在它们上时只会短暂闪烁以可见。这是我的代码。谁能告诉我为什么一个例子有效而另一个无效,我该如何解决?

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. 要完成这项工作,您必须使您的对象可聚焦并处于焦点。而不是 keyListener 的摇摆方式是使用KeyBinding. Swing 旨在与 keyBindings 一起使用。如何使用键绑定

于 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 回答