JPanel
實際上只是一個容器,您可以在其中放入不同的元素(甚至是其他JPanels
)。所以在你的情況下,我會建議一個大的JPanel
作爲某種主要容器爲您的窗口。 主面板您指定一個適合您需要的Layout
(here is an introduction to the layouts)。
在設置佈局你主面板您可以添加烤漆面板和你想要的其他JPanels(像那些在它的文本。)。
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
JPanel paintPanel = new JPanel();
JPanel textPanel = new JPanel();
mainPanel.add(paintPanel);
mainPanel.add(textPanel);
這只是一個排序所有子板垂直(Y軸)的例子。所以如果你想在mainPanel的底部(也許是一些圖標或按鈕)使用另一個佈局(比如水平佈局)來組織其他東西,只需再次創建一個新的JPanel作爲所有其他東西的容器並設置爲setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS)
。
正如您將會發現的那樣,佈局非常僵硬,並且可能很難找到面板的最佳佈局。所以不要放棄,閱讀介紹(上面的鏈接)並查看圖片 - 這就是我的做法:)
或者您可以使用NetBeans編寫您的程序。你有一個非常簡單的可視化編輯器(拖放)來創建各種Windows和框架。 (只有理解代碼之後是......棘手有時。)
編輯
因爲有興趣在這個問題做了很多人,我想提供的如何佈置一個JFrame,使其成爲完整的例子看起來像OP想要的。
類被稱爲MyFrame,延長波動的JFrame
public class MyFrame extends javax.swing.JFrame{
// these are the components we need.
private final JSplitPane splitPane; // split the window in top and bottom
private final JPanel topPanel; // container panel for the top
private final JPanel bottomPanel; // container panel for the bottom
private final JScrollPane scrollPane; // makes the text scrollable
private final JTextArea textArea; // the text
private final JPanel inputPanel; // under the text a container for all the input elements
private final JTextField textField; // a textField for the text the user inputs
private final JButton button; // and a "send" button
public MyFrame(){
// first, lets create the containers:
// the splitPane devides the window in two components (here: top and bottom)
// users can then move the devider and decide how much of the top component
// and how much of the bottom component they want to see.
splitPane = new JSplitPane();
topPanel = new JPanel(); // our top component
bottomPanel = new JPanel(); // our bottom component
// in our bottom panel we want the text area and the input components
scrollPane = new JScrollPane(); // this scrollPane is used to make the text area scrollable
textArea = new JTextArea(); // this text area will be put inside the scrollPane
// the input components will be put in a separate panel
inputPanel = new JPanel();
textField = new JTextField(); // first the input field where the user can type his text
button = new JButton("send"); // and a button at the right, to send the text
// now lets define the default size of our window and its layout:
setPreferredSize(new Dimension(400, 400)); // let's open the window with a default size of 400x400 pixels
// the contentPane is the container that holds all our components
getContentPane().setLayout(new GridLayout()); // the default GridLayout is like a grid with 1 column and 1 row,
// we only add one element to the window itself
getContentPane().add(splitPane); // due to the GridLayout, our splitPane will now fill the whole window
// let's configure our splitPane:
splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT); // we want it to split the window verticaly
splitPane.setDividerLocation(200); // the initial position of the divider is 200 (our window is 400 pixels high)
splitPane.setTopComponent(topPanel); // at the top we want our "topPanel"
splitPane.setBottomComponent(bottomPanel); // and at the bottom we want our "bottomPanel"
// our topPanel doesn't need anymore for this example. Whatever you want it to contain, you can add it here
bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.Y_AXIS)); // BoxLayout.Y_AXIS will arrange the content vertically
bottomPanel.add(scrollPane); // first we add the scrollPane to the bottomPanel, so it is at the top
scrollPane.setViewportView(textArea); // the scrollPane should make the textArea scrollable, so we define the viewport
bottomPanel.add(inputPanel); // then we add the inputPanel to the bottomPanel, so it under the scrollPane/textArea
// let's set the maximum size of the inputPanel, so it doesn't get too big when the user resizes the window
inputPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 75)); // we set the max height to 75 and the max width to (almost) unlimited
inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.X_AXIS)); // X_Axis will arrange the content horizontally
inputPanel.add(textField); // left will be the textField
inputPanel.add(button); // and right the "send" button
pack(); // calling pack() at the end, will ensure that every layout and size we just defined gets applied before the stuff becomes visible
}
public static void main(String args[]){
EventQueue.invokeLater(new Runnable(){
@Override
public void run(){
new MyFrame().setVisible(true);
}
});
}
}
請注意,這只是一個例子,有佈局的窗口多種方法。這一切都取決於你的需求,如果你想要的內容是可調整大小/響應。另一個非常好的方法是GridBagLayout,它可以處理相當複雜的佈局,但學習起來也相當複雜。
任何一個希望在爲downvote原因闡明,我很想學習一種如何可能提高對答案 – MadProgrammer