我是Java语言的新手,我开始自己和互联网制作一个程序。现在我有这个:
package com.butoane;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class App {
private JButton apasaAiciButton;
private JPanel Dunno;
private JButton apoiaici;
private JTextArea text1;
private JProgressBar progressBar1;
private JTextArea text2;
public App() {
String a = "H";
String b = "e";
String y = "Ce faci sefule??";
final String[] d = {"x"};
final String[] f = {"D"};
final int[] c = {0};
apasaAiciButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//apoiaici.setEnabled(true);
//apasaAiciButton.setEnabled(false);
text1.append(a);
progressBar1.setValue(c[0] +1);
c[0] = c[0] +1;
}
});
apoiaici.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//apoiaici.setEnabled(false);
//apasaAiciButton.setEnabled(true);
apasaAiciButton.setText("Apasa iar aici");
text1.append(b);
progressBar1.setValue(c[0] +1);
c[0] = c[0] +1;
}
});
}
public static void main(String[] args) {
JFrame frame = new JFrame("App");
frame.setContentPane(new App().Dunno);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
我想要做的是当进度条为100%时向text2添加功能。例如:当progressbar1为100%时,text2将文本设置为“你做到了!” 。我该怎么做?
FYK:我看过有关JProgressBar和JTextArea的oracle文档,我无法理解。
进度条是否有效?如果它工作,那么只需添加。
//the max value of the progress bar == actual value
if (progressBar1.getMaximum() == progressBar1.getValue() {
text2.append("You did it!");
}
您的代码将抛出NullPointerException,因为您没有初始化任何小部件。向您的进度条添加一个监听器(假设您使用的是java 8+)
progressBar1.addChangeListener(e ->
{
javax.swing.JProgressBar bar = (javax.swing.JProgressBar)e.getSource();
if(bar.getValue() == 100)
text2.setText("You dit it");
});