2014-09-03 74 views
2

enter image description here我遇到了添加到JPanel的組件對齊問題。我正在開發一個聊天應用程序。應該使用哪種佈局來達到預期的目標。我嘗試了所有的佈局,但我沒有得到想要的。調整窗口大小時會出現大部分問題。此外,從包含的圖像中獲得我想要實現的想法。 在此先感謝。 進入這裏將組件逐個添加到JPanel水平

enter image description here

+3

爲了更好地幫助越早,請張貼[最小完備示例](http://stackoverflow.com/help/mcve)向我們展示了您嘗試過的內容。 – splungebob 2014-09-03 19:54:49

+0

就佈局建議而言,使用'BoxLayout'會相當容易(我在幾分鐘內就嘲笑了一個模擬)。使用'GridBagLayout'將會有更多的工作,但可能看起來會更好一些。 – splungebob 2014-09-03 20:25:59

+0

我的投票轉到GridBagLayout,使用gridwidth設置爲GridBagConstraints.REMAINDER,並使用適當的錨將產生所需的結果 – MadProgrammer 2014-09-03 21:15:39

回答

4

形象描述我颳起了使用BoxLayout原型,沒有其他原因比我很少使用它,感覺就像嘗試它。通常,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++; 
    } 
}