Java 边界和流布局
好吧,我今天的编程练习遇到了一些问题 练习内容如下: (使用流程布局管理器)编写满足以下要求的程序:Java 边界和流布局,java,swing,border-layout,flowlayout,Java,Swing,Border Layout,Flowlayout,好吧,我今天的编程练习遇到了一些问题 练习内容如下: (使用流程布局管理器)编写满足以下要求的程序: 创建一个框架并将其布局设置为FlowLayout 创建两个面板并将其添加到框架中 每个面板包含三个按钮。面板使用流程布局 按钮应命名为“按钮1”、“按钮2”等。(我填写了原始代码) 现在我需要将代码更改为BorderLayout,同时将一个面板移到南部,另一个面板移到中间,我尝试了,但结果似乎不正确。按钮就在顶部和底部 原始代码(FlowLayout): 我尝试使用边框布局: import
- 创建一个框架并将其布局设置为FlowLayout
- 创建两个面板并将其添加到框架中
- 每个面板包含三个按钮。面板使用流程布局
import javax.swing.*;
import java.awt.*;
public class lab5_1 extends JFrame {
public lab5_1() {
setLayout(new FlowLayout());
// Create two panels
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
// Add three buttons to each panel
panel1.add(new JButton(" Button 1 "));
panel1.add(new JButton(" Button 2 "));
panel1.add(new JButton(" Button 3 "));
panel2.add(new JButton(" Button 4 "));
panel2.add(new JButton(" Button 5 "));
panel2.add(new JButton(" Button 6 "));
// Add panels to frame
add(panel1);
add(panel2);
}
public static void main(String[] args) {
lab5_1 frame = new lab5_1();
frame.setTitle(" Exercise 12_1 ");
frame.setSize(600,75);
frame.setLocationRelativeTo(null); // center frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
public class lab5_2 extends JFrame {
public lab5_2() {
setLayout(new BorderLayout());
// Create two panels
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
// Add three buttons to each panel
panel1.add(new JButton(" Button 1 "));
panel1.add(new JButton(" Button 2 "));
panel1.add(new JButton(" Button 3 "));
panel2.add(new JButton(" Button 4 "));
panel2.add(new JButton(" Button 5 "));
panel2.add(new JButton(" Button 6 "));
//Add Panel to frame
add(panel1, BorderLayout.CENTER);
add(panel2, BorderLayout.SOUTH);
}
public static void main(String[] args) {
lab5_2 frame = new lab5_2();
frame.setTitle(" Exercise 12_2 ");
frame.setSize(200, 200);
frame.setLocationRelativeTo(null); // center frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
中心区域获得尽可能多的可用空间。其他区域仅根据需要扩展以填充所有可用空间 因为你没有使用(=填充)
北部
或西部
或东部
区域,所以中部
区域填充了南部
之后剩下的空间(至少我理解这样的情况)。如果不是这样,您可能会看到未使用区域的空白。@pasty我的代码是正确的,但位置是关闭的,因为其余区域没有填充?同意pasty。中心部分将展开以填充所有可用空间。可能会显示一张您所期望的与您所得到的图片。是的-您需要添加更多元素(或移动现有元素)到另一个区域,或者像这样展示您的解决方案并解释输出。;-)@非常感谢您的快速回答,只是想确保我的代码没有错(: