2012-05-14 5 views
0

나는 아래와 같은 레이아웃을 수행하는 방법을 알아낼 수 없습니다 [STILL 도움을 필요로]. 내 타블렛을 찾을 수 없으므로 마우스를 사용해야했습니다. 어쨌든, 나는 Tic-Tac-Toe 게임으로 어떻게 제대로 할 수 있는지 알고 싶습니다.GUI 레이아웃 문제

나는 그것이 있어야하는 그리드를 가졌지 만, 나 역시 인생에서 스크린의 제목을 얻을 수는 없다. 나는 또한 단색의 간단한 스트립을 어떻게 보여줄지 알고 싶다. 나는 그것을 시도했지만 항상 나타나지는 않습니다. 때로는 다른 시간에 깜박 인 다음 사라지거나 전혀 나타나지 않는 경우도 있습니다.

아마도 레이아웃과 관련이있을 것이라고 말하고 싶지만 그렇지 않으면 그리드를 수행하는 방법을 알 수 없습니다. 오! 또한 패널 전체를 추가하는 것과 관련하여 잘못된 점을 파악한 경우, 이미 점령 된 버튼을 클릭 할 때 표시해야하는 빨간색 오류 텍스트가 왜 표시되지 않는지 알 수있었습니다. 어느 쪽이든 나타납니다. 나는 그것이 같은 문제라고 확신한다.

여기 내 코드입니다 : 어떤 도움

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

public class Code implements ActionListener { 

JFrame frame = new JFrame ("Tic-Tac-Toe"); 

JLabel title = new JLabel ("Tic-Tac-Toe"); //displayed title of the program 
JLabel error = new JLabel (""); //label that says error if you make a move on a 
//non-blank button 

JPanel titlestrip = new JPanel(); //the strip behind the title 
JPanel bgpanel = new JPanel(); //the background panel that fills up the window 
JPanel bgpanel2 = new JPanel(); //second bg panel with no layout 
JPanel buttonpanel = new JPanel(); //the panel that holds the nine buttons 

JButton one = new JButton (""); 
JButton two = new JButton (""); 
JButton three = new JButton (""); 
JButton four = new JButton (""); 
JButton five = new JButton (""); 
JButton six = new JButton (""); 
JButton seven = new JButton (""); 
JButton eight = new JButton (""); 
JButton nine = new JButton (""); 

GridBagConstraints x = new GridBagConstraints(); 

static String symbol = ""; //stores either an X or an O when the game begins 
static int count = 0; //hidden counter; even for one player & odd for the other 

public Code() { 
    Code(); 
} 

private void Code(){ 
    titlestrip.setLayout(null); 
    titlestrip.setBackground(new Color (0x553EA5)); //color of the strip behind          title 
    titlestrip.setLocation (98,5); 
    titlestrip.setSize (400, 50); 

    title.setFont(new Font("Rockwell Extra Bold", Font.PLAIN, 48)); //font settings 
    title.setForeground(new Color (0x10CDC6)); //title color 

    bgpanel.setBackground(new Color(0x433F3F)); //background color 
    bgpanel.setLayout(FlowLayout()); 

    bgpanel2.setBackground(new Color(0x433F3F)); 

    frame.setVisible (true); 
    frame.setSize (500,500); 
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 

    buttonpanel.setLayout (new GridLayout(3,3)); 
    buttonpanel.add(one); 
    buttonpanel.add(two); 
    buttonpanel.add(three); 
    buttonpanel.add(four); 
    buttonpanel.add(five); 
    buttonpanel.add(six); 
    buttonpanel.add(seven); 
    buttonpanel.add(eight); 
    buttonpanel.add(nine); 
    buttonpanel.setSize (200,200); 
    buttonpanel.setLocation(150, 150); 

    one.addActionListener(this); 
    two.addActionListener(this); 
    three.addActionListener(this); 
    four.addActionListener(this); 
    five.addActionListener(this); 
    six.addActionListener(this); 
    seven.addActionListener(this); 
    eight.addActionListener(this); 
    nine.addActionListener(this); 

    bgpanel.add(buttonpanel); 
    bgpanel2.add(title); 

    x.gridx = 150; 
    x.gridy = 400; 
    bgpanel2.add(error, x); 

    frame.add(bgpanel2); 
    frame.add(bgpanel); 
} 
private LayoutManager FlowLayout() { 
    // TODO Auto-generated method stub 
    return null; 
} 
//- - - - - - - - - - - - - - - - - - [ END ] - - - - - - - - - - - - - - - - - - - // 
//- - - - - - - - - - - - - - - - - -[ LAYOUT ] - - - - - - - - - - - - - - - - - - - // 
public static void main(String[] args) { 
    new SummativeCode(); 
} 
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // 
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // 
public void actionPerformed(ActionEvent e){ 
    count = count + 1; 
    String text = (String)e.getActionCommand(); //stores the kind of text in the button pressed 
    //Checks which player is making a move 
    if (count %2 == 0){ 
     symbol = "X"; 
    } 
    else { 
     symbol = "O"; 
    } 

    //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // 
    //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // 
    //sets the text in the button with an X or an O depending on whose turn it is 
    if (e.getSource() == one){ 
     if (text.equals("")){ //if text is blank, do the following 
      one.setText(symbol); 
     } 
     else { //if it's not blank, display error 
      error.setText("This is an occupied button. Please choose again."); 
      error.setForeground(Color.red); 
     } 
    } 
    if (e.getSource() == two){ 
     if (text.equals("")){ 
      two.setText(symbol); 
     } 
     else { 
      error.setText("This is an occupied button. Please choose again."); 
      error.setForeground(Color.red); 
     } 
    } 
    if (e.getSource() == three){ 
     if (text.equals("")){ 
      three.setText(symbol); 
     } 
     else { 
      error.setText("This is an occupied button. Please choose again."); 
      error.setForeground(Color.red); 
     } 
    } 
    if (e.getSource() == four){ 
     if (text.equals("")){ 
      four.setText(symbol); 
     } 
     else { 
      error.setText("This is an occupied button. Please choose again."); 
      error.setForeground(Color.red); 
     } 
    } 
    if (e.getSource() == five){ 
     if (text.equals("")){ 
      five.setText(symbol); 
     } 
     else { 
      error.setText("This is an occupied button. Please choose again."); 
      error.setForeground(Color.red); 
     } 
    } 
    if (e.getSource() == six){ 
     if (text.equals("")){ 
      six.setText(symbol); 
     } 
     else { 
      error.setText("This is an occupied button. Please choose again."); 
      error.setForeground(Color.red); 
     } 
    } 
    if (e.getSource() == seven){ 
     if (text.equals("")){ 
      seven.setText(symbol); 
     } 
     else { 
      error.setText("This is an occupied button. Please choose again."); 
      error.setForeground(Color.red); 
     } 
    } 
    if (e.getSource() == eight){ 
     if (text.equals("")){ 
      eight.setText(symbol); 
     } 
     else { 
      error.setText("This is an occupied button. Please choose again."); 
      error.setForeground(Color.red); 
     } 
    } 
    if (e.getSource() == nine){ 
     if (text.equals("")){ 
      nine.setText(symbol); 
     } 
     else { 
      error.setText("This is an occupied button. Please choose again."); 
      error.setForeground(Color.red); 
     } 
    } 
} 
} 

덕분에 내 아마도 끔찍한 코딩 논리를 변명.

답변

0

게임에서는 페인트 (그래픽)를 재정의하는 것이 가장 좋은 경우가 많습니다.

방법 내부에 다음과 같은 psuedocode을 사용

setColor(backColor); 
drawRect(0,0,width,height); 
setColor(foreColor); 
drawRect(0,0,width,150); // I'm just guessing that your top is 150 pixels 
drawImage(bufferedTTT,Op,width/2-bufferedTTT.width/2, 15) // to center the heading, you can use drawText, but it would be a lot harder for the same effect 
setColor(black) 
drawLine(...) 
... // draw Lines for TTT board. 
당신이 게시 코드에 문제가 몇 가지 있습니다

Image failed to load.

+0

머리글 이미지를 만들 수 있다면 고맙겠습니다. 패널을 사용하면 어떻게 할 수 있을까요? 그리고 어, 그래서 GridLayout을 사용하지 않습니다 ...? 나는 조금 혼란 스럽다. –

+0

레이아웃을 사용하지 않고 프로그래밍 방식으로 화면에 직접 페인트한다고 말하고 있습니다. [link] (http://docs.oracle.com/javase/6/docs/api/javax/swing/JComponent.html#paintComponent (java.awt.Graphics)) 그리고 이미지를 만든 후에 이미지를 추가하겠습니다. – m12

+0

이미지가 어떻게 보이나요? – m12

0

:

  • 스윙 구성 요소 만에서 접근되어야한다 이벤트 발송 스레드. Initial ThreadsThe Event Dispatch Thead
  • frame에 레이아웃이 설정되어 있지 않으므로 기본값은 BorderLayout입니다. bgpanelbgpanel2add(bgpanel, BorderLayout.CENTER)add(bgpanel2, BorderLayout.NORTH)과 함께 추가해야합니다. add(component)의 기본값은 CENTER이므로 bgpanelbgpanel2을 대체하며 두 번째 패널은 절대로 렌더링되지 않습니다.
  • actionPerformed에서 각 버튼에 대해 동일한 작업 순서가 반복됩니다. 이것은 일반적으로 반복적 인 작업을 수행하는 함수를 소개해야한다는 것을 나타냅니다.

당신이 여기에있는 것은 초보자 코드에 실제로 유용합니다. 좋은 일을 계속하고 자바에 오신 것을 환영합니다!