2014-01-10 1 views
0

값을 0으로 설정 한 후 인셋이 재설정되지 않습니다. 예를 들어 왼쪽 버튼으로 한 버튼을 이동 한 다음 다시 똑바로 리셋하면됩니다. 다른 구성 요소는 200 픽셀 이동하지 않습니다. 문제는 0,0,0,0 인 것으로 내 인세 트를 재정의하더라도 두 구성 요소가 모두 이동 중임을 알 수 있습니다. 답변 해 주셔서 감사합니다.인셋의 재설정이 없습니다

import java.awt.BorderLayout; 
import java.io.IOException; 

import javax.swing.JComponent; 
import javax.swing.JFrame; 


public class MainFrame { 
     public static void main(String[] args) throws IOException { 

      MainFrame gui = new MainFrame(); 
      gui.Frame(); 
     } 

    public JFrame Frame() throws IOException { 

     JFrame frame = new JFrame(""); 

     PlayGamePanel passme = new PlayGamePanel(); 
     JComponent panel2 = passme.GamePanel2(frame); 

     frame.add(panel2); 
     frame.getContentPane().add(BorderLayout.CENTER, panel2); 
     frame.setSize(860,500); 
     frame.setLocationRelativeTo(null); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setResizable(false); 
     frame.setVisible(true); 
     frame.getContentPane().add(panel2, BorderLayout.CENTER); 

     return frame; 
    } 

} 

가 여기에 삽입 된 문제가 내 코드입니다 : 여기

내가 문제가있는 JPanel을 시작하는 데 사용하는 코드의 사실

import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Insets; 
import java.awt.image.BufferedImage; 

import javax.swing.BorderFactory; 
import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JComponent; 
import javax.swing.JLabel; 

@SuppressWarnings("serial") 
public class PlayGamePanel extends JComponent{ 

    JComponent GamePanel2() throws IOException { 

     JComponent GamePanel = new JLabel(); 

     //Setting up the GridBagLayout 
     GamePanel.setLayout(new GridBagLayout()); 
     GridBagConstraints gbLayout = new GridBagConstraints(); 

     //Creating the buttons + setting up the GridBagConstraints 
     gbLayout.insets = new Insets(300, 350, 0, 450); 
     JButton AnswerOneButton = new JButton("") 
     GamePanel.add(AnswerOneButton, gbLayout); 

     gbLayout.insets = new Insets(0, 0, 0, 0); 
     JButton AnswerTwoButton = new JButton("1"); 
     GamePanel.add(AnswerTwoButton, gbLayout); 
+3

나머지 코드는 중요하지 않습니다. 더 나은 도움을 받으려면 [MCVE] (http://stackoverflow.com/help/mcve), 짧은 실행 파일, 컴파일 가능한 파일, 끝나지 않은 파일, 시작 또는 둘 다, whats FileIO ImageIO는 GBC의 Insets에 대한 질문과 관련이 있습니다 – mKorbel

+0

@mKorbel 잘못된 가져 오기를 제거하고 모든 것을 실행하는 데 필요한 코드를 추가했습니다. – Icy100

+0

@ alex2410 여기에 두 세계 중 하나 앤드류 톰슨의 SSCCE이며 두 번째는 [커뮤니티 여기 StackOverflow]입니다 (http://meta.stackexchange.com/questions/214955/can-we-create-a-help-center- - 그 - 개요 - 무슨 - - - - - - 계기 - 의미 -에 대한) – mKorbel

답변

3

을, 단지 하나 개의 구성이 한 번에 하나의 Layout에 대한 Insets 이렇게함으로써
: 그냥 결국 gbLayout.insets = new Insets(0, 0, 0, 0) 설정하고 이전 값을 제거됩니다

gbLayout.insets = new Insets(300, 350, 0, 450); 
// Some code... 
gbLayout.insets = new Insets(0, 0, 0, 0); 

.

당신이 묘사 한 것을 얻기 위해 좀 더 복잡한 레이아웃 구성으로 놀아야 만 할 것입니다!

+------------- GridBagLayout -------------+ 
|           | 
|    buttonIcon    | 
|           | 
+--------+--------------------------------+ 
|  |        | 
| glue |   buttonIcon2   | 
|  |        | 
+--------+--------------------------------+ 

당신은 GridBagLayouthere을 사용하는 방법에 대한 자세한 내용을보실 수 있습니다 : 따를 때 당신은 당신이 buttonIcon2의 왼쪽에 약간의 공간을 갖고 싶어

예를 들어, 당신은 GridBagLayout를 사용을 시도 할 수 있습니다.

관련 문제