2014-04-08 2 views
0
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.event.*; 
import java.util.*; 
public class Number extends JApplet implements ListSelectionListener 
{ 
    public static JList list; 
    public void init() 
    { 
     ArrayList<String> arr = new ArrayList<String>(); //making an arraylist with all the colors 
     arr.add("Red"); 
     arr.add("Green"); 
     arr.add("Blue"); 
     arr.add("Yellow"); 
     arr.add("Orange"); 
     arr.add("Purple"); 
     arr.add("Random"); 
     arr.add("White"); 

     list = new JList(arr.toArray()); //making the array list 

     add(list); //I think this may be the source of m 
     this.setVisible(true); 
     list.addListSelectionListener(this); 
     String x = JOptionPane.showInputDialog("Enter a number from 0-9: "); 
     int cool = Integer.parseInt(x); 
    } 
    public void valueChanged(ListSelectionEvent e) //trying to change the background of the applet using a JList, and this is where im running into the biggest problem 
    { 
     Container c = getContentPane(); 
     int y = list.getSelectedIndex(); 
     switch(y) 
     { 
      case 0: 
      c.setBackground(Color.red); 
      case 1: 
      c.setBackground(Color.green); 
      case 2: 
      c.setBackground(Color.blue); 
      case 3: 
      c.setBackground(Color.yellow); 
      case 4: 
      c.setBackground(Color.orange); 
      case 5: 
      c.setBackground(Color.magenta); 
      case 6: 
      c.setBackground(new Color((int)(Math.random()*255), (int)(Math.random()*255), (int)(Math.random()*255))); 
      case 7: 
      c.setBackground(Color.white); 
     } 
    } 
    public void paint(Graphics g) 
    { 
     super.paint(g); 
     String x = JOptionPane.showInputDialog("Enter a number from 0-9: "); 
     int cool = Integer.parseInt(x); 
     switch(cool) 
     { 
      //Switch structure that draws numbers 
     } 
     } 
    } 

JList가 JApplet에 표시되지 않는 문제가 발생했습니다. JList가 존재하지 않지만 완전히 확신 할 수없는 프레임에 추가 될 수 있다고 생각합니다. 그리고 그렇습니다. 나는 수입이 엄청나다고 생각합니다. 이 작은 문제에 대해 도움이 될지 궁금합니다.JApplet에서 JList 사용 (Small Issue)

답변

0
switch(y) 
{ 
    case 0: 
    c.setBackground(Color.red); 
    case 1: 

은 다음과 같아야합니다

switch(y) 
{ 
    case 0: 
    c.setBackground(Color.red); 
    break; //VERY important if we don't want it to 'fall through' to next statements 
    case 1: