我正在制作自己的Java套接字游戏。我的游戏可以在全屏上绘画(上面写着“在此处绘画图形”,但现在我在整个jframe上绘画)。我想添加一个带有滚动条的文本框,该滚动条仅用于显示文本,不接收任何输入,另一个文本框用于接收用户的文本输入,然后添加一个用于发送文本的按钮,用于聊天。但是,在我的问题上,我该如何开始进行布局?我知道我需要布局,但是有人可以帮我吗?此刻是我的代码(此代码此刻仅设置整个屏幕的绘画,现在需要像上图所示将屏幕划分开):
public class Setup extends JFrame implements Runnable{
JPanel panel;
JFrame window;
public Setup(Starter start, JFrame window){
window.setSize(600,500);
window.setLocationRelativeTo(null);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(false);
panel = new Display(start);
this.window = window;
}
public void run(){
window.getContentPane().add(panel);
window.setBackground(Color.BLACK);
window.setVisible(true);
}
}
“新的Display(开始)”-扩展了jpanel,基本上是我在所有图形上都进行明智绘制的地方。
另外,我看到人们添加了不同的面板,但是我不能让它们的尺寸相同。就像图片中一样,“此处绘制图形”面板是最大的面板,依此类推。
在JPanel
实际上仅仅是一个容器,你可以把不同的元素在它(甚至其他JPanels
)。因此,在您的情况下,我建议您为窗口使用一个大的JPanel
某种主容器。这主要面板您指定一个Layout
适合您的需要(这里是介绍了布局)。
在将布局设置为主面板后,您可以添加绘画面板和所需的其他JPanel(例如其中包含文本的面板)。
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,它可以处理非常复杂的布局,但是学习起来也很复杂。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句