大家帮忙看看问题出在哪里?

hnalenx 2010-01-18 09:48:05

import java.awt.*;
import javax.swing.*;
public class CrossLight extends JPanel{
private int width=getWidth();
private int height=getHeight();
private boolean Red,Yellow,Green;
private boolean isRed(){
return Red;
}
protected void setRed(boolean Red){
this.Red=Red;
repaint();
}
protected boolean isYellow(){
return Yellow;
}
protected void setYellow(boolean Yellow){
this.Yellow=Yellow;
repaint();
}
protected boolean isGreen(){
return Green;
}
protected void setGreen(boolean Green){
this.Green=Green;
repaint();
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
int width_Rect=width/3;
//int height_Rect=height/2;
int x_Rect=width/2;
int y_Rect=height/2;
int x_Red=x_Rect;
int y_Red=y_Rect;
int x_Yellow=x_Rect;
int y_Yellow=y_Red+width_Rect;
int x_Green=x_Rect;
int y_Green=y_Yellow+width_Rect;
g.setColor(Color.BLACK);
g.drawRect(x_Rect,y_Rect,width_Rect,3*width_Rect);
if(Red){
g.setColor(Color.RED);
g.fillOval(x_Red,y_Red,width_Rect,width_Rect);
}
else{
g.drawOval(x_Red,y_Red,width_Rect,width_Rect);
}
if(Yellow){
g.setColor(Color.YELLOW);
g.fillOval(x_Yellow,y_Yellow,width_Rect,width_Rect);
}
else{
g.drawRect(x_Yellow,y_Yellow,width_Rect,width_Rect);
}
if(Green){
g.setColor(Color.GREEN);
g.fillOval(x_Green,y_Green,width_Rect,width_Rect);
}
else{
g.drawOval(x_Green,y_Green,width_Rect,width_Rect);
}
}
public Dimension getPreferredSize(){
return new Dimension(60,90);
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ControlLight extends JFrame{
private JRadioButton jrbRed=new JRadioButton("Red");
private JRadioButton jrbYellow=new JRadioButton("Yellow");
private JRadioButton jrbGreen=new JRadioButton("Green");
private CrossLight light=new CrossLight();
public ControlLight(){
JPanel jpbutton=new JPanel(new FlowLayout(FlowLayout.CENTER,2,0));
jpbutton.add(jrbRed);
jpbutton.add(jrbYellow);
jpbutton.add(jrbGreen);
ButtonGroup group=new ButtonGroup();
group.add(jrbRed);
group.add(jrbYellow);
group.add(jrbGreen);
add(jpbutton,BorderLayout.SOUTH);
add(light);
jrbRed.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
light.setRed(true);
}
});
jrbYellow.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
light.setYellow(true);
}
});
jrbGreen.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
light.setGreen(true);
}
});
}
public static void main(String[] args){
ControlLight frame=new ControlLight();
frame.setTitle("ControlLight");
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}



大家帮我看看为什么CrossLight的实例在编译运行后主类中显示不出来?
...全文
203 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
zuoyuxing 2010-01-20
  • 打赏
  • 举报
回复
顶....我现在还看不太懂,呵呵
cbajing 2010-01-20
  • 打赏
  • 举报
回复
swing..............
岁月之梦 2010-01-20
  • 打赏
  • 举报
回复
swing 忘光了 sorry 给点分让我升星星吧!
SambaGao 2010-01-20
  • 打赏
  • 举报
回复
做swing的越来越多了。
最近java 6 update 18 对swing优化了很多。大家可以去看看。
huangwj20042008 2010-01-20
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 hnalenx 的回复:]
huangwj20042008同志,给我解析一下为什么我向下面这样写
int width_Rect=width/3;
    //int height_Rect=height/2;
    int x_Rect=width/2;
    int y_Rect=height/2;
所获取的值是空啊?麻烦了!!

[/Quote]

width,height是在初始化的时候赋的值,而构建panel的时候,getWidth(),getHeight()返回的是0。
hnalenx 2010-01-20
  • 打赏
  • 举报
回复
huangwj20042008同志,给我解析一下为什么我向下面这样写
int width_Rect=width/3;
//int height_Rect=height/2;
int x_Rect=width/2;
int y_Rect=height/2;
所获取的值是空啊?麻烦了!!
子龙奶爸 2010-01-19
  • 打赏
  • 举报
回复
顶一下吧
huangwj20042008 2010-01-19
  • 打赏
  • 举报
回复
int width_Rect=width/3;
//int height_Rect=height/2;
int x_Rect=width/2;
int y_Rect=height/2;

改成下面这样:
int width_Rect=getWidth()/3;
//int height_Rect=height/2;
int x_Rect=getWidth()/2;
int y_Rect=getHeight()/2;

pywepe 2010-01-19
  • 打赏
  • 举报
回复
顶一下吧
hnalenx 2010-01-19
  • 打赏
  • 举报
回复
#1楼的大哥给我讲一下原因吧,这样改一下我也不知道错在哪里,麻烦了!!
999朵玫瑰 2010-01-18
  • 打赏
  • 举报
回复
serialVersionUID
xiesisi3 2010-01-18
  • 打赏
  • 举报
回复
package defaultpackage;

import java.awt.*;
import javax.swing.*;

public class CrossLight extends JPanel {
/**
*
*/
private static final long serialVersionUID = 3545860909727139358L;

private int width = 200;

private int height = 200;

private boolean Red, Yellow, Green;

public CrossLight(){
this.setSize(200, 200);
this.setLocation(100, 100);
setVisible(true);
}

private boolean isRed() {
return Red;
}

protected void setRed(boolean Red) {
this.Red = Red;
repaint();
}

protected boolean isYellow() {
return Yellow;
}

protected void setYellow(boolean Yellow) {
this.Yellow = Yellow;
repaint();
}

protected boolean isGreen() {
return Green;
}

protected void setGreen(boolean Green) {
this.Green = Green;
repaint();
}

public void paint(Graphics g) {
super.paintComponent(g);
int width_Rect = width / 3;
// int height_Rect=height/2;
int x_Rect = width / 2;
int y_Rect = height / 2;
int x_Red = x_Rect;
int y_Red = y_Rect;
int x_Yellow = x_Rect;
int y_Yellow = y_Red + width_Rect;
int x_Green = x_Rect;
int y_Green = y_Yellow + width_Rect;
g.setColor(Color.BLACK);
g.drawRect(x_Rect, y_Rect, width_Rect, 3 * width_Rect);
if (Red) {
g.setColor(Color.RED);
g.fillOval(x_Red, y_Red, width_Rect, width_Rect);
repaint();
} else {
g.drawOval(x_Red, y_Red, width_Rect, width_Rect);
}
if (Yellow) {
g.setColor(Color.YELLOW);
g.fillOval(x_Yellow, y_Yellow, width_Rect, width_Rect);
} else {
g.drawRect(x_Yellow, y_Yellow, width_Rect, width_Rect);
repaint();
}
if (Green) {
g.setColor(Color.GREEN);
g.fillOval(x_Green, y_Green, width_Rect, width_Rect);
} else {
g.drawOval(x_Green, y_Green, width_Rect, width_Rect);
repaint();
}
}

public Dimension getPreferredSize() {
return new Dimension(60, 90);
}

public static void main(String[] args) {
CrossLight cl = new CrossLight();
cl.setRed(true);
JFrame jf = new JFrame();
jf.setLocation(300, 300);
jf.setSize(300, 300);
jf.add(cl);
jf.setVisible(true);
}
}
package defaultpackage;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class ControlLight extends JFrame {
/**
*
*/
private static final long serialVersionUID = 7839937459494006808L;

private JRadioButton jrbRed = new JRadioButton("Red");

private JRadioButton jrbYellow = new JRadioButton("Yellow");

private JRadioButton jrbGreen = new JRadioButton("Green");

private CrossLight light = new CrossLight();

public ControlLight() {
JPanel jpbutton = new JPanel(new FlowLayout(FlowLayout.CENTER, 2, 0));
jpbutton.add(jrbRed);
jpbutton.add(jrbYellow);
jpbutton.add(jrbGreen);
ButtonGroup group = new ButtonGroup();
group.add(jrbRed);
group.add(jrbYellow);
group.add(jrbGreen);
setSize(500, 500);
setLayout(new BorderLayout());
add(jpbutton, BorderLayout.NORTH);
// light.setRed(true);
add(light, BorderLayout.CENTER);
jrbRed.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
light.setRed(true);
}
});
jrbYellow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
light.setYellow(true);
}
});
jrbGreen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
light.setGreen(true);
}
});
}

public static void main(String[] args) {
ControlLight frame = new ControlLight();
frame.setTitle("ControlLight");
// frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

我改了一下,现在可以显示了。
内容概要:本文档系统讲解了创意版烟花的完整实现路径,从粒子系统原理发,深入剖析烟花效果的五大核心阶段——上升、爆炸、扩散、衰减与拖尾,并基于四种技术栈(HTML5 Canvas、Three.js、Python Pygame、AI音乐节拍同步)提供可运行的完整代码方案。文档涵盖基础实现、视觉增强(形状变化、闪烁、二次爆炸)、交互升级(鼠标拖动、手势控制)、性能优化(对象池、渲染优化)、部署上线(GitHub Pages、Vercel)及创意拓展(文字烟花、数据可视化、协同互动),形成“原理→编码→调优→部署→创新”的闭环学习链路。同时融入Web Audio API节拍检测、滑动窗口动态阈值等实用算法,助力开发者打造兼具美观性与技术深度的动态视觉作品。; 适合人群:具备基础编程能力的前端开发者、Python爱好者、多媒体交互设计人员,以及希望提升图形编程与动效设计能力的工作1-3年研发人员;也适用于教学演示、作品集建设或创意项目原型开发。; 使用场景及目标:①掌握粒子系统在动画与游戏开发中的底层实现机制;②实现网页端与桌面端的高性能烟花特效;③构建音乐可视化、数据艺术、互动装置等融合型项目;④学习从代码实现到线上部署的全流程工程实践。; 阅读建议:建议按照“Canvas基础→进阶优化→3D/Pygame/AI扩展”的路径逐步实践,重点关注参数调优表与性能优化策略,在调试中理解每行代码的作用;对于音乐同步等复杂功能,可先运行成功案例再深入算法逻辑,结合实际项目需求灵活组合各项技术模块。

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧