JAVA问一个JLabel的问题
packagechapter16;importjavax.swing.*;importjava.awt.event.*;importjava.awt.*;publiccl...
package chapter16;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class java16_3 extends JFrame {
private JButton jb1=new JButton("enlarge");
private JButton jb2=new JButton("enshort");
private JButton jb3=new JButton("changespeed");
private JLabel jl1=null;//先定义JLabel类,并不对其进行初始化
private CirclePanel canvas=new CirclePanel();
public static void main(String[] args) {//主函数
JFrame frame=new java16_3();
frame.setTitle("java16_3");
frame.setSize(800,600);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public java16_3()//构造函数
{
JPanel panel=new JPanel();
jl1=new JLabel("speed: "+ canvas.getspeed());
panel.add(jb1);
panel.add(jb2);
panel.add(jb3);
panel.add(jl1);
this.add(canvas,BorderLayout.CENTER);
this.add(panel,BorderLayout.SOUTH);
jb1.addActionListener(new jb1Listener());//new(创建)一个监听类,注册监听
jb2.addActionListener(new jb2Listener());
jb3.addActionListener(new jb3Listener());
}
class jb1Listener implements ActionListener//定义监听类,听过该监听类来调用enlarge()方法
{
public void actionPerformed(ActionEvent e)//当事件发生时,调用该方法
{
canvas.enlarge();//只有用类实例才能调用方法
}
}
class jb2Listener implements ActionListener//定义监听类,听过该监听类来调用enlarge()方法
{
public void actionPerformed(ActionEvent e)//当事件发生时,调用该方法
{
canvas.enshort();
}
}
class jb3Listener implements ActionListener//定义监听类,听过该监听类来调用enlarge()方法
{
public void actionPerformed(ActionEvent e)//当事件发生时,调用该方法
{
canvas.changespeed();
}
}
class CirclePanel extends JPanel//画圆的类
{
private int radius=5;
private int speed=1;
public int getspeed()
{
return speed;
}
public void changespeed()
{
speed++;
repaint();
}
public void enlarge()//增大圆的半径
{
radius=radius+speed;
repaint();//重绘
}
public void enshort()//减小圆的半径
{
if(radius>=0)
{
radius=radius-speed;
repaint();
}
}
protected void paintComponent(Graphics g)//画出圆
{
super.paintComponent(g);
g.drawOval(getWidth()/2-radius,getHeight()/2-radius , 2*radius, 2*radius);
}
}
}
怎么让jl1中的speed数值随着点击changespeed按钮而显示改变之后的速度? 展开
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class java16_3 extends JFrame {
private JButton jb1=new JButton("enlarge");
private JButton jb2=new JButton("enshort");
private JButton jb3=new JButton("changespeed");
private JLabel jl1=null;//先定义JLabel类,并不对其进行初始化
private CirclePanel canvas=new CirclePanel();
public static void main(String[] args) {//主函数
JFrame frame=new java16_3();
frame.setTitle("java16_3");
frame.setSize(800,600);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public java16_3()//构造函数
{
JPanel panel=new JPanel();
jl1=new JLabel("speed: "+ canvas.getspeed());
panel.add(jb1);
panel.add(jb2);
panel.add(jb3);
panel.add(jl1);
this.add(canvas,BorderLayout.CENTER);
this.add(panel,BorderLayout.SOUTH);
jb1.addActionListener(new jb1Listener());//new(创建)一个监听类,注册监听
jb2.addActionListener(new jb2Listener());
jb3.addActionListener(new jb3Listener());
}
class jb1Listener implements ActionListener//定义监听类,听过该监听类来调用enlarge()方法
{
public void actionPerformed(ActionEvent e)//当事件发生时,调用该方法
{
canvas.enlarge();//只有用类实例才能调用方法
}
}
class jb2Listener implements ActionListener//定义监听类,听过该监听类来调用enlarge()方法
{
public void actionPerformed(ActionEvent e)//当事件发生时,调用该方法
{
canvas.enshort();
}
}
class jb3Listener implements ActionListener//定义监听类,听过该监听类来调用enlarge()方法
{
public void actionPerformed(ActionEvent e)//当事件发生时,调用该方法
{
canvas.changespeed();
}
}
class CirclePanel extends JPanel//画圆的类
{
private int radius=5;
private int speed=1;
public int getspeed()
{
return speed;
}
public void changespeed()
{
speed++;
repaint();
}
public void enlarge()//增大圆的半径
{
radius=radius+speed;
repaint();//重绘
}
public void enshort()//减小圆的半径
{
if(radius>=0)
{
radius=radius-speed;
repaint();
}
}
protected void paintComponent(Graphics g)//画出圆
{
super.paintComponent(g);
g.drawOval(getWidth()/2-radius,getHeight()/2-radius , 2*radius, 2*radius);
}
}
}
怎么让jl1中的speed数值随着点击changespeed按钮而显示改变之后的速度? 展开
2个回答
展开全部
您好,这样的:
public class text
{
JFrame jf;
JButton jb1;
JLabel jl;
JPanel jp1;
JPanel jp2;
JButton jb2;
public static void main(String[] args)
{
text t=new text();
t.go();
}
void go()
{
final ImageIcon image1 = new ImageIcon("1.gif");
final ImageIcon image2 = new ImageIcon("2.gif");
jf=new JFrame("777");
jl = new JLabel(image1);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(300, 300);
jf.setLocation(400, 300);
jp1 = new JPanel();
jp2 = new JPanel();
jb1=new JButton("111");
jb2 =new JButton("222");
jf.getContentPane().add(jp1,BorderLayout.NORTH);
jf.getContentPane().add(jp2,BorderLayout.CENTER);
jp1.add(jb1);
jp2.add(jb2);
jp2.add(jl);
jb1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jl.setIcon(image1);
jp2.repaint();
}
});
jb2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jl.setIcon(image2);
jp2.repaint();
}
});
jf.setVisible(true);
}
}
public class text
{
JFrame jf;
JButton jb1;
JLabel jl;
JPanel jp1;
JPanel jp2;
JButton jb2;
public static void main(String[] args)
{
text t=new text();
t.go();
}
void go()
{
final ImageIcon image1 = new ImageIcon("1.gif");
final ImageIcon image2 = new ImageIcon("2.gif");
jf=new JFrame("777");
jl = new JLabel(image1);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(300, 300);
jf.setLocation(400, 300);
jp1 = new JPanel();
jp2 = new JPanel();
jb1=new JButton("111");
jb2 =new JButton("222");
jf.getContentPane().add(jp1,BorderLayout.NORTH);
jf.getContentPane().add(jp2,BorderLayout.CENTER);
jp1.add(jb1);
jp2.add(jb2);
jp2.add(jl);
jb1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jl.setIcon(image1);
jp2.repaint();
}
});
jb2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jl.setIcon(image2);
jp2.repaint();
}
});
jf.setVisible(true);
}
}
2015-05-09
展开全部
楼主,不清楚speed是做什么使用的。
更多追问追答
追问
speed是用来改变圆的半径大小的速度,就是按一下圆的半径是很快变大还是慢慢变大
speed是用来改变圆的半径大小的速度,就是按一下圆的半径是很快变大还是慢慢变大
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询