java swing container_Java Swing:如何动态更改GUI

动态更改布局的示例:

package swinglayout;

import java.awt.BorderLayout;

import java.awt.Container;

import java.awt.FlowLayout;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class LayoutChanger implements ActionListener{

JButton b1;

JButton b2;

JButton b3;

JButton b4;

JButton b5;

JButton b6;

/** This button set the flowlayout on panel2 with left orientation */

JButton flowLayout;

/** This button set the Gridlayout of 2,3 grid on panel2 */

JButton gridLayout;

/** This button set the Borderlayout on panel2*/

JButton borderLayout;

/**

* This panel is control panel where we use button to change

* layout of another panel

*/

JPanel panel;

/** This panel contain multiple button from b1 to b6 */

JPanel panel2;

JFrame frame;

public LayoutChanger() {

//set Default Look and Feel on frame

JFrame.setDefaultLookAndFeelDecorated(true);

frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Container con = frame.getContentPane();

con.setLayout(new BorderLayout());

panel = new JPanel();

panel2 = new JPanel();

//This button are used to only showing the layout effect

b1 = new JButton("HelloButton1");

b2 = new JButton("HelloButton2");

b3 = new JButton("HelloButton3");

b4 = new JButton("HelloButton4");

b5 = new JButton("HelloButton5");

b6 = new JButton("HelloButton6");

// By default panel have layout

panel2.add(b1);

panel2.add(b2);

panel2.add(b3);

panel2.add(b4);

panel2.add(b5);

panel2.add(b6);

// Layout changer button

flowLayout = new JButton("FlowLayout");

gridLayout = new JButton("GridLayout");

borderLayout = new JButton("BorderLayout");

//call Action listener on  every layout changer button

flowLayout.addActionListener(this);

gridLayout.addActionListener(this);

borderLayout.addActionListener(this);

panel.add(flowLayout);

panel.add(gridLayout);

panel.add(borderLayout);

// add layout changer button panel at a top

//button panel at the center of container

con.add(panel, BorderLayout.PAGE_START);

con.add(panel2, BorderLayout.CENTER);

frame.setVisible(true);

frame.pack();

}

public void actionPerformed(ActionEvent e) {

//set the flowlayout on panel2

if(e.getSource() == flowLayout) {

FlowLayout flow = new FlowLayout(FlowLayout.LEFT);

panel2.setLayout(flow);

panel2.validate();

}

//set the gridlayout on panel2

if(e.getSource() == gridLayout) {

GridLayout grid = new GridLayout(2,3);

panel2.setLayout(grid);

panel2.validate();

}

//set the gridlayout but the problem if we don't set the constraint

//all button are set on center. So you remove the all button from panel

//Then set grid layout on panel and add them with constraints.

if(e.getSource() == borderLayout) {

panel2.remove(b1);

panel2.remove(b2);

panel2.remove(b3);

panel2.remove(b4);

panel2.remove(b5);

panel2.remove(b6);

BorderLayout border = new BorderLayout();

panel2.setLayout(border);

panel2.add(b1,BorderLayout.NORTH);

panel2.add(b2,BorderLayout.SOUTH);

panel2.add(b3,BorderLayout.EAST);

panel2.add(b4,BorderLayout.WEST);

panel2.add(b5,BorderLayout.CENTER);

panel2.add(b6,BorderLayout.BEFORE_FIRST_LINE);

panel2.validate();

}

}

public static void main(String args[]) {

new LayoutChanger();

}

}

记住一件事是您在面板上设置了新的布局,不要忘记调用面板上的方法validate(),如果不调用此方法,则不会看到布局更改的影响。如果要通过调用来查看效果,则必须调整框架的大小。你也可以很容易像设置相同的布局FlowLayout,GridLayout和BoxLayout,但设置时BorderLayout就需要限制添加元素,所以我们首先从面板中删除所有组件remove(Component comp)方法然后通过约束添加的组件面板

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值