【发布时间】:2014-04-19 04:23:49
【问题描述】:
美好的一天,我是 java 语言的初学者,我正在尝试控制动画。我的动画有问题,因为程序不显示图像。我创建了一个 JLabels 数组,然后为此设置了 72 个图像。我创建了一个框架,然后添加了 2 个按钮,第一个带有充电电池的图像,然后是一个滑块,不要把注意力放在滑块上,因为我还没有使用它。我想创建2个动画,当我按下名为“cargar”的按钮时,程序必须隐藏实际图像,然后在同一位置显示下一个图像,然后程序必须等待一小段延迟,然后它必须重复相同循环中的动作,因此必须在帧中运行动画,其中动画必须显示电池如何充电。当有人按下名为“descargar”的按钮时,程序必须以相反的方式显示相同的图像,因此动画必须显示电池如何放电。观看视频以更清楚地了解问题。我无法让程序自动显示所有图像,它只显示最后一张图像,但奇怪的是程序可以不循环显示每张图像但它不是自动的,我必须多次按下按钮.我需要你的帮助来解决这个问题,请有人给我一个建议,或者一些减少代码行数的技巧,谢谢你的关注。
视频链接: http://youtu.be/w6pINh-4TGM
代码如下:
//Referring to the package
package programa;
//imported classes
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JSlider;
//begins subclass of JFrame and implements ActionListener and Runnable interfaces
public class Intento3 extends javax.swing.JFrame implements ActionListener, Runnable{
//global variables declaration
//creating an array of JLabels (72 JLabels)
JLabel[] labels = { new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel(),
new javax.swing.JLabel()};
//creating JButtons
JButton jButton1 = new javax.swing.JButton();
JButton jButton2 = new javax.swing.JButton();
//creating JSlider
JSlider miSlider = new JSlider(JSlider.HORIZONTAL, 0, 50, 0);
//creating a boolean variable
boolean carga;
//creating integer variables
int x = 0;
int i;
//start the program
public static void main(String[] arguments){
//creating an object of the created class
Intento3 curso = new Intento3();
//calling the method "cuadro" of the created class
curso.cuadro();
}
//start the method "cuadro"
public void cuadro(){
//title of the frame
setTitle("Animacion bateria");
//size of the frame
setSize(350, 350);
//statement to close the frame
setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
//declaration to prohibit the change of size of the frame
setResizable(false);
//made the frame visible
setVisible(true);
//set images to the array of JLabels
labels[0].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila1.jpg")));
labels[1].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila2.jpg")));
labels[2].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila3.jpg")));
labels[3].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila4.jpg")));
labels[4].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila5.jpg")));
labels[5].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila6.jpg")));
labels[6].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila7.jpg")));
labels[7].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila8.jpg")));
labels[8].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila9.jpg")));
labels[9].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila10.jpg")));
labels[10].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila11.jpg")));
labels[11].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila12.jpg")));
labels[12].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila13.jpg")));
labels[13].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila14.jpg")));
labels[14].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila15.jpg")));
labels[15].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila16.jpg")));
labels[16].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila17.jpg")));
labels[17].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila18.jpg")));
labels[18].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila19.jpg")));
labels[19].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila20.jpg")));
labels[20].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila21.jpg")));
labels[21].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila22.jpg")));
labels[22].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila23.jpg")));
labels[23].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila24.jpg")));
labels[24].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila25.jpg")));
labels[25].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila26.jpg")));
labels[26].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila27.jpg")));
labels[27].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila28.jpg")));
labels[28].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila29.jpg")));
labels[29].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila30.jpg")));
labels[30].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila31.jpg")));
labels[31].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila32.jpg")));
labels[32].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila33.jpg")));
labels[33].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila34.jpg")));
labels[34].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila35.jpg")));
labels[35].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila36.jpg")));
labels[36].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila37.jpg")));
labels[37].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila38.jpg")));
labels[38].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila39.jpg")));
labels[39].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila40.jpg")));
labels[40].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila41.jpg")));
labels[41].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila42.jpg")));
labels[42].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila43.jpg")));
labels[43].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila44.jpg")));
labels[44].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila45.jpg")));
labels[45].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila46.jpg")));
labels[46].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila47.jpg")));
labels[47].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila48.jpg")));
labels[48].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila49.jpg")));
labels[49].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila50.jpg")));
labels[50].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila51.jpg")));
labels[51].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila52.jpg")));
labels[52].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila53.jpg")));
labels[53].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila54.jpg")));
labels[54].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila55.jpg")));
labels[55].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila56.jpg")));
labels[56].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila57.jpg")));
labels[57].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila58.jpg")));
labels[58].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila59.jpg")));
labels[59].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila60.jpg")));
labels[60].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila61.jpg")));
labels[61].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila62.jpg")));
labels[62].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila63.jpg")));
labels[63].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila64.jpg")));
labels[64].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila65.jpg")));
labels[65].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila66.jpg")));
labels[66].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila67.jpg")));
labels[67].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila68.jpg")));
labels[68].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila69.jpg")));
labels[69].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila70.jpg")));
labels[70].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila71.jpg")));
labels[71].setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenesintento3/pila72.jpg")));
//set properties of JSlider
miSlider.setMajorTickSpacing(10);
miSlider.setMinorTickSpacing(1);
//set the titles of the buttons
jButton1.setLabel("Cargar");
jButton2.setLabel("Descargar");
//set a listener to the buttons
jButton1.addActionListener(this);
jButton2.addActionListener(this);
//add image "pila1" to the frame
add(labels[0]);
//set the exact position of the image "pila1"
this.setLayout(null);
Insets insets = this.getInsets();
Dimension size = labels[1].getPreferredSize();
labels[0].setBounds(70,50,196,102);
labels[0].setVisible(true);
//add button1 to the frame
add(jButton1);
//set the exact position of the button1
this.setLayout(null);
Insets insets1 = this.getInsets();
Dimension size1 = jButton1.getPreferredSize();
jButton1.setBounds(40,200,73,30);
jButton1.setVisible(true);
//add button1 to the frame
add(jButton2);
//set the exact position of the button2
this.setLayout(null);
Insets insets2 = this.getInsets();
Dimension size2 = jButton2.getPreferredSize();
jButton2.setBounds(200,200,94,30);
jButton2.setVisible(true);
//set properties of JSlider
miSlider.setPaintTicks(true);
miSlider.setPaintLabels(true);
//add JSlider to the frame
add(miSlider);
//set the exact position of the JSlider
this.setLayout(null);
Insets insets3 = this.getInsets();
Dimension size3 = miSlider.getPreferredSize();
miSlider.setBounds(70,240,200,40);
miSlider.setVisible(true);
}
//start the method "actionPerformed"
@Override
public void actionPerformed(ActionEvent e) {
//get the argument of the buttons
Object fuente = e.getSource();
//if the button1 is pressed...
if(fuente == jButton1){
//start the "b1apretado" method
b1apretado();
//start the execution thread
run();
}
//if the button2 is pressed...
if(fuente == jButton2){
//start the "b2apretado" method
b2apretado();
//start the execution thread
run();
}
}
//start the execution thread
@Override
public void run() {
//if boolean variable is true...
if(carga == true){
while( x > 0){
//add image to the frame
add(labels[x-1]);
//set the exact position of the image
labels[x-1].setBounds(70,50,196,102);
//hide the last image
labels[x].setVisible(false);
//set visible the image
labels[x-1].setVisible(true);
//reduce by one the variable "x"
--this.x;
//set a delay
try{
Thread.sleep(100);
} catch (InterruptedException e){}
}
}
//if boolean variable is false...
if(carga == false){
while( x < 71) {
//add image to the frame
add(labels[x+1]);
//set the exact position of the image
labels[x+1].setBounds(70,50,196,102);
//hide the last image
labels[x].setVisible(false);
//set visible the image
labels[x+1].setVisible(true);
//increase by one the variable "x"
++this.x;
//set a delay
try{
Thread.sleep(100);
} catch (InterruptedException e){}
}
}
}
//start the "b1apretado" method
public void b1apretado(){
//set the boolean variable as true
carga = true;
}
//start the "b2apretado" method
public void b2apretado(){
//set the boolean variable as false
carga = false;
}
}
【问题讨论】:
-
请创建一个 MCVE - MINIMAL,完整且可验证的示例。我建议了解
for循环,以便您可以在 3 行代码而不是 72 行代码中在图标中创建标签。
标签: java