Java论坛网»Java技术»关于Swing相当有难度的问题??
关于Swing相当有难度的问题??
问?:
界面包括四个小按钮以及一个大的显示区域,其中一个按钮用来退出程序,另外三个按钮的功能自行定义。在显示区域里面显示一个圆形,开始的时候在屏幕的中间。当用户按下上下左右箭头的时候,该圆形向相应的方向移动。每次按键导致的移动量自行决定。
答!: 1:
很难???
把圆形放label上
然后键盘事件 改变label的位置
把圆形放label上
然后键盘事件 改变label的位置
答!: 2:
/*
* DrawFrame.java
*
* Created on 2006年11月1日, 上午9:04
*/
import javax.swing.*;
import java.awt.*;
/**
*
* @author Administrator
*/
public class DrawFrame extends javax.swing.JFrame
{
/** Creates new form DrawFrame */
public DrawFrame()
{
initComponents();
int px = 100;
int py = 100;
int r = 50;
step = 10;
circle = new MyCircle(px, py, r);
drawPanel = new DrawPanel(circle);
this.getContentPane().add(drawPanel, BorderLayout.CENTER);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" 生成的代码 ">
private void initComponents()
{
jPanel1 = new javax.swing.JPanel();
btnLeft = new javax.swing.JButton();
btnRight = new javax.swing.JButton();
btnUp = new javax.swing.JButton();
btnDown = new javax.swing.JButton();
btnExit = new javax.swing.JButton();
FormListener formListener = new FormListener();
setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
btnLeft.setText("\u5de6");
btnLeft.addActionListener(formListener);
jPanel1.add(btnLeft);
btnRight.setText("\u53f3");
btnRight.addActionListener(formListener);
jPanel1.add(btnRight);
btnUp.setText("\u4e0a");
btnUp.addActionListener(formListener);
jPanel1.add(btnUp);
btnDown.setText("\u4e0b");
btnDown.addActionListener(formListener);
jPanel1.add(btnDown);
btnExit.setText("\u9000\u51fa");
btnExit.addActionListener(formListener);
jPanel1.add(btnExit);
getContentPane().add(jPanel1, java.awt.BorderLayout.NORTH);
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-469)/2, (screenSize.height-346)/2, 469, 346);
}
// 将事件从组件分发到事件处理程序的代码。
private class FormListener implements java.awt.event.ActionListener
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
if (evt.getSource() == btnExit)
{
DrawFrame.this.btnExitActionPerformed(evt);
}
else if (evt.getSource() == btnLeft)
{
DrawFrame.this.btnLeftActionPerformed(evt);
}
else if (evt.getSource() == btnRight)
{
DrawFrame.this.btnRightActionPerformed(evt);
}
else if (evt.getSource() == btnUp)
{
DrawFrame.this.btnUpActionPerformed(evt);
}
else if (evt.getSource() == btnDown)
{
DrawFrame.this.btnDownActionPerformed(evt);
}
}
}// </editor-fold>
private void btnDownActionPerformed(java.awt.event.ActionEvent evt)
{
circle.move(0,step);
drawPanel.repaint();
}
private void btnUpActionPerformed(java.awt.event.ActionEvent evt)
{
circle.move(0,-1*step);
drawPanel.repaint();
}
private void btnRightActionPerformed(java.awt.event.ActionEvent evt)
{
circle.move(step,0);
drawPanel.repaint();
}
private void btnLeftActionPerformed(java.awt.event.ActionEvent evt)
{
circle.move(-1*step,0);
drawPanel.repaint();
}
private void btnExitActionPerformed(java.awt.event.ActionEvent evt)
{
System.exit(0);
}
/**
* @param args the command line arguments
*/
public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
new DrawFrame().setVisible(true);
}
});
}
private MyCircle circle; //圆心坐标和半径
private int step; //每次移动的距离
private DrawPanel drawPanel;
// 变量声明 - 不进行修改
private javax.swing.JButton btnDown;
private javax.swing.JButton btnExit;
private javax.swing.JButton btnLeft;
private javax.swing.JButton btnRight;
private javax.swing.JButton btnUp;
private javax.swing.JPanel jPanel1;
// 变量声明结束
}
/*
* MyCircle.java
*/
import java.awt.*;
public class MyCircle
{
private int px,py,r; //圆心坐标和半径
public MyCircle(int px, int py, int r)
{
this.px = px;
this.py = py;
this.r = r;
}
public void move(int xStep, int yStep)
{
px = px + xStep;
py = py + yStep;
}
public void draw(Graphics g)
{
g.drawOval(px-r, py-r, 2*r, 2*r);
}
}
/*
* DrawPanel.java
*
* Created on 2006年11月1日, 上午9:26
*/
import java.awt.*;
/**
*
* @author Administrator
*/
public class DrawPanel extends javax.swing.JPanel
{
/** Creates new form DrawPanel */
public DrawPanel(MyCircle c)
{
circle = c;
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" 生成的代码 ">
private void initComponents()
{
setLayout(null);
}// </editor-fold>
public void paintComponent(Graphics g)
{
super.paintComponent(g);
circle.draw(g);
}
private MyCircle circle;
// 变量声明 - 不进行修改
// 变量声明结束
}
* DrawFrame.java
*
* Created on 2006年11月1日, 上午9:04
*/
import javax.swing.*;
import java.awt.*;
/**
*
* @author Administrator
*/
public class DrawFrame extends javax.swing.JFrame
{
/** Creates new form DrawFrame */
public DrawFrame()
{
initComponents();
int px = 100;
int py = 100;
int r = 50;
step = 10;
circle = new MyCircle(px, py, r);
drawPanel = new DrawPanel(circle);
this.getContentPane().add(drawPanel, BorderLayout.CENTER);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" 生成的代码 ">
private void initComponents()
{
jPanel1 = new javax.swing.JPanel();
btnLeft = new javax.swing.JButton();
btnRight = new javax.swing.JButton();
btnUp = new javax.swing.JButton();
btnDown = new javax.swing.JButton();
btnExit = new javax.swing.JButton();
FormListener formListener = new FormListener();
setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
btnLeft.setText("\u5de6");
btnLeft.addActionListener(formListener);
jPanel1.add(btnLeft);
btnRight.setText("\u53f3");
btnRight.addActionListener(formListener);
jPanel1.add(btnRight);
btnUp.setText("\u4e0a");
btnUp.addActionListener(formListener);
jPanel1.add(btnUp);
btnDown.setText("\u4e0b");
btnDown.addActionListener(formListener);
jPanel1.add(btnDown);
btnExit.setText("\u9000\u51fa");
btnExit.addActionListener(formListener);
jPanel1.add(btnExit);
getContentPane().add(jPanel1, java.awt.BorderLayout.NORTH);
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-469)/2, (screenSize.height-346)/2, 469, 346);
}
// 将事件从组件分发到事件处理程序的代码。
private class FormListener implements java.awt.event.ActionListener
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
if (evt.getSource() == btnExit)
{
DrawFrame.this.btnExitActionPerformed(evt);
}
else if (evt.getSource() == btnLeft)
{
DrawFrame.this.btnLeftActionPerformed(evt);
}
else if (evt.getSource() == btnRight)
{
DrawFrame.this.btnRightActionPerformed(evt);
}
else if (evt.getSource() == btnUp)
{
DrawFrame.this.btnUpActionPerformed(evt);
}
else if (evt.getSource() == btnDown)
{
DrawFrame.this.btnDownActionPerformed(evt);
}
}
}// </editor-fold>
private void btnDownActionPerformed(java.awt.event.ActionEvent evt)
{
circle.move(0,step);
drawPanel.repaint();
}
private void btnUpActionPerformed(java.awt.event.ActionEvent evt)
{
circle.move(0,-1*step);
drawPanel.repaint();
}
private void btnRightActionPerformed(java.awt.event.ActionEvent evt)
{
circle.move(step,0);
drawPanel.repaint();
}
private void btnLeftActionPerformed(java.awt.event.ActionEvent evt)
{
circle.move(-1*step,0);
drawPanel.repaint();
}
private void btnExitActionPerformed(java.awt.event.ActionEvent evt)
{
System.exit(0);
}
/**
* @param args the command line arguments
*/
public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
new DrawFrame().setVisible(true);
}
});
}
private MyCircle circle; //圆心坐标和半径
private int step; //每次移动的距离
private DrawPanel drawPanel;
// 变量声明 - 不进行修改
private javax.swing.JButton btnDown;
private javax.swing.JButton btnExit;
private javax.swing.JButton btnLeft;
private javax.swing.JButton btnRight;
private javax.swing.JButton btnUp;
private javax.swing.JPanel jPanel1;
// 变量声明结束
}
/*
* MyCircle.java
*/
import java.awt.*;
public class MyCircle
{
private int px,py,r; //圆心坐标和半径
public MyCircle(int px, int py, int r)
{
this.px = px;
this.py = py;
this.r = r;
}
public void move(int xStep, int yStep)
{
px = px + xStep;
py = py + yStep;
}
public void draw(Graphics g)
{
g.drawOval(px-r, py-r, 2*r, 2*r);
}
}
/*
* DrawPanel.java
*
* Created on 2006年11月1日, 上午9:26
*/
import java.awt.*;
/**
*
* @author Administrator
*/
public class DrawPanel extends javax.swing.JPanel
{
/** Creates new form DrawPanel */
public DrawPanel(MyCircle c)
{
circle = c;
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" 生成的代码 ">
private void initComponents()
{
setLayout(null);
}// </editor-fold>
public void paintComponent(Graphics g)
{
super.paintComponent(g);
circle.draw(g);
}
private MyCircle circle;
// 变量声明 - 不进行修改
// 变量声明结束
}
相关JAVA教程:
spring 加載問題?
关于swing中元件重叠的问题
一个关于drag and drop出错的问题
求Jfreechart的开发文档,决不失言
关于swt的问题``请来帮我啊``
一个关于htmlparser 的问题 头痛!
petstore在执行时ant setup时,发生错误!
关于hibernate HQL查询问题
JLabel有多种外观么? 俺想要个类似于IE下边那种凹下去的Label外观...求达人指点.
NetBeans IDE5.5正式版可以下载了!
petstore配置问题
netbeans??