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);
}
}
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);
}
}
帮你改了一下,自己看看吧!
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);
}
}