当前位置:编程学习 > JAVA >>

求帮助,画小球,小球无法显示出来



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SmallBall extends JFrame
{
private int x = 100;
    private int y = 100;
JPanel p1=new JPanel();//p1用来显示小球
JPanel p2=new JPanel();//p2用来显示上下左右四个按钮
private JButton jbt1=new JButton("Left");
private JButton jbt2=new JButton("Right");
private JButton jbt3=new JButton("Up");
private JButton jbt4=new JButton("Down");                                                                                                                                                                                           
public SmallBall()
{
add(p1,BorderLayout.NORTH);
add(p2,BorderLayout.SOUTH);
p2.add(jbt1);
p2.add(jbt2);
p2.add(jbt3);
p2.add(jbt4);
jbt1.addActionListener(new ButtonListener());
jbt2.addActionListener(new ButtonListener());
jbt3.addActionListener(new ButtonListener());
jbt4.addActionListener(new ButtonListener());
}
class ButtonListener extends JPanel implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jbt1)
{
x-=5;
}
else if(e.getSource()==jbt2)
{
x+=5;
}
else if(e.getSource()==jbt3)
{
y+=5;
}
else
{
y-=5;
}
repaint();
}
protected void paintComponent(Graphics g)
{

super.paintComponent(g);
g.setColor(Color.RED);
g.drawOval(x, y, 20, 20);
}
}
public static void main(String[] args)
{
SmallBall frame=new SmallBall();
frame.setSize(1000,800);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
--------------------编程问答-------------------- 上下左右四个按钮控制小球的移动,但是小球无法显示 --------------------编程问答-------------------- 居然把重新绘制放在listener里面,谁告诉你的啊!
帮你改了一下,自己看看吧!
package com.gloomyfish.swing.rounedpanel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class SmallBall extends JFrame {
/**
 * 
 */
private static final long serialVersionUID = 1L;
private int x = 200;
private int y = 200;

JPanel p2 = new JPanel();// p2用来显示上下左右四个按钮
private JButton jbt1 = new JButton("Left");
private JButton jbt2 = new JButton("Right");
private JButton jbt3 = new JButton("Up");
private JButton jbt4 = new JButton("Down");

public SmallBall() {
getContentPane().setLayout(new BorderLayout());
JPanel p1 = new JPanel(){// p1用来显示小球

/**
 * 
 */
private static final long serialVersionUID = 1L;

protected void paintComponent(Graphics g) {

// super.paintComponent(g);
g.setColor(Color.RED);
g.drawOval(x, y, 20, 20);
}
};
getContentPane().add(p1, BorderLayout.CENTER);
getContentPane().add(p2, BorderLayout.SOUTH);
p2.add(jbt1);
p2.add(jbt2);
p2.add(jbt3);
p2.add(jbt4);
jbt1.addActionListener(new ButtonListener());
jbt2.addActionListener(new ButtonListener());
jbt3.addActionListener(new ButtonListener());
jbt4.addActionListener(new ButtonListener());
}

class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jbt1) {
x -= 5;
} else if (e.getSource() == jbt2) {
x += 5;
} else if (e.getSource() == jbt3) {
y += 5;
} else {
y -= 5;
}
repaint();
}

}

public static void main(String[] args) {
SmallBall frame = new SmallBall();
frame.setSize(600, 600);
//frame.pack();
// frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
--------------------编程问答--------------------

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

public class SmallBall extends JFrame {
private int x = 100;
private int y = 100;
JPanel p1 = new JPanel();// p1用来显示小球
JPanel p2 = new JPanel();// p2用来显示上下左右四个按钮
private JButton jbt1 = new JButton("Left");
private JButton jbt2 = new JButton("Right");
private JButton jbt3 = new JButton("Up");
private JButton jbt4 = new JButton("Down");

public SmallBall() {
add(p1, BorderLayout.NORTH);
add(p2, BorderLayout.SOUTH);
p2.add(jbt1);
p2.add(jbt2);
p2.add(jbt3);
p2.add(jbt4);
jbt1.addActionListener(new ButtonListener());
jbt2.addActionListener(new ButtonListener());
jbt3.addActionListener(new ButtonListener());
jbt4.addActionListener(new ButtonListener());
}
public void paint(Graphics g) {

super.paint(g);
g.setColor(Color.RED);
g.drawOval(x, y, 20, 20);
}

class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jbt1) {
x -= 5;
} else if (e.getSource() == jbt2) {
x += 5;
} else if (e.getSource() == jbt3) {
y += 5;
} else {
y -= 5;
}
repaint();
}
}

public static void main(String[] args) {
SmallBall frame = new SmallBall();
frame.setSize(1000, 800);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
--------------------编程问答-------------------- 代码太长,懒得看 --------------------编程问答--------------------
引用 2 楼 jia20003 的回复:
居然把重新绘制放在listener里面,谁告诉你的啊!
帮你改了一下,自己看看吧!
Java code?1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071……

谢谢,但是为什么不能放在listener里面?我就是对这里不太理解 --------------------编程问答--------------------
引用 3 楼 hwndid 的回复:
Java code?1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556import java.awt.*;import java.awt.event.*;import javax.swing.*; public cla……

3Q
补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,