2014-09-03 33 views
2

enter image description here JPanel에 추가 된 컴포넌트 정렬에 문제가 있습니다. 채팅 응용 프로그램을 작성 중입니다. 의도 한 목적을 달성하기 위해 어떤 레이아웃을 사용해야하는지. 모든 레이아웃을 시도했지만 원하는 것을 얻지 못했습니다. 창 크기를 조정할 때 주로 문제가 발생합니다. 또한 내가 얻고 자하는 것에 대해 포함 된 이미지에서 아이디어를 얻으십시오. 미리 감사드립니다. 내가 거의 사용하지보다는 다른 이유 BoxLayout를 사용하여 프로토 타입을 채찍질하고 시도하는 것 같은 느낌이 여기가로로 JPanel에 컴포넌트를 하나씩 추가하기

enter image description here

+3

더 나은 도움말은 빨리, 게시하시기 바랍니다 [최소한의 완벽한 예] (http://stackoverflow.com/help/mcve) 당신이 시도한 것을 보여줍니다. – splungebob

+0

레이아웃 권장 사항까지,'BoxLayout'을 사용하는 것은 다소 쉬울 것입니다 (저는 몇 분 안에 모의 연습을했습니다). 'GridBagLayout'을 사용하면 조금 더 효과적 일지 모르지만 조금 더 멋지게 보일 것입니다. – splungebob

+0

내 표가 GridBagLayout으로 이동합니다. 그리드 폭을 GridBagConstraints.REMAINDER로 설정하고 적절한 앵커를 사용하면 원하는 결과가 생성됩니다. – MadProgrammer

답변

4

이미지 설명을 입력합니다. 일반적으로 GridBagLayout이 내 첫 선택입니다.

편집가 : PIC는 (! 감사 trashgod)를 추가

enter image description here

import java.awt.*; 
import java.awt.event.*; 

import javax.swing.*; 

public class MessageAppDemo implements Runnable 
{ 
    private String[] messages = new String[] { 
     "Hello?", 
     "Hey, what's up?", 
     "Where are you?", 
     "Right behind you.", 
     "Stop following me!", 
     "But you owe me money.", 
     "I'll gladly repay you on Tuesday.", 
     "You said that last week!", 
     "But now I actually have a job." 
    }; 
    private int msgCounter = 0; 
    private JPanel panel; 
    private JScrollPane scrollPane; 
    private Timer timer; 

    public static void main(String[] args) 
    { 
    SwingUtilities.invokeLater(new MessageAppDemo()); 
    } 

    public void run() 
    { 
    panel = new JPanel(); 
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 

    scrollPane = new JScrollPane(panel); 
    scrollPane.setVerticalScrollBarPolicy(
     JScrollPane.VERTICAL_SCROLLBAR_NEVER); 
    scrollPane.setAutoscrolls(true); 

    JFrame frame = new JFrame("Message App"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER); 
    frame.setSize(260, 180); 
    frame.setLocationRelativeTo(null); 
    frame.setVisible(true); 

    timer = new Timer(1500, new ActionListener() 
    { 
     public void actionPerformed(ActionEvent event) 
     { 
     if (msgCounter < messages.length) 
     { 
      addMessage(messages[msgCounter]); 
     } 
     else 
     { 
      timer.stop(); 
     } 
     } 
    }); 
    timer.start(); 
    } 

    private void addMessage(String text) 
    { 
    boolean rightAligned = msgCounter % 2 != 0; 
    Color color = rightAligned ? Color.CYAN : Color.ORANGE; 

    JLabel label = new JLabel(text); 
    label.setOpaque(true); 
    label.setBackground(color); 
    label.setBorder(BorderFactory.createCompoundBorder(
     BorderFactory.createLineBorder(Color.BLUE), 
     BorderFactory.createEmptyBorder(2,4,2,4) 
    )); 

    JPanel p = new JPanel(); 
    p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS)); 
    p.setBorder(BorderFactory.createEmptyBorder(2,4,2,4)); 

    if (rightAligned) 
    { 
     p.add(Box.createHorizontalGlue()); 
     p.add(label); 
    } 
    else 
    { 
     p.add(label); 
     p.add(Box.createHorizontalGlue()); 
    } 

    panel.add(p); 
    panel.revalidate(); 
    int x = panel.getPreferredSize().width; 
    int y = panel.getPreferredSize().height; 
    panel.scrollRectToVisible(new Rectangle(x-1 ,y-1, 1, 1)); 

    msgCounter++; 
    } 
} 
관련 문제