2014-06-20 3 views
0

저는 Java 그래픽에 익숙하지 않아 도움이 필요합니다. 게임 kakuro에 대한 GUI를 만들려고합니다. 여기서 보드 모양이 http://en.wikipedia.org/wiki/Kakuro 인 이미지를 볼 수 있습니다. 나는 사각형을 가지고 그리드를 생성하는 코드를 이미 가지고 있지만 사각형의 중심에 정확히 숫자를 채울 수있는 방법을 모르며 가장 중요한 것은 직사각형의 일부를 대각선으로 나눌 필요가있는 곳 예제와 마찬가지로 두 개의 다른 숫자가 있습니까?kakuro gui 사각형이있는 격자 레이아웃

import java.awt.Graphics; 
    import javax.swing.JComponent; 
    import javax.swing.JFrame; 

    class Grid extends JComponent { 
    public void paint(Graphics g) { 
    int width=30; 
    int height=30;  
    for(int x=0;x<10;x++) 
    { 
     for(int y=0 ;y < 10;y++) 
     { 
      // create rectangles 
      g.drawRect(x*width,y*height,width,height); 
      // Fill in values. However, how to make it in the centre? 
      g.drawString("2", x*width,y*height); 
     } 
    } 
    } 
    } 

    public class Cube { 

    public static void main(String[] a) { 
    JFrame window = new JFrame(); 
    window.setSize(200,200); 
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    window.getContentPane().add(new Grid()); 
    window.setVisible(true); 
    } 
} 

답변

0

당신은 BorderLayout

import java.awt.BorderLayout; 
import java.awt.Color; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.SwingUtilities; 

public class KakuroCorner { 

    private JFrame frame; 
    private JButton myButton1; 
    private JLabel myButton1_Label_E; 
    private JLabel myButton1_Label_S; 

    public KakuroCorner() { 
     myButton1_Label_E = new JLabel("3"); 
     myButton1_Label_E.setHorizontalAlignment(JLabel.CENTER); 
     myButton1_Label_E.setForeground(Color.white); 

     myButton1_Label_S = new JLabel("45"); 
     myButton1_Label_S.setHorizontalAlignment(JLabel.CENTER); 
     myButton1_Label_S.setForeground(Color.white); 

     myButton1 = new JButton(); 
     myButton1.setBackground(Color.black); 
     myButton1.setLayout(new BorderLayout()); 
     myButton1.add(myButton1_Label_E, BorderLayout.EAST); 
     myButton1.add(myButton1_Label_S, BorderLayout.SOUTH); 
     myButton1.setEnabled(false); 

     frame = new JFrame(); 
     frame.add(myButton1); 
     frame.pack(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { new KakuroCorner(); } 
     }); 
    } 
} 
를 사용하여 JButton에 몇 JLabel을 사용할 수 있습니다