- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
您好,我们需要向 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);
}
}
最佳答案
JFrame
默认使用 BorderLayout
作为布局管理器。当添加 2 个组件并使用 BorderLayout
(不指定其他选项)时,第二个组件将替换旧的组件。您需要指定要将组件添加到的区域,例如:
frame.add(component, BorderLayout.CENTER);
frame.add(component1, BorderLayout.SOUTH);
或使用其他布局管理器。
<小时/>参见:
关于java - 向 Jframe 添加多个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19732348/
我正在尝试将 JFrame (bar) 添加到 JFrame (foo),这会强制 bar 在之前关闭foo 可以再次集成。 例如,在我可以再次编写代码之前,必须关闭“About Eclipse”框架
我有一个名为 User 的 JFrame,在其中声明一个名为 id 的变量,并根据某些条件将其设置为特定值。 我需要在称为输出的第二个 JFrame 中使用此变量。 这是我的代码 class Inpu
我有这个问题:我有一个 ArrayList,其中包含一些我想在特定框架中使用的项目,问题是数组列表通过在主类中初始化而变满我的项目。在本类(class)中,我还启动了与其他框架链接的起始框架(登录框架
如何在隐藏一个 Swing 程序的同时将一个 javax.Swing 程序移动到另一个 Swing 程序,以及如何制作可滚动的 JFrame。请帮助我。 最佳答案 在这里你可以找到一个非常good e
所以基本上我有一个 2D 游戏(基本上是过去的仿制品的链接),当你按 e 键时,库存会打开,关闭时会隐藏。问题是,每次我按 e 键时,它都会打开一个包含所有初始值的新库存,我希望它在初始值之后打开一个
我有一个 JFrame(1,主框架),它延续了 JPanel 和 JTable 以及数据。用户可以编辑数据。因此,一个新的 JFrame (2) 打开,用户可以输入新数据。如果他们在第二帧中单击“确定
我有一个新的netbeans“Java应用程序”项目,我试图从主JFrame添加第二个JFrame,用户可以从中加载文件。 所以我有主要的 JFrame main 方法 public static v
我的程序应该在单击按钮时启动第二个 JFrame 并打印一条语句,但它总是启动三个 JFrame 并打印三个语句。我只需要它来打印出一份声明并启动一个 Jframe。这是代码: import java
我目前已经构建了一个使用多个框架的应用程序。但如果我能在 1 帧中使用所有我用过的帧,那就太好了。如下图所示。 因此,如果您按左侧按钮“Speler Overzicht”,它将在右侧面板中向用户显示,
我目前有一个按钮,单击该按钮时,会执行一种方法,该方法创建一个带有加载多个图像的面板的 jframe。如果多次单击此按钮,图像将继续添加到加载到 jframe 上的现有图像上。我应该使用什么代码,以便
为什么无法将JFrame添加到JFrame中?它只是将一个组件添加到容器中。 Java 如何禁止我这样做?是的,我知道这样做没有意义,但我的问题的重点是了解 Swing 机制 - 它在幕后是如何工作的
我创建了一个生成 StartUpWindow 对象的类。其中一个 JButton 组件关闭 JFrame,然后调用一种新类型的框架进行实例化,AdminMainWindow。但是,当 AdminMai
我试图在不显示 JFrame 本身的情况下将 JFrame 渲染为图像(类似于 this 问题所问的内容)。我试过使用这段代码: private static BufferedImage getScr
我正在使用 XFCE 4 桌面的 Debian 8 上编译并运行以下代码。 import javax.swing.JFrame; import javax.swing.JComponent; impo
我有 14 个 JFrame 类和一个 JFrame 类,其中包含一个充当日历的 JTable。我希望当用户从 JTable 日历中选择日期时,更改 14 个 JFrame 类中任何一个文本字段或任何
我有 3 个扩展 JFrame 的对象让我们调用他们 FrameA FrameB FrameC . FrameA是我的主应用程序窗口。来自 FrameA的构造函数如果应用程序未注册我创建 FrameB
我试图简单地创建四个 jtextfields 和一个 jbutton。按下按钮后,我希望输入到 jtextfields 的文本作为参数(p、var、s、f)传递到另一个窗口,该窗口使用给定的参数显示数
这个问题已经有答案了: Close one JFrame without closing another? (2 个回答) 已关闭 6 年前。 我制作了一个程序,其中存在三个jframe。但问题是,当
我正在用 java 构建一个桌面学生管理应用程序。所以我需要关闭当前的 JFRAME,同时另一个 JFRAME 可见(关闭新的 jframe 后,旧的 jframe 应该可见)。你能帮我一下吗? 最佳
我正在开发的应用程序包含一个主 JFrame,用户最终可能会从中打开另一个补充框架。我正在尝试实现应用程序的这种行为,其中一旦主框架最小化,补充框架就会最小化(图标化)。 我正在考虑重写主框架的 se
我是一名优秀的程序员,十分优秀!