我正在尝试制作一个JButton需要多次更改颜色的游戏。这有效,但随后停止改变颜色。
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class Start {
int dimension = 200;
ArrayList < Integer > list = new ArrayList < Integer >();
int buttonCount = 0;
final int pressTime = 1000;
final int hplus = 22;
final int fontSize = 20;
final int fontCustome = Font.PLAIN;
final String font = "Comic Sans MS";
JFrame frame;
JButton blueButton = new JButton();
JButton redButton = new JButton();
JButton yellowButton = new JButton();
JButton greenButton = new JButton();
boolean added = false;
public static void main ( String[] args ) {
Start s = new Start();
s.init();
}
public void init() {
try {
UIManager.setLookAndFeel ( "javax.swing.plaf.nimbus.NimbusLookAndFeel" );
} catch ( Exception e ) {
e.printStackTrace();
}
frame = new JFrame ( "Repeator" );
setUp ( new String[] { "Blue", "Red", "Yellow", "Green" }, new JButton[] { blueButton, redButton, yellowButton, greenButton }, new Color[] { Color.BLUE, Color.RED, Color.YELLOW, Color.GREEN }, new int[][] { { 0, 0 }, { dimension, 0 }, { dimension, dimension }, { 0, dimension } } );
frame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
frame.setMinimumSize ( new Dimension ( 100, 100 + hplus ) );
frame.setLayout ( null );
frame.setAlwaysOnTop ( true );
frame.setSize ( dimension * 2, dimension * 2 + hplus );
frame.setLocationRelativeTo ( null );
frame.setVisible ( true );
frame.addComponentListener ( new ComponentListener() {
public void componentHidden ( ComponentEvent e ) {
resize();
}
public void componentMoved ( ComponentEvent e ) {
resize();
}
public void componentResized ( ComponentEvent e ) {
resize();
}
public void componentShown ( ComponentEvent e ) {
resize();
}
private void resize() {
int s = frame.getWidth();
if ( frame.getWidth() > frame.getHeight() - hplus ) s = frame.getHeight() - hplus;
dimension = s / 2;
setUp ( new String[] { "Blue", "Red", "Yellow", "Green" }, new JButton[] { blueButton, redButton, yellowButton, greenButton }, new Color[] { Color.BLUE, Color.RED, Color.YELLOW, Color.GREEN }, new int[][] { { 0, 0 }, { dimension, 0 }, { dimension, dimension }, { 0, dimension } } );
}
} );
try {
Thread.sleep ( pressTime );
} catch ( Exception e ) {
System.exit ( 405 );
}
cpuGo ( 1, list );
}
public void setUp ( String[] names, JButton[] buttons, Color[] colors, int[][] positions ) {
for ( int i = 0; buttons.length > i; i++ ) {
// get the button
JButton j = buttons [ i ];
// set up button
j.setOpaque ( true );
j.setBorderPainted ( false );
j.setBackground ( colors [ i ] );
j.setFont ( new Font ( font, fontCustome, fontSize ) );
j.setText ( names [ i ] );
j.setBounds ( positions [ i ] [ 0 ], positions [ i ] [ 1 ], dimension, dimension );
// add the listener to the button
if ( ! added ) j.addActionListener ( new ActionListener() {
public void actionPerformed ( ActionEvent e ) {
String text = ( ( JButton ) e.getSource() ).getText();
// I was going to use switch ( j.getText ) { case:... } but I want it to support java 1.6
if ( text.equals ( "Blue" ) ) blueActivate();
else if ( text.equals ( "Red" ) ) redActivate();
else if ( text.equals ( "Yellow" ) ) yellowActivate();
else greenActivate();
}
} );
if ( ! added ) frame.add ( j );
}
added = true;
}
public void cpuGo ( int buttonsDone, ArrayList < Integer > pattern ) {
buttonEnable ( false );
System.out.println ( pattern );
for ( int button: pattern ) {
System.out.println ( "prt -1 = \t" + button );
if ( button == 0 ) activate ( blueButton, 0 );
else if ( button == 1 ) activate ( redButton, 1 );
else if ( button == 2 ) activate ( yellowButton, 2 );
else activate ( greenButton, 3 );
}
System.out.println ( "prt 0" );
Random generator = new Random();
int r = generator.nextInt ( 4 );
if ( r == 0 ) activate ( blueButton, 0 );
else if ( r == 1 ) activate ( redButton, 1 );
else if ( r == 2 ) activate ( yellowButton, 2 );
else activate ( greenButton, 3 );
pattern.add ( r );
System.out.println ( list );
buttonEnable ( true );
}
public void buttonEnable ( boolean enabled ) {
blueButton.setEnabled ( enabled );
redButton.setEnabled ( enabled );
yellowButton.setEnabled ( enabled );
greenButton.setEnabled ( enabled );
}
public void activate ( JButton button, int pressButtonID ) {
setUp ( new String[] { "Blue", "Red", "Yellow", "Green" }, new JButton[] { blueButton, redButton, yellowButton, greenButton }, new Color[] { Color.BLUE, Color.RED, Color.YELLOW, Color.GREEN }, new int[][] { { 0, 0 }, { dimension, 0 }, { dimension, dimension }, { 0, dimension } } );
button.setBackground ( button.getBackground().darker() );
try {
Thread.sleep ( pressTime );
} catch ( InterruptedException e ) {
System.exit ( 405 );
}
button.setBackground ( button.getBackground().brighter() );
}
public void blueActivate() {
System.out.println ( "blue activated" );
pushButton ( 0, buttonCount, list );
}
public void redActivate() {
System.out.println ( "red activated" );
pushButton ( 1, buttonCount, list );
}
public void yellowActivate() {
System.out.println ( "yellow activated" );
pushButton ( 2, buttonCount, list );
}
public void greenActivate() {
System.out.println ( "green activated" );
pushButton ( 3, buttonCount, list );
}
public void pushButton ( int buttonID, int buttonsPushed, ArrayList < Integer > pattern ) {
if ( pattern.get ( buttonsPushed ) == buttonID ) {
buttonsPushed ++;
if ( buttonsPushed == pattern.size() ) cpuGo ( pattern.size() + 1, pattern );
}
else {
frame.setAlwaysOnTop ( false );
JOptionPane.showMessageDialog ( null, "You lose! Round survived: " + ( pattern.size() - 1 ) );
frame.setAlwaysOnTop ( true );
}
}
}
我到处都试图找到它,但我找不到任何有用的东西。 你能找到一种让它多次改变的方法吗?感谢。