0

您好,我们需要向我们的 Jframe 添加多个类:

import javax.swing.JFrame;

public class Concert
{
   public static void main(String[] args)
   {
      JFrame frame = new JFrame();
      frame.setSize(1000, 800);
      frame.setTitle("Concert!");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      Concertbackground component = new Concertbackground();
      frame.add(component);

        BandComponent component1 = new BandComponent();
        frame.add(component1);



      frame.setVisible(true);
   }
}

这里是我们的一些类,我们很困惑如何让我们所有的类同时显示,而不会覆盖之前显示的图形。

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import javax.swing.JComponent;
import java.awt.GradientPaint;

/*
   component that draws the concert background
*/
public class Concertbackground extends JComponent
{  
    public void paintComponent(Graphics g)
    {  

        super.paintComponent(g);

        // Recover Graphics2D 
        Graphics2D g2 = (Graphics2D) g;

        //Background Top
        g2.setColor(Color.BLUE);
        Rectangle backgroundTop = new Rectangle (0, 0, getWidth(), getHeight() / 4); 
        g2.fill(backgroundTop);

        // Background bottom
        g2.setColor(Color.GREEN);
        Rectangle backgroundBottom = new Rectangle (0, getHeight() / 2, getWidth(), getHeight() / 2); 
        g2.fill(backgroundBottom);

        // Speaker base
        g2.setColor(Color.BLACK);
        Rectangle base = new Rectangle (0, 0, 50, 100);
        g2.fill(base);

        // Speakers circles gray top
        g2.setColor(Color.DARK_GRAY);
        Ellipse2D.Double speakerTop = new Ellipse2D.Double(10, 10, 30, 30); 
        g2.fill(speakerTop); 

        //speakers circles black top
        g2.setColor(Color.BLACK);
        Ellipse2D.Double speakerTop1 = new Ellipse2D.Double(15, 15, 20, 20); 
        g2.fill(speakerTop1); 

        // Speakers circles gray bottom
        g2.setColor(Color.DARK_GRAY);
        Ellipse2D.Double speakerBottom = new Ellipse2D.Double(10, 50, 30, 30); 
        g2.fill(speakerBottom); 

        //speakers circles black bottom
        g2.setColor(Color.BLACK);
        Ellipse2D.Double speakerBottom1 = new Ellipse2D.Double(15, 55, 20, 20); 
        g2.fill(speakerBottom1); 


    }

}

public class BandComponent extends JComponent
{  
   public void paintComponent(Graphics g)
   {  
      // Recover Graphics2D 
      Graphics2D g2 = (Graphics2D) g;



      int xScale = 250;
      int yScale = 100;
      int x = 343;
      int y = 343;


      //singer
      Polygon sing = new Polygon();
          sing.addPoint(667 ,208 + xScale);       
          sing.addPoint(676,213 + xScale);  
          sing.addPoint(678,217 + xScale);
          sing.addPoint(682,221 + xScale);
          sing.addPoint(681,224 + xScale);   
          sing.addPoint(680,231 + xScale);
          sing.addPoint(676,242 + xScale);
          sing.addPoint(672,244 + xScale);
          sing.addPoint(672,250 + xScale);
          sing.addPoint(682,248 + xScale);
          sing.addPoint(713,244 + xScale);
          sing.addPoint(734,247 + xScale);
          sing.addPoint(750,247 + xScale);
          sing.addPoint(794,232 + xScale);
          sing.addPoint(800,231 + xScale);       
          sing.addPoint(801,223 + xScale);  
          sing.addPoint(807,219 + xScale);
          sing.addPoint(806,221 + xScale);
          sing.addPoint(806,229 + xScale);
          sing.addPoint(818,222 + xScale);
          sing.addPoint(820,223 + xScale);
          sing.addPoint(825,227 + xScale);
          sing.addPoint(825,240 + xScale);
          sing.addPoint(817,243 + xScale);
          sing.addPoint(807,245 + xScale);
          sing.addPoint(803,247 + xScale);
          sing.addPoint(801,252 + xScale);
          sing.addPoint(781,257 + xScale);
          sing.addPoint(762,264 + xScale);
          sing.addPoint(734,271 + xScale);
          sing.addPoint(701,286 + xScale);
          sing.addPoint(691,296 + xScale);
          sing.addPoint(693,311 + xScale);
          sing.addPoint(690,317 + xScale);
          sing.addPoint(690,335 + xScale);
          sing.addPoint(691,339 + xScale);
          sing.addPoint(689,343 + xScale);
          sing.addPoint(712,382 + xScale);
          sing.addPoint(725,400 + xScale);
          sing.addPoint(731,418 + xScale);
          sing.addPoint(731,428 + xScale);
          sing.addPoint(738,454 + xScale);
          sing.addPoint(741,460 + xScale);
          sing.addPoint(746,468 + xScale);
          sing.addPoint(766,468 + xScale);
          sing.addPoint(771,481 + xScale);//
          sing.addPoint(723,482 + xScale);
          sing.addPoint(720,462 + xScale);
          sing.addPoint(718,454 + xScale);
          sing.addPoint(709,436 + xScale);
          sing.addPoint(703,436 + xScale);
          sing.addPoint(699,417 + xScale);
          sing.addPoint(686,396 + xScale);
          sing.addPoint(678,395 + xScale);
          sing.addPoint(676,437 + xScale);
          sing.addPoint(673,439 + xScale);
          sing.addPoint(638,435 + xScale);
          sing.addPoint(640,398 + xScale);
          sing.addPoint(634,410 + xScale);
          sing.addPoint(625,416 + xScale);
          sing.addPoint(622,436 + xScale);
          sing.addPoint(622,443 + xScale);
          sing.addPoint(615,447 + xScale);
          sing.addPoint(609,456 + xScale);
          sing.addPoint(606,481 + xScale);//
          sing.addPoint(557,481 + xScale);
          sing.addPoint(560,467 + xScale);
          sing.addPoint(579,467 + xScale);
          sing.addPoint(587,464 + xScale);
          sing.addPoint(593,452 + xScale);
          sing.addPoint(594,441 + xScale);
          sing.addPoint(592,434 + xScale);
          sing.addPoint(600,416 + xScale);
          sing.addPoint(608,405 + xScale);
          sing.addPoint(609,394 + xScale);
          sing.addPoint(617,376 + xScale);
          sing.addPoint(619,363 + xScale);
          sing.addPoint(632,334 + xScale);
          sing.addPoint(637,324 + xScale);
          sing.addPoint(635,314 + xScale);
          sing.addPoint(639,296 + xScale);
          sing.addPoint(627,285 + xScale);
          sing.addPoint(600,279 + xScale);
          sing.addPoint(582,278 + xScale);
          sing.addPoint(575,275 + xScale);
          sing.addPoint(546,256 + xScale);
          sing.addPoint(536,252 + xScale);
          sing.addPoint(533,350 + xScale);
          sing.addPoint(534,361 + xScale);
          sing.addPoint(532,367 + xScale);
          sing.addPoint(529,369 + xScale);
          sing.addPoint(524,363 + xScale);
          sing.addPoint(525,355 + xScale);
          sing.addPoint(531,254 + xScale);
          sing.addPoint(527,249 + xScale);
          sing.addPoint(527,242 + xScale);
          sing.addPoint(529,237 + xScale);
          sing.addPoint(532,237 + xScale);
          sing.addPoint(536,178 + xScale);
          sing.addPoint(534,129 + xScale);
          sing.addPoint(535,123 + xScale);
          sing.addPoint(541,120 + xScale);
          sing.addPoint(545,123 + xScale);
          sing.addPoint(547,131 + xScale);
          sing.addPoint(545,173 + xScale);
          sing.addPoint(538,233 + xScale);
          sing.addPoint(549,239 + xScale);
          sing.addPoint(558,241 + xScale);
          sing.addPoint(585,257 + xScale);
          sing.addPoint(599,257 + xScale);
          sing.addPoint(627,254 + xScale);
          sing.addPoint(647,251 + xScale);
          sing.addPoint(653,248 + xScale);
          sing.addPoint(652,235 + xScale);
          sing.addPoint(648,226 + xScale);
          sing.addPoint(652,218 + xScale);
          sing.addPoint(661,212 + xScale);

            g2.setColor(Color.black);
            g2.fill(sing);
            g2.draw(sing);
      //guitar
      Polygon guitar = new Polygon();

         guitar.addPoint(148,28);
         guitar.addPoint(158,32);
         guitar.addPoint(164,38);
         guitar.addPoint(168,46);
         guitar.addPoint(169,52);
         guitar.addPoint(167,60);
         guitar.addPoint(164,65);
         guitar.addPoint(165,70);
         guitar.addPoint(161,76);
         guitar.addPoint(158,92);
         guitar.addPoint(162,97);
         guitar.addPoint(161,102);
         guitar.addPoint(158,106);
         guitar.addPoint(155,108);
         guitar.addPoint(151,127);
         guitar.addPoint(152,133);
         guitar.addPoint(155,137);
         guitar.addPoint(151,146);
         guitar.addPoint(153,147);
         guitar.addPoint(160,142);
         guitar.addPoint(162,133);
         guitar.addPoint(162,123);
         guitar.addPoint(161,113);
         guitar.addPoint(162,110);
         guitar.addPoint(164,117);
         guitar.addPoint(169,131);
         guitar.addPoint(171,144);
         guitar.addPoint(170,159);
         guitar.addPoint(166,167);
         guitar.addPoint(166,171);
         guitar.addPoint(174,174);
         guitar.addPoint(183,184);
         guitar.addPoint(191,195);
         guitar.addPoint(196,198);
         guitar.addPoint(198,200);
         guitar.addPoint(199,210);
         guitar.addPoint(211,225);
         guitar.addPoint(212,233);
         guitar.addPoint(220,248);
         guitar.addPoint(233,260);
         guitar.addPoint(245,266);
         guitar.addPoint(248,268);
         guitar.addPoint(249,277);
         guitar.addPoint(205,275);
         guitar.addPoint(204,262);
         guitar.addPoint(187,238);
         guitar.addPoint(178,224);
         guitar.addPoint(177,216);
         guitar.addPoint(156,201);
         guitar.addPoint(146,197);
         guitar.addPoint(134,211);
         guitar.addPoint(128,229);
         guitar.addPoint(125,244);//
         guitar.addPoint(121,246);
         guitar.addPoint(107,248);
         guitar.addPoint(100,252);
         guitar.addPoint(97,258);
         guitar.addPoint(96,253);
         guitar.addPoint(89,258);
         guitar.addPoint(65,267);
         guitar.addPoint(63,274);
         guitar.addPoint(64,283);
         guitar.addPoint(41,282);
         guitar.addPoint(44,270);
         guitar.addPoint(47,264);
         guitar.addPoint(51,255);
         guitar.addPoint(73,238);
         guitar.addPoint(79,228);
         guitar.addPoint(97,222);
         guitar.addPoint(101,204);
         guitar.addPoint(102,181);
         guitar.addPoint(100,170);
         guitar.addPoint(95,161);
         guitar.addPoint(97,154);
         guitar.addPoint(91,152);
         guitar.addPoint(77,131);
         guitar.addPoint(65,123);
         guitar.addPoint(61,105);
         guitar.addPoint(64,94);
         guitar.addPoint(72,91);
         guitar.addPoint(78,82);
         guitar.addPoint(78,76);
         guitar.addPoint(70,73);
         guitar.addPoint(70,67);
         guitar.addPoint(93,51);
         guitar.addPoint(101,48);
         guitar.addPoint(111,52);
         guitar.addPoint(118,59);
         guitar.addPoint(119,70);
         guitar.addPoint(117,78);
         guitar.addPoint(113,79);
         guitar.addPoint(112,86);
         guitar.addPoint(111,88);
         guitar.addPoint(109,89);
         guitar.addPoint(109,92);
         guitar.addPoint(122,99);//
         guitar.addPoint(124,99);
         guitar.addPoint(133,96);
         guitar.addPoint(145,93);
         //guitar.addPoint(138,124);
         guitar.addPoint(150,69);
         guitar.addPoint(150,62);
         guitar.addPoint(155,58);
         guitar.addPoint(154,53);
         guitar.addPoint(149,50);
         guitar.addPoint(154,46);
         guitar.addPoint(153,38);
         guitar.addPoint(147,28);

            g2.setColor(Color.black);
            g2.fill(guitar);
            g2.draw(guitar);


         Polygon guitar2 = new Polygon ();
         guitar2.addPoint(141,108);
         guitar2.addPoint(139,126);
         guitar2.addPoint(135,122);
         guitar2.addPoint(128,122);
         guitar2.addPoint(129,116);
         guitar2.addPoint(143,108);

            g2.setColor(Color.white);
            g2.fill(guitar2);
            g2.draw(guitar2);



         //bass guitar
        Polygon bassgt = new Polygon ();

         bassgt.addPoint(871,21);
         bassgt.addPoint(879,24);
         bassgt.addPoint(885,32);
         bassgt.addPoint(886,42);
         bassgt.addPoint(895,47);
         bassgt.addPoint(904,56);
         bassgt.addPoint(907,69);
         bassgt.addPoint(909,83);
         bassgt.addPoint(910,91);
         bassgt.addPoint(941,81);
         bassgt.addPoint(946,75);
         bassgt.addPoint(945,67);
         bassgt.addPoint(950,67);
         bassgt.addPoint(955,75);
         bassgt.addPoint(960,68);
         bassgt.addPoint(963,74);
         bassgt.addPoint(967,72);
         bassgt.addPoint(971,66);
         bassgt.addPoint(973,70);
         bassgt.addPoint(981,67);
         bassgt.addPoint(984,71);
         bassgt.addPoint(982,76);
         bassgt.addPoint(987,80);
         bassgt.addPoint(986,82);
         bassgt.addPoint(980,83);
         bassgt.addPoint(979,90);
         bassgt.addPoint(974,85);
         bassgt.addPoint(970,86);
         bassgt.addPoint(973,91);
         bassgt.addPoint(965,86);
         bassgt.addPoint(960,90);
         bassgt.addPoint(961,100);
         bassgt.addPoint(955,92);
         bassgt.addPoint(944,91);
         bassgt.addPoint(907,103);
         bassgt.addPoint(906,109);
         bassgt.addPoint(893,114);
         bassgt.addPoint(895,123);
         bassgt.addPoint(900,131);
         bassgt.addPoint(904,134);
         bassgt.addPoint(908,145);
         bassgt.addPoint(911,159);
         bassgt.addPoint(918,171);
         bassgt.addPoint(919,190);
         bassgt.addPoint(923,198);
         bassgt.addPoint(919,201);
         bassgt.addPoint(919,210);
         bassgt.addPoint(927,220);
         bassgt.addPoint(942,226);
         bassgt.addPoint(944,234);
         bassgt.addPoint(909,230);
         bassgt.addPoint(905,214);
         bassgt.addPoint(899,204);
         bassgt.addPoint(893,203);
         bassgt.addPoint(889,171);
         bassgt.addPoint(877,151);
         bassgt.addPoint(861,152);
         bassgt.addPoint(852,169);
         bassgt.addPoint(849,203);
         bassgt.addPoint(841,210);
         bassgt.addPoint(840,228);
         bassgt.addPoint(828,233);
         bassgt.addPoint(806,235);
         bassgt.addPoint(805,228);
         bassgt.addPoint(822,219);
         bassgt.addPoint(824,204);
         bassgt.addPoint(817,201);
         bassgt.addPoint(822,196);
         bassgt.addPoint(822,184);
         bassgt.addPoint(828,162);
         bassgt.addPoint(829,152);
         bassgt.addPoint(820,149);
         bassgt.addPoint(811,144);
         bassgt.addPoint(806,134);
         bassgt.addPoint(805,117);
         bassgt.addPoint(820,107);
         bassgt.addPoint(819,89);
         bassgt.addPoint(811,83);
         bassgt.addPoint(811,77);
         bassgt.addPoint(824,66);
         bassgt.addPoint(825,61);
         bassgt.addPoint(842,53);
         bassgt.addPoint(852,43);
         bassgt.addPoint(853,29);
         bassgt.addPoint(870,20);

            g2.setColor(Color.black);
            g2.fill(bassgt);
            g2.draw(bassgt);

         Polygon bassgt2 = new Polygon();   
         bassgt2.addPoint(845,78);
         bassgt2.addPoint(845,98);
         bassgt2.addPoint(843,98);
         bassgt2.addPoint(842,105);
         bassgt2.addPoint(839,109);
         bassgt2.addPoint(834,103);
         bassgt2.addPoint(832,85);
         bassgt2.addPoint(845,78);

            g2.setColor(Color.white);
            g2.fill(bassgt2);
            g2.draw(bassgt2);




         Polygon drums = new Polygon ();


         drums.addPoint(713,104);
         drums.addPoint(706,121);
         drums.addPoint(721,377);
         drums.addPoint(248,380);
         drums.addPoint(253,228);
         drums.addPoint(250,206);
         drums.addPoint(237,178);
         drums.addPoint(206,166);
         drums.addPoint(201,154);
         drums.addPoint(198,152);
         drums.addPoint(208,148);
         drums.addPoint(236,150);
         drums.addPoint(247,130);
         drums.addPoint(227,119);
         drums.addPoint(219,105);
         drums.addPoint(222,96);
         drums.addPoint(233,88);
         drums.addPoint(251,84);
         drums.addPoint(272,83);
         drums.addPoint(300,91);
         drums.addPoint(285,72);
         drums.addPoint(294,57);
         drums.addPoint(319,46);
         drums.addPoint(372,45);
         drums.addPoint(406,50);
         drums.addPoint(428,65);
         drums.addPoint(433,74);
         drums.addPoint(450,58);
         drums.addPoint(478,48);
         drums.addPoint(514,48);
         drums.addPoint(544,51);
         drums.addPoint(566,52);
         drums.addPoint(577,67);
         drums.addPoint(575,79);
         drums.addPoint(561,95);
         drums.addPoint(545,98);
         drums.addPoint(525,105);
         drums.addPoint(524,147);
         drums.addPoint(524,183);
         drums.addPoint(645,175);
         drums.addPoint(662,143);
         drums.addPoint(617,152);
         drums.addPoint(608,148);
         drums.addPoint(614,139);
         drums.addPoint(633,128);
         drums.addPoint(661,116);
         drums.addPoint(659,107);
         drums.addPoint(625,114);
         drums.addPoint(592,113);
         drums.addPoint(571,111);
         drums.addPoint(565,102);
         drums.addPoint(576,86);
         drums.addPoint(616,70);
         drums.addPoint(647,66);
         drums.addPoint(679,67);
         drums.addPoint(695,72);
         drums.addPoint(699,90);
         drums.addPoint(678,100);
         drums.addPoint(667,103);
         drums.addPoint(672,113);
         drums.addPoint(689,105);
         drums.addPoint(709,106);

            g2.setColor(Color.black);
            g2.fill(drums);
            g2.draw(drums);



         }
}
4

2 回答 2

3

JFrame默认情况下用作BorderLayout布局管理器。添加 2 个组件并BorderLayout使用时(未指定其他选项),则第二个组件将替换旧组件。您需要指定要将组件添加到的区域,例如:

frame.add(component, BorderLayout.CENTER);
frame.add(component1, BorderLayout.SOUTH);

或使用另一个布局管理器。


看:

于 2013-11-01T17:53:28.850 回答
2

的默认布局是 BorderLayout,当你这样做时:

Concertbackground component = new Concertbackground();
      frame.add(component);
    BandComponent component1 = new BandComponent();
    frame.add(component1);

你在同一个地方添加组件和组件1。

检查 BorderLayout 使用情况或使用其他布局正确显示组件。

于 2013-11-01T17:54:39.120 回答