GridLayout

本文介绍了一个简单的Java Swing程序,该程序能够通过点击按钮在两种不同的GridLayout布局之间进行切换。初始布局为2行3列,点击按钮后切换到3行2列,再次点击则返回原始布局。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package gui;

 

 

import java.awt.*;

import java.awt.event.*;

 

import javax.swing.*;

/**

 * 编写一个Java GUI应用程序,窗口标题为“GridLayout”,

 * 窗口布局如下图A所示,在图A窗口中单击任意一个Button,

 * 网格的划分方式会变化为图B;在图B窗口中单击任意一个Button,

 * 网格的划分方式会变化为图A。(25分)

 * @since 2010-9-20

 * @start 12:34

 * @end 12:55

 * @version 0.1

 * @author allchin

 * @serial 1

 * */

public class Four_LayoutChanger {

public static void main(String[] args){

LayoutChanger lc=new LayoutChanger();

lc.init();

lc.start();

}

}

class LayoutChanger extends JFrame implements ActionListener{

GridLayout gl=new GridLayout(2,3),

  gl2=new GridLayout(3,2);

Button btn1=new Button("one"),

  btn2=new Button("two"),

  btn3=new Button("three"),

  btn4=new Button("four"),

  btn5=new Button("five"),

  btn6=new Button("six");

  

  

public void init(){

 

this.getContentPane().setLayout(gl);

this.setBounds(111, 111, 800, 600);

this.setVisible(true);

this.validate();

this.getContentPane().add(btn1);

this.getContentPane().add(btn2);

this.getContentPane().add(btn3);

this.getContentPane().add(btn4);

this.getContentPane().add(btn5);

this.getContentPane().add(btn6);

btn1.addActionListener(this);

btn2.addActionListener(this);

btn3.addActionListener(this);

btn4.addActionListener(this);

btn5.addActionListener(this);

btn6.addActionListener(this);

}

public void start(){

}

 

@Override

public void actionPerformed(ActionEvent e) {

this.getContentPane().removeAll();

 

if(this.getContentPane().getLayout().equals(gl)){

this.getContentPane().setLayout(gl2);

}

else

if(this.getContentPane().getLayout().equals(gl2)){

this.getContentPane().setLayout(gl);

}

 

 

this.getContentPane().add(btn1);

this.getContentPane().add(btn2);

this.getContentPane().add(btn3);

this.getContentPane().add(btn4);

this.getContentPane().add(btn5);

this.getContentPane().add(btn6);

this.getContentPane().validate();

this.validate();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值