【问题标题】:Java Swing: How to change GUI dynamicallyJava Swing:如何动态更改 GUI
【发布时间】:2011-08-10 15:22:35
【问题描述】:

我需要动态添加组件。此外,我需要动态改变布局。

【问题讨论】:

  • 我想我代表所有人祝你在这件事上好运! Swing 应该是可行的,但与任何事情一样,魔鬼在细节中。你读过布局管理器的教程吗?您对尝试过的代码有任何具体问题或问题吗?您当前的问题是否有任何细节可以告诉我们,以帮助我们帮助您?不幸的是,您当前提出的问题过于笼统,无法给出具体答案。

标签: java swing


【解决方案1】:

作为参考,这里有一个sscce,它显示了基本方法validate()。这个更详细的example 显示了这两个要求:它改变了布局并动态添加了组件。

import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.*;

/** @see http://stackoverflow.com/questions/5750068 */
public class DynamicLayout extends JPanel {

    private static final LayoutManager H = new GridLayout(1, 0);
    private static final LayoutManager V = new GridLayout(0, 1);

    public DynamicLayout() {
        this.setLayout(H);
        this.setPreferredSize(new Dimension(320, 240));
        for (int i = 0; i < 3; i++) {
            this.add(new JLabel("Label " + String.valueOf(i), JLabel.CENTER));
        }
    }

    private void display() {
        JFrame f = new JFrame("DynamicLayout");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        JPanel p = new JPanel();
        p.add(new JButton(new AbstractAction("Horizontal") {

            @Override
            public void actionPerformed(ActionEvent e) {
                DynamicLayout.this.setLayout(H);
                DynamicLayout.this.validate();
            }
        }));
        p.add(new JButton(new AbstractAction("Vertical") {

            @Override
            public void actionPerformed(ActionEvent e) {
                DynamicLayout.this.setLayout(V);
                DynamicLayout.this.validate();
            }
        }));
        f.add(p, BorderLayout.SOUTH);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new DynamicLayout().display();
            }
        });
    }
}

【讨论】:

    【解决方案2】:

    动态改变布局的例子:

    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) 方法从面板中删除所有组件,然后添加受约束的面板中的组件

    【讨论】:

      猜你喜欢
      • 2022-01-18
      • 1970-01-01
      • 1970-01-01
      • 2011-09-15
      • 1970-01-01
      • 1970-01-01
      • 2017-03-13
      • 1970-01-01
      • 2014-04-02
      相关资源
      最近更新 更多