2012-05-11 71 views
5

我並不是Java的新手(我已經使用它一年了),但這是我第一次使用swing。我試圖讓一個非常簡單的聊天客戶端同時學習socket和swing。我的問題是「我必須做些什麼才能正確對齊我的面板?」。我嘗試了很多東西(雖然我沒有在我的代碼中)。通常我會自己做這樣的事情,但我需要尋求幫助。我需要改變wieghtx,重量級嗎?我想讓客戶看起來像這樣。使用GridBagLayout對齊面板

enter image description here

這是它目前的樣子。

enter image description here

這裏是我的代碼。

package com.client.core; 

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 


public class Window extends JFrame{ 

private int screenWidth = 800; 
private int screenHeight = 600; 

public Window(){ 

    //Initial Setup 
    super("NAMEHERE - Chat Client Alpha v0.0.1"); 
    setResizable(true); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setSize(screenWidth,screenHeight); 
    GridBagConstraints c = new GridBagConstraints(); 

    //Main Panel 
    JPanel window = new JPanel(); 
    window.setLayout(new GridBagLayout()); 
    window.setBackground(Color.black); 

    //Panels 
    JPanel display = new JPanel(); 
    JPanel chat = new JPanel(); 
    chat.setLayout(new GridBagLayout()); 
    JPanel users = new JPanel(); 


    display.setBackground(Color.blue); 
    c.gridx = 0; 
    c.gridy = 0; 
    c.insets= new Insets(5,5,5,5); 
    window.add(display, c); 

    chat.setBackground(Color.red); 
    c.gridx = 0; 
    c.gridy = 3; 
    c.gridheight = 2; 
    c.gridwidth = 1; 
    c.insets= new Insets(5,5,5,5); 
    window.add(chat, c); 

    users.setBackground(Color.green); 
    c.gridx = 2; 
    c.gridy = 0; 
    c.insets= new Insets(5,5,5,5); 
    window.add(users, c); 

    //Buttons 


    //Text fields 
    JTextArea text = new JTextArea("DEREADFADSFEWFASDFSADFASDF"); 
    c.gridx = 0; 
    c.gridy = 0; 
    chat.add(text); 
    JTextField input = new JTextField("type here to chat", 50); 
    c.gridx = 0; 
    c.gridy = 1; 
    c.insets= new Insets(5,5,5,5); 
    chat.add(input); 


    add(window); 


} 

static class ActLis implements ActionListener{ 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     // TODO Auto-generated method stub 

    } 

} 
} 
+1

的GridBagLayout是相當困難與手工編寫,特別是對人的新擺動。通過使用BorderLayout,您可以更簡單地實現該佈局,其左側面板包含三個左側組件。 – Vulcan

+0

同意GridBagLayout過於複雜,特別是如果你是新手揮杆。我建議使用MigLayout或JGoodies FormLayout。允許複雜的佈局,但使如何佈局面板更清晰。 –

+0

相關:http://stackoverflow.com/questions/10333559/gridbaglayout-jscrollpane-how-to-reduce-row-height – assylias

回答

3

你可以做什麼,而且很可能使所期望的結果

JPanel somethingHere = ...; 
JPanel chat = ...; 
JPanel userList = ...; 

JPanel leftPanel = new JPanel(new BorderLayout()); 
leftPanel.add(somethingHere, BorderLayout.CENTER); 
leftPanel.add(chat, BorderLayout.SOUTH); 

JPanel total = new JPanel(new BorderLayout()); 
total.add(leftPanel, BorderLayout.CENTER); 
total.add(userList, BorderLayout.EAST); 

方式簡單,然後用GridBagLayout

+0

好的,你是第二個推薦BorderLayout的人,所以我會試試看。謝謝! – cgasser

+0

@Robin:我曾經用BorderLayout嘗試過一次,有時會發生什麼,因爲JTextArea在開始時會是空的,它佔用的方式太少,有時不會吸引眼球,所以無論你有太多的嵌套去做,達到理想的效果。因此有時GridBagLayout確實適用於這種情況。 +1雖然爲嘗試:-) –

+0

@nIcEcOw通常一個'JTextArea'將被包含在一個滾動窗格中,然後您手動設置行數/列數。如果你把scrollpane放在中間,你可以通過調整框架來調整它的大小 – Robin

3

搞亂如果你想這樣的事情作爲一個輸出:

MESSENGER WINDOW

你可以從這個c頌例子,雖然你可以刪除最後ButtonPanel如果你不需要說:

package to.uk.gagandeepbali.swing.messenger.gui; 

import java.awt.BorderLayout; 
import java.awt.GridLayout; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Color; 

import javax.swing.BorderFactory; 
import javax.swing.JButton; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTextPane; 
import javax.swing.JTextField; 

public class ChatPanel extends JPanel 
{ 
    private JButton backButton; 
    private JButton exitButton; 
    private JButton sendButton; 

    private JTextPane chatPane; 
    private JTextPane namePane; 
    private JTextField chatField; 

    private GridBagConstraints gbc; 

    private final int GAP = 10; 
    private final int SMALLGAP = 1; 

    public ChatPanel() 
    { 
     gbc = new GridBagConstraints(); 
    } 

    protected void createGUI() 
    { 
     setOpaque(true); 
     setBackground(Color.WHITE); 
     setLayout(new BorderLayout(5, 5)); 

     JPanel centerPanel = new JPanel(); 
     centerPanel.setOpaque(true); 
     centerPanel.setBackground(Color.WHITE); 
     centerPanel.setBorder(BorderFactory.createEmptyBorder(GAP, GAP, 0, GAP)); 
     centerPanel.setLayout(new GridBagLayout()); 
     gbc.gridx = 0; 
     gbc.gridy = 0; 
     gbc.gridwidth = 5; 
     gbc.weightx = 0.8; 
     gbc.weighty = 1.0; 
     gbc.fill = GridBagConstraints.BOTH; 
     gbc.anchor = GridBagConstraints.FIRST_LINE_START; 
     chatPane = new JTextPane(); 
     JScrollPane scrollerChat = new JScrollPane(); 
     scrollerChat.setBorder(BorderFactory.createTitledBorder("Chat")); 
     scrollerChat.setViewportView(chatPane); 
     centerPanel.add(scrollerChat, gbc); 

     gbc.gridx = 5; 
     gbc.gridwidth = 2; 
     gbc.weightx = 0.2; 
     namePane = new JTextPane(); 
     JScrollPane scrollerName = new JScrollPane(namePane); 
     scrollerName.setBorder(BorderFactory.createTitledBorder("Names")); 
     centerPanel.add(scrollerName, gbc); 

     gbc.gridx = 0; 
     gbc.gridy = 5; 
     gbc.gridwidth = 5; 
     gbc.weightx = 0.8; 
     gbc.weighty = 0.1; 
     gbc.fill = GridBagConstraints.HORIZONTAL; 
     chatField = new JTextField(); 
     chatField.setOpaque(true); 
     chatField.setBorder(BorderFactory.createCompoundBorder(
       BorderFactory.createTitledBorder("") 
       , BorderFactory.createEmptyBorder(SMALLGAP, SMALLGAP, SMALLGAP, SMALLGAP))); 
     centerPanel.add(chatField, gbc); 

     gbc.gridx = 5; 
     gbc.gridwidth = 2; 
     gbc.weightx = 0.2; 
     sendButton = new JButton("Send"); 
     sendButton.setBorder(BorderFactory.createTitledBorder("")); 
     centerPanel.add(sendButton, gbc);  

     JPanel bottomPanel = new JPanel(); 
     bottomPanel.setOpaque(true); 
     bottomPanel.setBackground(Color.WHITE); 
     bottomPanel.setBorder(
       BorderFactory.createTitledBorder("")); 
     bottomPanel.setLayout(new BorderLayout()); 

     JPanel buttonPanel = new JPanel(); 
     buttonPanel.setOpaque(true); 
     buttonPanel.setBackground(Color.WHITE); 
     buttonPanel.setBorder(BorderFactory.createEmptyBorder(GAP, GAP, 0, GAP)); 
     backButton = new JButton("Back"); 
     exitButton = new JButton("Exit"); 
     buttonPanel.add(backButton); 
     buttonPanel.add(exitButton); 
     bottomPanel.add(buttonPanel, BorderLayout.CENTER); 

     add(centerPanel, BorderLayout.CENTER); 
     add(bottomPanel, BorderLayout.PAGE_END); 
    } 

    public JTextPane getChatPane() 
    { 
     return chatPane; 
    } 

    public JTextPane getNamePane() 
    { 
     return namePane; 
    } 

    public JTextField getChatField() 
    { 
     return chatField; 
    } 

    public JButton getExitButton() 
    { 
     return exitButton; 
    } 

    public JButton getBackButton() 
    { 
     return backButton; 
    } 

    public JButton getSendButton() 
    { 
     return sendButton; 
    } 
} 
+1

感謝您的幫助!儘管如此,我發現我想要的只是與BoxLayout搞混了。 – cgasser

+1

@Zexanima:你最歡迎並保持微笑:-)。很高興你得到它:-) –

1

這是我想出了迄今。紅色框是我計劃添加一個簡單的2D頭像界面與LWJGL的地方。

enter image description here

這裏是它的代碼

package com.client.core; 

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

import javax.swing.*; 
import javax.swing.event.DocumentEvent; 
import javax.swing.event.DocumentListener; 


public class Window extends JFrame{ 

private int screenWidth = 800; 
private int screenHeight = 600; 

public Window(){ 

    //Initial Setup 
    super("NAMEHERE - Chat Client Alpha v0.0.1"); 
    setResizable(true); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setSize(screenWidth,screenHeight); 


    //Main Panels 
    JPanel window = new JPanel(new BorderLayout()); 
    JPanel center = new JPanel(new BorderLayout()); 
    JPanel right = new JPanel(new BorderLayout()); 


    //Panels 
    JPanel display = new JPanel(new BorderLayout()); 
    display.setBackground(Color.red); 
    JPanel chat = new JPanel(); 
    chat.setLayout(new BoxLayout(chat, BoxLayout.Y_AXIS)); 
    chat.setBackground(Color.blue); 
    JPanel users = new JPanel(new BorderLayout()); 
    users.setBackground(Color.green); 


    //TextFields 
    JTextArea chatBox = new JTextArea("Welcome to the chat!", 7,50); 
    chatBox.setEditable(false); 
    JTextField chatWrite = new JTextField(); 
    JScrollPane userList = new JScrollPane(); 
    JTextField userSearch = new JTextField(10); 
    userList.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
    users.add(userList); 
    users.add(userSearch, BorderLayout.NORTH); 
    chat.add(chatBox); 
    chat.add(chatWrite); 
    chat.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1)); 

    //Menu bar 
    JMenuBar menu = new JMenuBar(); 
     JMenu file = new JMenu("File"); 
      JMenuItem exit = new JMenuItem("Exit"); 
      JMenuItem ipconnect = new JMenuItem("Connect to IP"); 
    file.add(ipconnect); 
    file.add(exit); 
    menu.add(file); 


    //Main window adding 
    right.add(users); 
    center.add(display, BorderLayout.CENTER); 
    center.add(chat, BorderLayout.SOUTH); 
    window.add(center, BorderLayout.CENTER); 
    window.add(right, BorderLayout.EAST); 
    window.add(menu, BorderLayout.NORTH); 
    add(window); 




    //Listeners 
    chatWrite.addKeyListener(new KeyLis()); 
    ipconnect.addActionListener(new ActLis()); 
    exit.addActionListener(new ActLis()); 
} 







static class KeyLis implements KeyListener{ 

    @Override 
    public void keyPressed(KeyEvent e) { 
     if(e.getKeyCode() == KeyEvent.VK_ENTER){ 
      System.out.println("Message recieved."); 
     } 

    } 

    @Override 
    public void keyReleased(KeyEvent e) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void keyTyped(KeyEvent e) { 
     // TODO Auto-generated method stub 

    } 



} 

static class ActLis implements ActionListener{ 

    @Override 
    public void actionPerformed(ActionEvent e) { 
    if(e.getActionCommand() == "Exit"){ 
     System.exit(0); 
    } else if(e.getActionCommand() == "Connect to IP"){ 
     System.out.println("Connecting...."); 
     JFrame frameip = new JFrame(); 
     JPanel panelip = new JPanel(); 
     JButton buttonip = new JButton("Hello"); 
     frameip.add(panelip); 
     panelip.add(buttonip); 
     JDialog ippop = new JDialog(frameip, "Enter IP", false); 
    } 


    } 
} 
} 
+0

啊哈,這很好:-),讓我試試這個,如果我能用GridBag做到這一點,會讓你知道,在這:-) –

+0

這看起來不錯我想,我不需要GridBagLayout :-)太好了 –