如何在Java中将图形添加到其他面板

时间:2014-05-19 13:39:01

标签: java graphics jframe jpanel

import java.awt.event.ActionEvent; //this is my button class
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;
import java.awt.Color;
import java.awt.Dimension;    
public class Buttons extends JFrame{
    private JPanel panel1, panel2;
    private JButton button1, button2, button3; 
    private JMenuBar menuBar;
    public Buttons()
    {
       createPanel();
       addPanel();
    }
    private void createPanel()
    {
     setLocationRelativeTo(null);
        panel1 = new JPanel();
        panel1.setBackground(Color.cyan);

        button1 = new JButton("Start");
        button1.addActionListener(new addButtonListener());
        button1.setBounds(50, 90, 190, 30);

        button2 = new JButton("Instructions");
        button2.setBounds(70, 130, 160, 30);

        panel2 = new JPanel();
        //button3 = new JButton("Test");
        //panel2.setBackground(Color.orange);
        //button3.setBounds(50, 50, 90, 30);

    }

    private void addPanel()
    {
        panel1.add(button1);
        panel1.add(button2);
        panel2.add(button3);          
        add(panel1);
    }
    class addButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent ae) 
        {
            getContentPane().removeAll();
            getContentPane().add(panel2);
            repaint();
            printAll(getGraphics());



        }
    }
    public static void main(String[]args)
    {


     JMenuItem exitAction  = new JMenuItem("Exit");
     JMenuBar menuBar = new JMenuBar();
     JMenu save = new JMenu("Save");
     JMenu file = new JMenu("File");
     JMenu credit = new JMenu("Credit");
     file.add(exitAction);
     menuBar.add(file);
     menuBar.add(save);
     menuBar.add(credit);
     exitAction.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
         System.exit(0);
         }
     });



        Buttons frame = new Buttons();
        frame.setTitle("Bikini Bottom Marathon");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600,500);
        frame.setVisible(true);
        frame.setJMenuBar(menuBar);

    }
}
















 import javax.swing.*; 
 //this is my graphics class to create a board. the images are imported from saved   
 pictures in my class file. I'm trying to add the output from this code (the board)    
 onto the buttons class
 import java.applet.*;
 import java.awt.*;
 import javax.imageio.*;
 import java.net.*;
 import java.io.*;
 import java.awt.image.*;
 import java.applet.*;
 public class compscigame extends Applet
 {


public void paint (Graphics page)
{




  Image pineapple = getImage(getCodeBase(), "pineapple.jpg");
  page.drawImage (pineapple, 50, 500, 50, 50, this);

  Image raygun = getImage(getCodeBase(), "Raygun.jpg");
  page.drawImage (raygun, 300, 350, 50, 50, this); 

  Image karen = getImage(getCodeBase(), "Karen.jpg");
  page.drawImage(karen, 100, 50, 50, 50, this);    

  Image karensmad = getImage(getCodeBase(), "karensmad.jpg");
  page.drawImage(karensmad, 150, 400, 50, 50, this);

  Image rocketboots = getImage(getCodeBase(), "rocketboots.jpg");
  page.drawImage(rocketboots, 200, 300, 50, 50, this);

  Image emptybeaker = getImage(getCodeBase(), "emptybeaker.jpg");
  page.drawImage(emptybeaker, 350, 150, 50, 50, this);

  Image happykaren = getImage(getCodeBase(), "happykaren.jpg");
  page.drawImage(happykaren, 100, 50, 50, 50, this);

  Image raygunnotshooting = getImage(getCodeBase(), "raygunnotshooting.jpg");
  page.drawImage (raygunnotshooting, 450, 200, 50, 50, this);

  Image notfirerocket = getImage(getCodeBase(), "notfirerocket.jpg");
  page.drawImage (notfirerocket, 450, 450, 50, 50, this);

  Image firerocket = getImage(getCodeBase(), "firerocket.jpg");
  page.drawImage (firerocket, 500, 200, 50, 50, this);

  Image fullbeaker = getImage(getCodeBase(), "fullbeaker.jpg");
  page.drawImage (fullbeaker, 250, 250, 50, 50, this);

  Image boots = getImage(getCodeBase(), "boots.jpg");
  page.drawImage (boots, 300, 500, 50, 50, this);

  Image krustykrab = getImage(getCodeBase(), "krustykrab.jpg");
  page.drawImage(krustykrab, 50, 50, 50, 50, this);

  Board board = new Board(page, new Color (255,0,100));
  setBackground(Color.YELLOW);

}

class Board
{
    private Graphics g;
    private Color col;
    private Square [][] squares;
    public Board (Graphics g, Color col)
    {
      this.col = col;
      this.g = g;
      Color temp;
      squares = new Square[10][10];
      for (int i = 1; i <= 10; i++)
      {
        for (int j = 1; j <= 10; j++)
        {
          int num;
          if (i + j % 2 == 0)
            temp = col;
          else
            temp = new Color(255, 0 , 255);   
          Square t;
          t = new Square (g, temp, i, j);
          if (j % 2 != 0)
          {
            num = 10 - i + 1 + 10 * (j - 1);
          }
          else
          {
            num = i + 10 * (j - 1);
          }
          t.setVal(num);
          t.drawSquare();
          squares[i - 1][j - 1] = t;
        }
      }
    }
  }

   class Square
  {
    private Color col;
    private int x;
    private int y;
    private Graphics g;
    private int val;
    public Square (Graphics g, Color col, int x, int y)
    {
      this.col = col;
      this.g = g;
      this.x = x;
      this.y = y;
      val = 0;
    }
    public void setVal(int num)
    {
      val = num;
    }
    public void drawSquare()
    {
      g.setColor(col);
      g.drawRect(50 + (10 - x) * 50, 50 + (10 - y) * 50, 50, 50);
      String str = "" + val;
      g.drawString(str, 50 + (10 - x) * 50 + 5, 50 + (10 - y) * 50 + 10);
    }
 } 

我只想让董事会在另一个小组中。如您所见,我使用ActionListener来创建我的按钮将执行的操作。当您单击开始时,它会转到显示&#34; test。&#34;的按钮。我希望董事会的图形显示在第二个面板上,(面板2)       }

2 个答案:

答案 0 :(得分:0)

创建一个自定义内部类,扩展JPanel,然后覆盖onPaintComponent方法。在那里画板。创建一个新的BoardPanel而不是JPanel。

答案 1 :(得分:0)

将您的绘画代码放在一个单独的类(JPanel)

public void paint (Graphics page)
{
...
}

applet不需要依赖任何此代码。