2013-02-17 2 views
2

아래 그림을 달성하기 위해 최상의 레이아웃 관리자를 생각하려고합니다. 절대 위치 지정이란 익숙한 것이지만, 이것을 사용하여 배경 이미지를 얻을 수는 없습니다. GridBagLayout은 우수하지만 끔찍한 노력을 할 때마다 각 그리드마다 별도의 이미지가 표시됩니다.배경 이미지 및 텍스트 용 레이아웃 관리자

누구나 쉬운 방법을 알고 있습니까? 아니면 쉬운 코드로 다음을 달성 할 수 있습니까?

enter image description here

답변

2

이렇게하는 방법에는 여러 가지가 있습니다.

간단한은

당신이 배경이 필요하지 않은 경우

이 (가 아닌 크기 조정 창 멀리 얻을 수 있습니다 즉) 실행시에 확장 될 ... 이미 enter image description here 가능한 것을 사용할 수 있습니다 단순히 JLabel을 기본 컨테이너로 사용하면 훨씬 쉽게 작업을 수행 할 수 있습니다.
public class LabelBackground { 

    public static void main(String[] args) { 
     new LabelBackground(); 
    } 

    public LabelBackground() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (Exception ex) { 
       } 

       JFrame frame = new JFrame("Test"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(new LoginPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class LoginPane extends JLabel { 

     public LoginPane() { 
      try { 
       setIcon(new ImageIcon(ImageIO.read(getClass().getResource("/background.jpg")))); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 
      setLayout(new GridBagLayout()); 
      GridBagConstraints gbc = new GridBagConstraints(); 
      gbc.anchor = GridBagConstraints.EAST; 
      gbc.insets = new Insets(2, 2, 2, 2); 
      gbc.gridx = 0; 
      gbc.gridy = 0; 

      JLabel nameLabel = new JLabel("Name: "); 
      nameLabel.setForeground(Color.WHITE); 
      JLabel passwordLabel = new JLabel("Password: "); 
      passwordLabel.setForeground(Color.WHITE); 

      add(nameLabel, gbc); 
      gbc.gridy++; 
      add(passwordLabel, gbc); 

      gbc.anchor = GridBagConstraints.WEST; 
      gbc.gridx++; 
      gbc.gridy = 0; 
      add(new JTextField(20), gbc); 
      gbc.gridy++; 
      add(new JTextField(20), gbc); 

      gbc.gridy++; 
      gbc.insets = new Insets(10, 2, 2, 2); 
      gbc.anchor = GridBagConstraints.EAST; 
      add(new JButton("Submit"), gbc); 

     } 

    } 

} 

당신은을 살펴하는 것 같아서

JPanel filler = new JPanel(); 
filler.setOpaque(false); 
gbc.gridx++; 
gbc.weightx = 1; 
add(filler, gbc); 

enter image description here

... 생성자, 추가의 끝에서 왼쪽으로 정렬

의 예와 업데이트 자세한 내용은 How to use GridBagLayout

+0

굉장해! 단 하나의 질문, 어떻게 레이아웃 했어 그래서 내가 왼쪽에 그것을 원한다면 txt 필드가 중간에 있었습니까, 내가 무엇을 바꿀까요? – user1992697

+0

모든 내용을 맨 왼쪽으로 이동하려면 'gridx'위치 2 (텍스트 필드 오른쪽)에 투명한 패널을 추가하고'weightx'에'1'을 입력하십시오. – MadProgrammer

+0

을 추가했습니다. 추가 예제 – MadProgrammer

4

그것을 할 방법에는 여러 가지가 있습니다. 이것들은 제가 지금 생각할 수있는 것입니다 :

  1. JComponent의 하위 클래스를 만듭니다.
  2. paintComponent(Graphics g) 메서드를 재정 의하여 표시 할 이미지를 그립니다.
  3. Set the content paneJFrame이 서브 클래스입니다.

일부 샘플 코드 :이 코드는 처리하지 않는

class ImagePanel extends JComponent { 
    private Image image; 
    public ImagePanel(Image image) { 
     this.image = image; 
    } 
    @Override 
    protected void paintComponent(Graphics g) { 
     g.drawImage(image, 0, 0, null); 
    } 
} 

// elsewhere 
BufferedImage myImage = ImageIO.read(...); 
JFrame myJFrame = new JFrame("Image pane"); 
myJFrame.setContentPane(new ImagePanel(myImage)); 

주 재 크기 즉, 당신이 원하는 무엇 경우 JFrame에 맞게 이미지를.

+0

어디서 코드가 나옵니까? – user1992697

+0

'frame.setVisible (true); 앞에'. – syb0rg

+0

그래서 주요 방법은? – user1992697