2013-05-23 1 views
0

현재 Java에서 행운 복제본을 만들고있는 학교 프로젝트에서 작업 중입니다. 패널에 buttonPanel이라는 클래스 내에 JButton이라는 패널이 있고 프로그램이 실행되는 클래스 역할을하는 별도의 클래스 wheelGUI이 있습니다. GUI에서 JButtonspin을 눌렀을 때 JTextField results의 매개 변수 역할을하는 spinWheel 메서드를 사용하여 String[] wheelStuff의 임의 값을 spinValue 문자열에 할당 한 다음 해당 임의 값을 GUI의 청록색 상자. 기술적이지 않은 용어로 버튼 스핀을 누르면 현재 플레이어의 스핀 값 역할을하는 무작위 값을 청록색 상자에 표시합니다. 여기서, wheelGUIJava 별도의 클래스에있는 JButton을 누른 후 JTextField에 문자열 표시

도와
package wheelOfFortune; 

import java.awt.*; 
import java.awt.event.*; 
import java.io.IOException; 
import java.util.Random; 

import javax.swing.*; 

public class wheelGUI extends JFrame implements ActionListener { 
private playerPlate player1, player2, player3; 
Color yungMoney = new Color(0, 180, 100); 
private String fileName = "M:/wheelOfFortune/src/wheelOfFortune/img/wheel1.png"; 
private String cat; 
private static String spinValue = ""; 
private static String[] wheelStuff = new String[]{"Bankrupt", "Lose a Turn", "$5000", "$600", "$500", "$300", "$800", "$550", "$400", "$900", "$350", "$450", "$700"}; 
private static JTextField results; 

public wheelGUI() { 
    super("Butt Stuff!"); 

    ImageIcon i = new ImageIcon(fileName); 
    JLabel picture = new JLabel(i); 

    player1 = new playerPlate("Garrett", Color.RED); 
    player2 = new playerPlate("Jonny", Color.YELLOW); 
    player3 = new playerPlate("Robert", Color.BLUE); 

    buttonPanels buttons = new buttonPanels(); 
    letterBoard letters = new letterBoard(); 
    catBox category = new catBox(cat); 
    inputField input = new inputField(); 

    Box wall = Box.createHorizontalBox(); 
    wall.add(player1); 
    wall.add(Box.createHorizontalStrut(5)); 
    wall.add(player2); 
    wall.add(Box.createHorizontalStrut(5)); 
    wall.add(player3); 

    JPanel result = new JPanel(); 
    result.setBackground(yungMoney); 
    JTextField results = new JTextField(spinValue); 
    results.setBackground(Color.CYAN); 
    results.setHorizontalAlignment(JTextField.CENTER); 
    results.setBorder(BorderFactory.createLineBorder(Color.BLACK,2)); 
    results.setPreferredSize(new Dimension(150,100)); 
    results.setFont(new Font("Impact", Font.PLAIN, 28)); 
    results.setEditable(false); 
    result.add(results); 


    Box catInput = Box.createVerticalBox(); 
    catInput.add(category); 
    catInput.add(Box.createVerticalStrut(50)); 
    catInput.add(result); 
    catInput.add(Box.createVerticalStrut(100)); 
    catInput.add(input); 

    Container c = getContentPane(); 
    c.setBackground(yungMoney); 
    c.add(buttons, BorderLayout.EAST); 
    c.add(wall, BorderLayout.SOUTH); 
    c.add(letters, BorderLayout.NORTH); 
    c.add(picture, BorderLayout.WEST); 
    c.add(catInput, BorderLayout.CENTER); 
} 
public static String spinWheel(String[] wheelStuff) 
{ 
    Random rnd = new Random(); 
    wheelStuff[rnd.nextInt(wheelStuff.length)] = spinValue; 
    return spinValue; 
} 
public static void main(String[] args) { 
    wheelGUI window = new wheelGUI(); 
    window.setBounds(50, 50, 1024, 768); 
    window.setDefaultCloseOperation(EXIT_ON_CLOSE); 
    window.setResizable(false); 
    window.setVisible(true); 
} 

public void actionPerformed(ActionEvent e) 
{ 
    // logic for any additional panels. other logics should be in individual 
    // classes. 
} 

} 

감사 클래스 buttonPanel

package wheelOfFortune; 

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class buttonPanels extends JPanel 
         implements ActionListener 
{ 
private JButton spin, solve, buyVowel, guess, reset, end, cont; 
Color yungMoney = new Color(0, 180, 100); 
private static String[] wheelStuff = new String[]{"Bankrupt", "Lose a Turn", "$5000", "$600", "$500", "$300", "$800", "$550", "$400", "$900", "$350", "$450", "$700"}; 

public buttonPanels() 
{ 
    setBackground(yungMoney); 
    spin = new JButton("Spin!"); 
    spin.addActionListener(this); 
    solve = new JButton("Solve the Puzzle"); 
    solve.addActionListener(this); 
    buyVowel = new JButton("Buy a Vowel"); 
    buyVowel.addActionListener(this); 
    guess = new JButton("Guess a Letter"); 
    guess.addActionListener(this); 
    reset = new JButton("Reset"); 
    reset.addActionListener(this); 
    cont = new JButton("Continue"); 
    cont.addActionListener(this); 

    JPanel buttonPanel = new JPanel(new GridLayout(3, 1, 5, 5)); 
    buttonPanel.setPreferredSize(new Dimension(300,380)); 
    buttonPanel.setBackground(yungMoney); 
    buttonPanel.add(spin); 
    buttonPanel.add(guess); 
    buttonPanel.add(buyVowel); 
    buttonPanel.add(solve); 
    buttonPanel.add(cont); 
    buttonPanel.add(reset); 

    add(buttonPanel); 
} 
public void actionPerformed(ActionEvent e) 
{ 
    JButton b = (JButton)e.getSource(); 
    b.addActionListener(this); 
    if(b==spin) 
    { 
     wheelGUI.spinWheel(wheelStuff); 
    } 
    repaint(); 
} 

} 

을위한 코드입니다 그리고 여기 메인 클래스의 코드입니다! 참고 : 앞서 언급 한 것과 관련이없는 wheelGUI의 모든 코드는 무시할 수 있습니다. 이는 다른 여러 클래스의 코드입니다.

답변

0

이런 식으로 static 필드를 사용하는 것은 좋지 않습니다.

대신 buttonsPanelwheelGUI을 참조해야합니다. 이렇게하면 buttonsPanel이 필요한 경우 wheelGUI에서 필요한 메소드를 호출 할 수 있습니다.

더 나은 솔루션 모델 변화

기본 문제는 당신이 results 텍스트 필드를 섀도 잉의 일부로 이벤트를 발사, 두 UI를 사이에 앉아서 논리를 모델링 할 수있는 모델을 사용하는 것입니다. 즉,이 선언 한 다음 클래스 수준, 정적 필드와 이것은 의미한다

JTextField results = new JTextField(spinValue); 

results = new JTextField(spinValue); 

에 당신의 wheelGUI 생성자에서 생성자 내에서

변경 다음 줄을 그것을 다시 선언이다 결과 필드에 텍스트를 설정하면 필드의 올바른 인스턴스 값을 설정하게됩니다.

관련 문제