2013-05-09 3 views
1

학습 프로젝트로 블랙 잭 게임을 제작해야합니다.자바 고정 요소 위치

SWING GUI로 빌드하고 싶습니다. 필요한 부분은 화면을 두 부분으로 나눈 다음 필요한 부분을 기준으로 absolute (x, y) 위치를 사용하여 요소를 삽입 할 수 있도록합니다 (제 경우에는 부호있는 ImageIcon이있는 JButton이 확장됩니다). 그런

뭔가 :

enter image description here

나는 당신이 매우 간단한 방법으로 요소로 작업 할 수 있습니다 안드로이드, 아래 개발에서 온, 나는 스윙에서 길을 잃었 느낌. AbsoluteLayout 같은 것이 없습니까?

여기이 내 여러 시도의 한 예입니다 :

public void run() { 
    // TODO Auto-generated method stub 
    JFrame jFrame = new JFrame("Blackjack"); 
    jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    Container pane = jFrame.getContentPane(); 
    Insets insets = pane.getInsets(); 

    URL url = ClassLoader.getSystemClassLoader().getResource("10_of_clubs.png"); 

    BufferedImage bi = null; 
    try { 
     bi = ImageIO.read(url); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    Image resizedImage = bi.getScaledInstance(128, 186, 0); 

    ImageIcon icon = new ImageIcon(resizedImage); 

    ImageButton imgButton = new ImageButton(icon); 
    imgButton.setPreferredSize(new Dimension(128, 186)); 

    ImageButton imgButton2 = new ImageButton(icon); 
    imgButton.setPreferredSize(new Dimension(128, 186)); 

    pane.setLayout(new GridBagLayout()); 

    JPanel headPanel = new JPanel(); 
    JPanel headPanel2 = new JPanel(); 

    GridBagConstraints cns = new GridBagConstraints(); 
    cns.gridx = 0; 
    cns.gridy = 0; 
    cns.weightx = 0.5; 
    cns.weighty = 0.2; 
    cns.anchor = GridBagConstraints.FIRST_LINE_START; 
    cns.fill = GridBagConstraints.BOTH;   
    headPanel.setBackground(Color.RED); 

    headPanel.add(imgButton, cns); 

    GridBagConstraints cns2 = new GridBagConstraints(); 
    cns2.gridx = 0; 
    cns2.gridy = 0; 
    cns2.weightx = 0.5; 
    cns2.weighty = 0.2; 
    cns2.anchor = GridBagConstraints.FIRST_LINE_START; 
    cns2.fill = GridBagConstraints.CENTER;   
    headPanel2.setBackground(Color.BLUE); 

    headPanel2.add(imgButton2, cns2); 

    pane.add(headPanel); 
    pane.add(headPanel2); 


    jFrame.setSize(800, 600); 
    jFrame.setVisible(true); 
    jFrame.setLocationRelativeTo(null); 
} 

그건 내가 무엇을 얻을 :

enter image description here

TNX.

답변

0

당신은 절대 레이아웃을 원하는 경우, 살펴 보시기 바랍니다 : 당신이 한 번 봐 걸릴 수 있습니다 자바 레이아웃에 대해 읽고 일반적으로 http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html 을 여기 http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html 모든 자바 스윙 구성 요소입니다 : 시각적 가이드 : http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

난 당신이 수직 분리

0

를 만들 수 (http://algo.math.ntua.gr/~symvonis/other-material/java_material/JavaTutorial/uiswing/components/splitpane.html) JSplitPane를 사용할 수 있습니다 생각 당신은 당신이 할 수있는 중복 요소를 가지고 있기 때문에 :

    0123을
  1. JLayeredPane 안에 이미지가있는 기존 JButton을 사용하십시오. 깨끗한 렌더링을 위해 카드를 다른 레이어에 배치하십시오. 'setBounds()'를 사용하여 절대적인 카드 위치 설정

  2. Canvas를 사용하여 절대 위치로 카드를 직접 그립니다. 이 방법을 사용하는 경우 자신이 클릭 처리를 수행해야합니다 (카드에 클릭이 포함되어 있는지 확인하십시오).

관련 문제