[英]Java Swing Panel layout
我正在為大學開發一個Java項目,涉及我們設置TCP服務器和客戶端。 我有這個部分工作,現在為我的項目添加更多的感覺我想添加一個GUI。
我們還沒有開始學習Java中的GUI。 但是我想嘗試,因為我認為這將是一個有用的練習。 我有一個非常基本的GUI設置和為該按鈕設置的相應ActionListener。 我的下一個問題是定位我的面板,使它們在框架上看起來整潔整潔......
目前,我將所有組件放在一個面板中,如下所示:
public ClientGUI(){
//Initialise Frame
frame = new JFrame("TCP Client");
//Initialise Panel 1 & Components
p1 = new JPanel();
//Set Layout
p1.setLayout(new GridLayout(1,2));
//Label 1 - For TextArea
l1 = new JLabel("Chat Log");
p1.add(l1);
//TextArea - To display conversation
t1 = new JTextArea(10,10);
p1.add(t1);
//Label 2 - For TextField
l2 = new JLabel("Message");
p1.add(l2);
//Message Box - For user input
t2 = new JTextField(10);
p1.add(t2);
//Button 1 - To send message
b1 = new JButton("Send");
p1.add(b1);
//Add panels to frame
frame.add(p1);
//Frame properties...
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.setVisible(true);
//Add Event listener to button
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ev){
//do something
t1.setText(t2.getText());
}
});
我希望它看起來像下面粗糙的線框。
我很感激任何人可能有任何反饋! 非常感謝。
你想要的是BoxLayout ,它以列或行的形式提供UI元素。 然后你可以將它們嵌套在另一個中,例如將一個水平框布局面板作為另一個垂直的元素(有點像嵌套的HTML表格)。 所以你的所有元素都會進入頂層垂直BoxLayout,而擁有JLabel2和JTextField的行將是它自己的水平BoxLayout嵌套在頂層垂直布局中。 這是一個關於布局管理器的相當不錯的教程,它包括BoxLayout。
有許多不同的方法,以及許多不同的LayoutManager
使用。 在這里閱讀更多內容:
以下是我使用GridBagLayout
的示例:
//necessary imports
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
public class Test {
/**
* Default constructor for Test.class
*/
public Test() {
initComponents();
}
public static void main(String[] args) {
/**
* Set look and feel of app
*/
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
// If Nimbus is not available, you can set the GUI to another look and feel.
}
/**
* Create GUI and components on Event-Dispatch-Thread
*/
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Test test = new Test();
}
});
}
/**
* Initialize GUI and components (including ActionListeners etc)
*/
private void initComponents() {
JFrame jFrame = new JFrame("Chat Test");
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setResizable(false);
//Initialise Panel 1 & Components
JPanel p1 = new JPanel(new GridBagLayout());
JPanel p2 = new JPanel(new GridBagLayout());
//Label 1 - For TextArea
JLabel l1 = new JLabel("Chat Log");
//TextArea - To display conversation
final JTextArea t1 = new JTextArea(10, 10);
JScrollPane pane = new JScrollPane(t1);
//Label 2 - For TextField
JLabel l2 = new JLabel("Message");
//Message Box - For user input
final JTextField t2 = new JTextField(10);
//Button 1 - To send message
JButton b1 = new JButton("Send");
GridBagConstraints gc = new GridBagConstraints();
gc.fill = GridBagConstraints.HORIZONTAL;
gc.weightx = 1;
gc.gridx = 0;
gc.gridy = 0;
p1.add(l1, gc);
gc.gridx = 0;
gc.gridy = 1;
p1.add(pane, gc);
GridBagConstraints gc2 = new GridBagConstraints();
gc2.fill = GridBagConstraints.HORIZONTAL;
gc2.weightx = 1;
gc2.gridx = 0;
gc2.gridy = 0;
gc2.ipadx = 10;
p2.add(l2, gc2);
gc2.gridx = 1;
gc2.gridy = 0;
p2.add(t2, gc2);
gc2.gridx = 1;
gc2.gridy = 1;
p2.add(b1, gc2);
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ev) {
//do something
t1.setText(t2.getText());
}
});
jFrame.add(p1, BorderLayout.CENTER);
jFrame.add(p2, BorderLayout.SOUTH);
//pack frame (size JFrame to match preferred sizes of added components and set visible
jFrame.pack();
jFrame.setVisible(true);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.