实际上只是一个容器,您可以在其中放置不同的元素(甚至是其他)。因此,在您的情况下,我建议将一个大容器作为您窗口的某种主容器。您分配的主面板适合您的需求(这是布局的介绍)。JPanel
JPanels
JPanel
Layout
将布局设置为主面板后,您可以添加油漆面板和所需的其他JPanels(例如其中有文本的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和Frames。(之后只理解代码是...有时很棘手。
编辑
由于有很多人对这个问题感兴趣,我想提供一个完整的例子,说明如何布局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,它可以处理相当复杂的布局,但学习起来也非常复杂。