如何解决JProgressBar 在 actionevent 中不显示进度条
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
public class Project extends JFrame implements ActionListener,ItemListener
{
JFrame frm = new JFrame("Welcome aboard!");
JFrame frame = new JFrame("Calculating...");
JFrame JP = new JFrame("JACK 'N POY");
JLabel wait = new JLabel(" Please wait for minimal seconds");
JLabel label = new JLabel(new ImageIcon("rps1.gif"));
JProgressBar prog = new JProgressBar(1,100);
JPanel pane = new JPanel();
JLabel lbl = new JLabel(" Welcome to Rock,Paper and Scissor!");
JLabel icon = new JLabel(new ImageIcon("Open.gif"));
JLabel title = new JLabel("JACK EN POY");
JLabel pname = new JLabel();
JLabel cname = new JLabel("Computer");
JLabel pscore = new JLabel("Player pts: ");
JLabel cscore = new JLabel("Computer pts: ");
Font fhead = new Font("Courier",Font.BOLD,30);
Font fPSubHead = new Font("Courier",Font.PLAIN,15);
Font fscores = new Font("Courier",11);
JButton btn = new JButton("GO");
JButton btn1 = new JButton("Clear");
JButton proceed = new JButton("Proceed");
JRadioButton [] pc = new JRadioButton[3];
JRadioButton [] cc = new JRadioButton[3];
ButtonGroup pgrp = new ButtonGroup();
ButtonGroup cgrp = new ButtonGroup();
String[] choices = {"Rock","Paper","Scissor"};
Integer[] loc = {80,110,140};
String pmove,cmove,name;
int p = 0,c = 0;
Random rdm = new Random();
public Project()
{
Welcome();
setJLabel();
setJRadio();
setJButton();
setJPane();
setJFrame();
}
public void Welcome()
{
name = JOptionPane.showInputDialog(null,"Enter your name: ","Demographics",JOptionPane.QUESTION_MESSAGE);
name = "" + name.toupperCase().charat(0) + name.substring(1).toLowerCase();
pname.setText(name);
frm.getContentPane().setBackground(Color.DARK_GRAY);
frm.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
frm.setSize(300,200);
frm.setLocation(550,300);
frm.add(icon,BorderLayout.norTH);
frm.add(lbl,BorderLayout.CENTER);
frm.add(proceed,BorderLayout.soUTH);
frm.setResizable(false);
lbl.setForeground(Color.white);
proceed.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0) {
frm.dispose();
JP.setVisible(true);
return;
}});
frm.setVisible(true);
}
public void Jprog()
{
prog.setValue(0);
prog.setBounds(0,100,50);
prog.setStringPainted(true);
prog.setBackground(Color.DARK_GRAY);
prog.setForeground(Color.red);
wait.setForeground(Color.WHITE);
frame.setDefaultCloSEOperation(JFrame.disPOSE_ON_CLOSE);
frame.setLocation(550,300);
frame.setSize(300,190);
frame.getContentPane().setBackground(Color.DARK_GRAY);
frame.add(label,BorderLayout.norTH);
frame.add(wait,BorderLayout.CENTER);
frame.add(prog,BorderLayout.soUTH);
frame.setVisible(true);
fill();
frame.dispose();
}
public void fill()
{
int i=0;
while(i<=100)
{
prog.setValue(i);
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printstacktrace();
}
i+=1;
}
prog.setString("Already Done!");
}
public void setJLabel()
{
title.setSize(200,50);
title.setLocation(48,0);
title.setFont(fhead);
title.setForeground(Color.YELLOW);
pname.setSize(100,30);
pname.setLocation(30,50);
pname.setFont(fPSubHead);
pname.setForeground(Color.CYAN);
cname.setSize(100,30);
cname.setLocation(185,50);
cname.setFont(fPSubHead);
cname.setForeground(Color.magenta);
pscore.setSize(150,30);
pscore.setLocation(15,180);
pscore.setFont(fscores);
pscore.setText(pscore.getText() + p);
pscore.setForeground(Color.white);
cscore.setSize(150,30);
cscore.setLocation(170,180);
cscore.setFont(fscores);
cscore.setText(cscore.getText() + c);
cscore.setForeground(Color.white);
}
public void setJFrame()
{
JP.setSize(300,300);
JP.setLocation(525,250);
JP.setDefaultCloSEOperation(EXIT_ON_CLOSE);
JP.setResizable(false);
JP.getContentPane().setBackground(Color.DARK_GRAY);
JP.add(pane);
}
public void setJRadio()
{
for(int i=0; i<choices.length; i++){
pc[i] = new JRadioButton(choices[i],false);
pc[i].setSize(80,30);
pc[i].setLocation(20,loc[i]);
pc[i].addItemListener(this);
pc[i].setForeground(Color.white);
pc[i].setBackground(Color.DARK_GRAY);
cc[i] = new JRadioButton(choices[i],false);
cc[i].setSize(80,30);
cc[i].setLocation(185,loc[i]);
cc[i].setEnabled(false);
cc[i].setForeground(Color.white);
cc[i].setBackground(Color.DARK_GRAY);
pgrp.add(pc[i]);
cgrp.add(cc[i]);
}
}
public void setJPane()
{
pane.setLayout(null);
pane.add(title);
pane.add(pname);
pane.add(cname);
pane.add(pscore);
pane.add(cscore);
pane.setBackground(Color.DARK_GRAY);
pane.add(btn);
pane.add(btn1);
for(int i=0; i<choices.length; i++)
{
pane.add(pc[i]);
pane.add(cc[i]);
}
}
public void setJButton()
{
btn.setSize(100,20);
btn.setLocation(40,230);
btn.setEnabled(false);
btn1.setSize(100,20);
btn1.setLocation(150,230);
btn.addActionListener(this);
btn1.addActionListener(this);
}
public static void main(String[] args)
{
new Project();
}
public void clear()
{
pgrp.clearSelection();
cgrp.clearSelection();
btn.setEnabled(false);
}
public void Output()
{
if(pmove.equals("Rock") && cmove.equals("Paper"))
{
cscore.setText("Computer pts: " + (c+=1));
JOptionPane.showMessageDialog(null,"Points goes to Computer!");
}
else if(pmove.equals("Rock") && cmove.equals("Scissor"))
{
pscore.setText("Player pts: " + (p+=1));
JOptionPane.showMessageDialog(null,"Points goes to " + name + "!");
}
else if(pmove.equals("Paper") && cmove.equals("Rock"))
{
pscore.setText("Player pts: " + (p+=1));
JOptionPane.showMessageDialog(null,"Points goes to " + name + "!");
}
else if(pmove.equals("Paper") && cmove.equals("Scissor"))
{
cscore.setText("Computer pts: " + (c+=1));
JOptionPane.showMessageDialog(null,"Points goes to Computer!");
}
else if(pmove.equals("Scissor") && cmove.equals("Paper"))
{
pscore.setText("Player pts: " + (p+=1));
JOptionPane.showMessageDialog(null,"Points goes to " + name + "!");
}
else if(pmove.equals("Scissor") && cmove.equals("Rock"))
{
cscore.setText("Computer pts: " + (c+=1));
JOptionPane.showMessageDialog(null,"Points goes to Computer!");
}
else
{
JOptionPane.showMessageDialog(null,"DRAW!");
}
if(c == 5)
{
btn1.setEnabled(true);
for(int j=0; j<choices.length; j++)
pc[j].setEnabled(false);
JOptionPane.showMessageDialog(null,"Your such a loser," + name + "!!!");
}
else if(p == 5)
{
btn1.setEnabled(true);
for(int j=0; j<choices.length; j++)
pc[j].setEnabled(false);
JOptionPane.showMessageDialog(null,"Congratulations " + name + "!");
}
clear();
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == btn)
{
int i = rdm.nextInt(3);
cc[i].setSelected(true);
cmove = cc[i].getActionCommand();
Jprog();
Output();
}
else
{
clear();
p = c = 0;
for(int i=0;i<choices.length; i++)
pc[i].setEnabled(true);
cscore.setText("Computer pts: " + c);
pscore.setText("Player pts: " + p);
}
}
public void itemStateChanged(ItemEvent ie)
{
for(int i=0; i<choices.length; i++)
{
if(pc[i].isSelected())
{
pmove = pc[i].getActionCommand();
btn.setEnabled(true);
}
}
}
}
我很困惑为什么它没有显示。它只显示一个空白帧,但它的工作方式就像一个进度条,在给定的时间执行后,它会处理。它只显示在动作事件之外时,但这样做时,我无法实现我想要的。 JProgressBar
函数是 Jprog()
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。