Java BorderLayout

BorderLayout是什么布局啊FlowLayoutGridLayout呢?谁能举个例子啊?最好各举一个!!谢谢大家了!... BorderLayout是什么布局 啊 FlowLayoutGridLayout呢 ?谁能举个例子啊 ? 最好各举一个 !!谢谢大家了 ! 展开
 我来答
匿名用户
2013-11-26
展开全部
BorderLayout:import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
public class StarBorderLayout extends JFrame{
int i = 0;
JButton button1;
String[] buttonName = {"North","迟郑South","Center","West","East"};
JButton[] buttonArray = new JButton[5];
JPanel panel1 , panel2 ;
public StarBorderLayout(){
super("BorderLayout");
setSize(300,200);
setVisible(true);
panel1 = new JPanel();
panel2 = new JPanel();
button1 = new JButton("显示");
panel1.add(button1);
panel2.setLayout(new BorderLayout());
for(String string : buttonName){
buttonArray[i] = new JButton(string);
panel2.add(buttonName[i],buttonArray[i]);
i++;
}

for(JButton button : buttonArray){
button_click(button);
}
button_click(button1);
add("North",panel1);
add("Center",panel2);
}
private void button_click(final JButton b) {
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1){

button1.setVisible(true);
for(JButton button : buttonArray)
button.setVisible(true);
}
else
b.setVisible(false);
}
});
}
public static void main(String args[]){
new StarBorderLayout();
}
}
FlowLayout:import java.awt.FlowLayout;import javax.swing.*;public class StarFlowLayout extends JFrame{
JButton[] buttonArray = new JButton[8];
public StarFlowLayout(){
super("FlowLayout");
setLayout(new FlowLayout(FlowLayout.LEFT,15,15)); // 第一个参数表示向左靠齐,第二个是列间距,第三个是行间距
setSize(300,400);
setVisible(true);
for(int i = 0; i <=7 ; i++){
buttonArray[i] = new JButton("button"+i);
add(buttonArray[i]);
}
}
public static void main(String args[]){
new StarFlowLayout();
}
}
GridLayout:import java.awt.*;import javax.swing.*;
public class StarGridLayout extends JFrame {
int i = 0;
String[] string = {"7","8","9","/"搜裤,"4","5","6","*","1","码漏颂2","3","+","0",".","=","-"};
JTextField textfield = new JTextField();
JButton[] buttonArray = new JButton[20];
JPanel panel1 , panel2 ;
public StarGridLayout(){
super("GridLayout");
setSize(300,400);
setVisible(true);
panel1 = new JPanel(new BorderLayout());
panel1.add(textfield);

panel2 = new JPanel(new GridLayout(4,5));
for(String str : string){
buttonArray[i] = new JButton(str);
panel2.add(buttonArray[i]);

i++;
}

add("North",panel1);
add("Center",panel2);
} public static void main(String[] args) {
new StarGridLayout();
}}
匿名用户
2013-11-26
展开全部
这梁拍局是个例子 import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class BorderLayoutDemo extends JFrame implements ActionListener {
private JButton buttons[];
private final String names[] = { "North", "South", "East", "West", "Center"贺桥 };
private BorderLayout layout;
private Container container;

public BorderLayoutDemo() {
super("BoderLayout布局");
layout = new BorderLayout(5, 5);
container = getContentPane();
container.setLayout(layout);
buttons = new JButton[names.length];
for (int count = 0; count < names.length; count++) {
buttons[count] = new JButton(names[count]);
buttons[count].addActionListener(this);
}
container.add(buttons[0], BorderLayout.NORTH);
container.add(buttons[1], BorderLayout.SOUTH);
container.add(buttons[2], BorderLayout.EAST);
container.add(buttons[3], BorderLayout.WEST);
container.add(buttons[4], BorderLayout.CENTER);
setSize(300, 200);
setVisible(true);
}

public void actionPerformed(ActionEvent event) {
for (int count = 0; count <橡让 buttons.length; count++)
if (event.getSource() == buttons[count])
buttons[count].setVisible(false);
else
buttons[count].setVisible(true);
layout.layoutContainer(container);
}

public static void main(String[] args) {
BorderLayoutDemo application = new BorderLayoutDemo();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2013-11-26
展开全部
1顺序布局管理器import java.awt.*;
public class TestFlowLayout{
public static void main(String args[]) {
Frame f=new Frame("这是一个布局管理器");
f.setLayout(new FlowLayout());
f.add(new Button("第一个按钮"));
f.add(new Button("第二个按钮"));
f.add(new Button("第三个按钮"));
f.add(new Button("第四个蠢搭洞按钮"));
f.setSize(300,300);
f.setVisible(true);
}
} 2.边界布局管理器import java.awt.*;
public class TestBorderLayout {
public static void main(String args[]) {
Frame f=new Frame();
f.setLayout(new BorderLayout());
f.add("North",new Button("第一个按钮"));
f.add("West",new Button("第二个按钮"));
f.setSize(300,300);
f.setVisible(true);
}
} 3.网格布局管理器 import java.awt.*;
public class GridLayoutExample{
public static void main(String args[]) {
Frame f=new Frame("网格布局演示");
String str[]={"1","2","3","4","带枯5","6","7"枝猛,"8","9"};
f.setLayout(new GridLayout(3,3));
Button btn[]=new Button[str.length];
for(int i=0;i<str.length;i++){
btn[i]=new Button(str[i]);
f.add(btn[i]);
}
f.setSize(300,200);
f.setVisible(true);
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式