2

我对 Java 还是很陌生,但是在我的程序的其他部分中一直在使用JPanels,和成功JButtons,但是当我尝试使用一对, , 和在其中覆盖绘制背景时遇到了麻烦。JLabelsJTextFieldsJPanelJButtonsJLabelsJTextFields

这是程序这一部分的所有相关代码。目前,当程序运行时,唯一会显示的是背景的p1_fireButton,player1PowerFieldplayer1AngleFieldovertop (即使我为玩家 2 创建了组件,我还是特意注释掉了添加p2_Panel以便我可以专注于p1_Panel.

奇怪的是,JComponents 只有在程序出于任何原因运行后我按 TAB 键时才会显示这些内容,我也希望有人能帮我解决这个问题。我的目标是p1_panel占据左边的橙色框和p2_panel占据右边的橙色框。帮助将不胜感激!

在此处输入图像描述

public class GameFrame extends JFrame
{ //start class GameFrame

    ImageIcon background = new ImageIcon("background.jpg");
    ImageIcon terrain1 = new ImageIcon("terrain1.png");
    //ImageIcon tank_red = new ImageIcon("tank_red.png");
    //ImageIcon tank_red = new ImageIcon(player1Tank);

    private int x_rectangle = 50;
    private int y_rectangle = 50;

    private JButton p1_fireButton;
    private JButton p2_fireButton;
    private JPanel p1_Panel;
    private JPanel p2_Panel;

    private JLabel player1PowerLabel;
    private static JTextField player1PowerField;
    private JLabel player1AngleLabel;
    private static JTextField player1AngleField;

    private JLabel player2PowerLabel;
    private static JTextField player2PowerField;
    private JLabel player2AngleLabel;
    private static JTextField player2AngleField;

    String player1Name;
    String player2Name;
    final Timer gameTimer = new Timer(8, new timer());

    Projectile projectile = new Projectile(200, 300);

    public GameFrame(String title)
    { //start GameFrame constructor
        super(title);
        Dimension size = getPreferredSize();
        size.width = 1000;
        setPreferredSize(size);
        setResizable(false);
        setLayout(null);
        Color trans = new Color(0, 0, 0, 0);

        //player1 panel
        p1_Panel = new JPanel();
        p1_Panel.setLayout(null);
        p1_Panel.setBounds(0, 0, 500, 300);
        p1_Panel.setBackground(trans);

        p2_Panel = new JPanel();
        p2_Panel.setLayout(null);
        p2_Panel.setBounds(500, 0, 500, 300);
        p2_Panel.setBackground(trans);

        //player2 panel
        /*p2_fireButtonPanel = new JPanel();
        p2_fireButtonPanel.setBounds(400, 85, 100, 100);
        p2_fireButtonPanel.setBackground(trans);*/

        //player1 angle/power fields
        player1PowerLabel = new JLabel("Power");
        player1PowerLabel.setLayout(null);
        player1PowerLabel.setBounds(400, 20, 50, 50);

        player1PowerField = new JTextField(3);
        player1PowerField.setLayout(null);
        player1PowerField.setBounds(400, 10, 50, 25);

        player1AngleLabel = new JLabel("Angle");
        player1AngleLabel.setLayout(null);
        player1AngleLabel.setBounds(30, 10, 50, 50);

        player1AngleField = new JTextField(3);
        player1AngleField.setLayout(null);
        player1AngleField.setBounds(300, 10, 50, 25);


        //player2 angle/power fields
        player2PowerLabel = new JLabel("Power");
        player2PowerLabel.setLayout(null);
        player2PowerLabel.setBounds(0, 0, 10, 10);

        player2PowerField = new JTextField(3);
        player2PowerField.setLayout(null);
        player2PowerField.setBounds(10, 10, 10, 10);

        player2AngleLabel = new JLabel("Angle");
        player2AngleLabel.setLayout(null);
        player2AngleLabel.setBounds(30, 10, 10, 10);

        player2AngleField = new JTextField(3);
        player2AngleField.setLayout(null);
        player2AngleField.setBounds(60, 10, 10, 10);


        //player1 fire button
        p1_fireButton = new JButton("Fire!");
        p1_fireButton.setLayout(null);
        p1_fireButton.setBounds(430, 70, 50, 50);
        ActionListener fireListener = new fireButtonListener();
        p1_fireButton.addActionListener(fireListener);

        //player2 fire button
        p2_fireButton = new JButton("Fire AGAIN!");
        p2_fireButton.setLayout(null);
        p2_fireButton.setBounds(530, 70, 50, 50);


        //add components to player1 panel
        p1_Panel.add(p1_fireButton);
        p1_Panel.add(player1PowerLabel);
        p1_Panel.add(player1PowerField);
        p1_Panel.add(player1AngleLabel);
        p1_Panel.add(player1AngleField);

        //add components to player2 panel
        p2_Panel.add(p2_fireButton);
        p2_Panel.add(player2PowerLabel);
        p2_Panel.add(player2PowerField);
        p2_Panel.add(player2AngleLabel);
        p2_Panel.add(player2AngleField);

        //add components to GameFrame
        add(p1_Panel);
        //add(p2_Panel);

        projectile.fireProjectile(60, -60 * Math.PI / 180.0);

    } //end GameFrame constructor

    public void paint(Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g;

        Image bg = background.getImage();
        Image t1 = terrain1.getImage();
        Image p1tank = StartPanel.getPlayer1Tank();
        Image p2tank = StartPanel.getPlayer2Tank();
        //Image tank1 = tank_red.getImage();

        g2.drawImage(bg,  0, 0, 1000, 800, this);
        g2.drawImage(t1,  0, 420, 1000, 380, this);


        g2.drawImage(p1tank, 50, 300, 66, 50, null);
        g2.drawImage(p2tank, 500, 300, 66, 50, null);

        player1Name = new String(StartPanel.getPlayer1Name());
        player2Name = new String(StartPanel.getPlayer2Name());

        g.drawString(player1Name, 50, 50);
        g.drawString(player2Name, 525, 50);

        g2.setColor(Color.green);
        g2.fillOval((int)projectile.getXPosition(), (int)projectile.getYPosition(), 15, 15);


    }

    public class timer implements ActionListener
    {

        public void actionPerformed(ActionEvent e)
        {    
            //TanksGUI.gameFrame.moveRectangle(1, 1);
            projectile.advanceProjectile(0.05);

            if (projectile.getYPosition() > 400)
            {
                gameTimer.stop();
            }
            repaint();

        }

    }
    public class fireButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {

            gameTimer.start();
        }
    }


} //end class GameFrame
4

2 回答 2

2
  • 不要扩展JFrame你没有添加任何创建实例无法实现的功能。它还使您的项目不可移植。
  • 不要覆盖paint顶级容器。如果没有其他原因,它们不是双缓冲的,如果你想执行动画,这会给你带来问题。paint顶级容器的链相当复杂,您只是绕过了整个过程。
  • 相反,创建一个自定义组件(从类似的东西JPanel)并将其用作您的主要画布。覆盖它的paintComponent方法并渲染你的背景,确保你super.paintComponent先调用。
  • 确保您放置在此“画布”上的任何容器都是透明的,否则您的背景将不会显示。

查看在 AWT 和 Swing中执行自定义绘画和绘画以获取更多信息

于 2013-03-06T02:50:38.730 回答
2

查看BackgroundPanel以帮助您解决此问题。基本代码是:

JPanel north = new JPanel();
north.add( new JLabel(...) );
north.add( new JTextField(10) );
JPanel gamePanel = new GamePanel();
BackgroundPanel background = new BackgroundPanel(...);
background.add(north, BorderLayout.North);
background.add(gamePanel);
frame.add(background);

GamePanel 是您为游戏角色进行所有自定义绘画的面板。正如 MadProgrammer 所指出的,这种绘制永远不应该在框架的 paint() 方法中完成。

于 2013-03-06T03:42:44.460 回答