第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Java Swing:如何動態(tài)更改GUI

Java Swing:如何動態(tài)更改GUI

藍(lán)山帝景 2019-10-21 15:28:50
我需要動態(tài)添加組件。此外,我需要動態(tài)更改布局。
查看完整描述

2 回答

?
心有法竹

TA貢獻(xiàn)1866條經(jīng)驗 獲得超5個贊

動態(tài)更改布局的示例:


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();

  }

}

記住一件事是您在面板上設(shè)置了新的布局,不要忘記調(diào)用面板上的方法validate(),如果不調(diào)用此方法,則不會看到布局更改的影響。如果要通過調(diào)用來查看效果,則必須調(diào)整框架的大小。你也可以很容易像設(shè)置相同的布局FlowLayout,GridLayout和BoxLayout,但設(shè)置時BorderLayout就需要限制添加元素,所以我們首先從面板中刪除所有組件remove(Component comp)方法然后通過約束添加的組件面板


查看完整回答
反對 回復(fù) 2019-10-21
  • 2 回答
  • 0 關(guān)注
  • 888 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號