在绘制的背景中使用JPanel中的JButtons,JLabel和JTextField

时间:2013-03-06 02:42:41

标签: java swing jpanel jbutton paint

我对Java很陌生,但已经在我的程序的其他部分成功使用JPanelsJButtonsJLabelsJTextFields,但我是尝试让JPanel内有一对JButtonsJLabelsJTextFields覆盖在彩绘背景之外时遇到麻烦。

以下是此部分程序的所有相关代码。目前,当程序运行时,唯一会显示的是p1_fireButtonplayer1PowerFieldplayer1AngleField背景的覆盖(即使我有为播放器2创建的组件,我故意注释掉了添加p2_Panel,以便我可以专注于p1_Panel

奇怪的是,那些JComponents只会在程序运行后因为任何原因点击TAB键时显示,我也希望有人可以帮我修复。我的目标是让p1_panel占据左侧橙色框,p2_panel占据右侧橙色框。非常感谢帮助!

enter image description here

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

2 个答案:

答案 0 :(得分:2)

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

有关详细信息,请查看Performing Custom PaintingPainting in AWT and Swing

答案 1 :(得分: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()方法中完成。