2014-04-25 1 views
0

파일> 새 사용자를 클릭 할 때 열리는 프레임이 있지만 텍스트 필드가 모두 함께 스윕됩니다.스윙 레이아웃에서 스쳐지는 텍스트 상자

나는 모두 LayoutManager으로 new GridLayout(3, 2)을 사용하기 위해 세로로 쌓아 놓으려고합니다. 그러나 새로운 창에 대한 내용은 맨 아래에 있습니다. 당신이 GridLayout에 간격하려면

import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 
public class App extends JFrame implements ActionListener 
{ 
    private final int WIDTH = 300; 
    private final int HEIGHT = 550; 
    private int control = 0; 
    String[] username = new String[10]; 
    String[] pass = new String[10]; 
    private String tempName; 
    private String tempPass; 

    Container con = getContentPane(); 
    JPanel panel = new JPanel(); 
    private JTextField name = new JTextField(); 
    private JPasswordField password = new JPasswordField(); 

    JMenuBar mainBar = new JMenuBar(); 
    JMenu menu1 = new JMenu("File"); 
    JMenu menu2 = new JMenu("Edit"); 
    JMenuItem newUser = new JMenuItem("New User"); 

    JButton but1 = new JButton("Log In"); 
    JButton but2 = new JButton("test"); 

    JLabel error = new JLabel("Login info not corret\n Or user not registered."); 
    JLabel success = new JLabel("Success!"); 

    /////////////stuff for dialog/////////////////// 
    JPanel panel2 = new JPanel(); 
    JTextField newModalUser = new JTextField(); 
    JPasswordField newModalPass = new JPasswordField(); 
    JPasswordField newModalPassCon = new JPasswordField(); 
    JButton register = new JButton("Register"); 
    //////////////////////////////////////////////// 
    public static void main(String[] args) 
    { 
     App frame = new App(); 
    } 

    public App() 
    { 
     //just settin some stuff up 
     super("For The Bold"); 
     setSize(WIDTH, HEIGHT); 

     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setLocationRelativeTo(null); 
     //setResizable(false); 
     setVisible(true); 

     //add menubar 
     setJMenuBar(mainBar); 
     mainBar.add(menu1); 
     menu1.add(newUser); 

     //background of main JFrame 
     setContentPane(new JLabel(new ImageIcon("//Users//ryanchampin//Desktop//GUI app//image.png"))); 

     //test names in the arrays 
     username[0] = "ryan"; 
     pass[0] = "test"; 

     //main stuff in the middle 
     //panel.setBackground(Color.RED); 
     panel.setSize(300,300); 
     panel.add(name); 
     panel.add(password); 
     panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS)); 
     panel.add(but1); 

     panel.add(but2); 

     add(panel,new GridBagConstraints()); 
     setLayout(new GridBagLayout()); 

     //assign action listener 
     but1.addActionListener(this); 
     newUser.addActionListener(this); 

     register.addActionListener(this); 
    } 


    public void actionPerformed(ActionEvent e) 
    { 
     Object source = e.getSource(); 
     tempName = name.getText(); 
     tempPass = password.getText(); 

     if(source == but1) 
     { 
      for(int x = 0; x < username.length; x++) 
      { 
       if(tempName.equalsIgnoreCase(username[x]) && tempPass.equals(pass[x])) 
       {    
        //display success JLabel 
        add(success); 
        System.out.println("success"); 
        break; 
       } 
       else 
       { 
        success.setText(null); 
        add(error); 
        name.setText(null); 
        password.setText(null); 
       } 
      } 
     } 
     else 
      if(source == newUser) 
      { 
       panel.setVisible(false); 
       setLayout(new GridLayout(3,2)); 
       add(panel2); 
       panel2.add(newModalUser); 
       panel2.add(newModalPass); 
       panel2.add(newModalPassCon); 
       panel2.add(register); 
      } 
      else if(source == register) 
       System.out.println("yay it worked"); 
    } 
} 

답변

0

, 당신은 그리드의 각 요소 사이에 나타납니다 공간의 픽셀 수를 설정합니다 setHgap(int)setVgap(int) 방법을 사용할 수 있습니다. GridLayout를 구성하고 그 위에 두 개의 setter 메소드를 호출 한 다음 setLayout(LayoutManager) 방법으로 전달할 : : 또는

GridLayout layout = new GridLayout(3, 2); 
layout.setHgap(5); // or whatever number of pixels you want 
layout.setVgap(5); // same 
setLayout(layout); 

, 당신이 캐스팅 할 수

코드에서

, 당신이 할 수있는 두 가지 방법이 있습니다 LayoutManager 당신은 getLayout()를 호출 얻고 그것을 두 가지 방법을 부를 것이다 :

setLayout(new GridLayout(3, 2)); 
((GridLayout) getLayout()).setHgap(5); 
((GridLayout) getLayout()).setVgap(5); 
+0

아니요 스타일에 대한 선호하지 않는 간격 ..... 텍스트 입력란이 너무 부끄럽기 때문에 입력 할 수 없습니다 – Ryan

1
  • 피가 setSize(...)를 사용하여 가능한 경우 setPreferredSize(...)
  • 대신 구성 요소와 레이아웃 관리자가 자체 크기를 설정하도록하십시오.
  • CardLayout을 사용하면 수행중인 작업 대신보기를 바꿀 수 있습니다. 이렇게하면 CardLayout은 주어진 모든 "카드"에 맞게 컨테이너 크기를 조정합니다.
  • setVisible(true)모든 구성 요소를 추가 후 팩을 호출 한 후 호출하는 것을 잊지 마세요 모든 구성 요소를 추가 후 GUI pack()를 호출하는 것을 잊지 마십시오.
  • 새 JTextFields 및 JPasswordFields를 만들 때 생성자에 열 수에 대한 int를 전달합니다.

편집
당신은 질문 :

무슨 팩() 사용을?

pack() 방법은 모든 구성 요소가 적절하게 배치 된 후 GUI는 GUI의 최고의 크기를 설정 한 후 구성 요소를 배치하기 위해 그 구성 컨테이너의 모든 레이아웃 매니저를 가지고 있고, 알려줍니다.

+0

네, 그게 ... 덕분에 ... btw ... whats pack() 사용? – Ryan

+0

@ user3268104 : 오신 것을 환영합니다. 답변은 편집을 참조하십시오. –

0

세로로 쌓아두기를 원한다면 BoxLayout을 계속 사용하기가 쉽지 않을까요?

관련 문제