我的问题是对于理解这个简单的代码来说可能是非常基本的。我自己编写了这段代码,从各处抓取一些代码来理解。我想实际逐行遵循此代码,了解每一行的含义?
我在代码行上方添加了我的理解作为注释,它可能是错误的,或者其中一些标记为 ****
意味着我只是不知道它的含义。如果你能在这里帮助我,那就太好了。
谢谢
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.text.ParseException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class TestingSwingComponents {
public TestingSwingComponents() {
//Create a frame which is the window that pops up
JFrame myframe = new JFrame();
//*****
myframe.getContentPane().setLayout(new BorderLayout());
//set the frame size to be 600 X 600 size
myframe.setSize(600, 600);
// create Pane1
JPanel myPanel = new JPanel();
//set the Layout component of Panel, as how you would like it to be
//here it is 2 rows and 15 columns
myPanel.setLayout(new GridLayout(2, 15));
//create a button with text in it
JButton letterButton = new JButton("click Me");
//add the created button component to the panel
myPanel.add(letterButton);
//******
myframe.getContentPane().add(myPanel, BorderLayout.SOUTH);
// create another panel
JPanel panelFormat = new JPanel();
//create a textfield
JTextField txtfield = new JTextField();
//create a label for the textfield
JLabel label = new JLabel("Guesss");
//set the layout type for this panel
panelFormat.setLayout(new BorderLayout());
//add label to panel
panelFormat.add(label);
//add textfield to panel
panelFormat.add(txtfield);
//I dont know the difference between the below two
//BorderLayout.CENTER still does not center the panel in the frame, I dont know why
myframe.getContentPane().add(panelFormat, BorderLayout.CENTER);
myframe.add(panelFormat);
// default settings
myframe.setTitle("Get buttons");
myframe.setVisible(true);
myframe.setLocationRelativeTo(null);
myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) throws ParseException {
new TestingSwingComponents();
}
}
最佳答案
myframe.getContentPane().setLayout(new BorderLayout());
要回答这个问题,您需要了解 Swing 窗口的结构。 JFrame
(实际上是任何 Swing 窗口)由一系列生成窗口 View 的组件组成。
(图片来自How to use Root Panes)
JRootPane
构成了 View 的基础,其顶部是 JLayeredPane
以及所谓的“玻璃 Pane ”。 JLayeredPane
负责管理 JMenuBar
和“内容 Pane ”。
内容 Pane 是组件在窗口上驻留的位置。
因此,这一行的意思是,“获取框架的内容 Pane 并将其布局设置为使用 BorderLayout
”
布局 API 本身就是一个完整的问题,您可以阅读 Laying out components within a container对于更深入的描述,但基本上,布局管理器消除了您(很大程度上)关心不同系统所使用的渲染技术差异的需要......
//******
myframe.getContentPane().add(myPanel, BorderLayout.SOUTH);
这又回到了布局管理器。因为您可以拥有任意数量的布局管理器,所以 Swing 允许您在添加组件时向布局管理器传递“约束”,从而使布局管理器了解您可能希望如何添加该组件。
如果你仔细看看BorderLayout
您将看到它有五个可以添加组件的位置。
该行基本上是说,“请将 myPanel
添加到框架/内容 Pane 内的SOUTH
位置”
评论更新
如果你看一下这个片段...
panelFormat.setLayout(new BorderLayout());
//add label to panel
panelFormat.add(label);
//add textfield to panel
panelFormat.add(txtfield);
它将 panelFormat
的布局管理器设置为 BorderLayout
。 BorderLayout
在其五个可用位置中的任何一个中只能有一个组件。当您使用 add(Component)
而不向其传递布局约束时,BorderLayout
使用 CENTER
作为默认位置,这意味着您正在尝试添加将两个组件添加到 CENTER
位置,这是不可能的,因此 BorderLayout
仅使用最后添加的组件。
why not borderlayout fix the size of textfield instead of stretching it all window
因为这就是 BorderLayout
的工作原理,不,GridLayout
可能会做一些类似的事情。
您可以尝试FlowLayout
或GridBagLayout
根据评论更新
您确实需要花时间阅读链接的(和其他建议的)教程...但基本上,您可以使用 GridBagLayout
就像任何其他布局一样,您创建一个实例并将其应用到容器中...
// create another panel
JPanel panelFormat = new JPanel();
//create a textfield
JTextField txtfield = new JTextField(10);
//create a label for the textfield
JLabel label = new JLabel("Guesss");
//set the layout type for this panel
panelFormat.setLayout(new GridBagLayout());
//add label to panel
panelFormat.add(label);
//add textfield to panel
panelFormat.add(txtfield);
//I dont know the difference between the below two
//BorderLayout.CENTER still does not center the panel in the frame, I dont know why
myframe.getContentPane().add(panelFormat, BorderLayout.CENTER);
myframe.add(panelFormat);
关于java - Java Swing 组件的基本问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19394262/