如何通过ItemListener更改流布局



我不能使用分组复选框来更改FlowLayout的对齐方式。我确信选中复选框会进入"itemStateChanged"方法,但.setAssignment()或.setLayout()都不起作用!!有什么方法可以解决这个问题吗?非常感谢。

import java.awt.*;
import java.awt.event.*;
public class flowlayout
{
static Checkbox cb1 = new Checkbox("Left");
static Checkbox cb2 = new Checkbox("Center");
static Checkbox cb3 = new Checkbox("Right");    
static Frame frm = new Frame("Frame Title");    
public static void main(String args[])  {
    frm.setLayout(new FlowLayout());
    CheckboxGroup cg = new CheckboxGroup();
    cb1.setCheckboxGroup(cg);
    cb2.setCheckboxGroup(cg);
    cb3.setCheckboxGroup(cg);       
    cb1.addItemListener(new ItmLis());
    cb2.addItemListener(new ItmLis());
    cb3.addItemListener(new ItmLis());      
    frm.setSize(400,200);
    frm.add(cb1);
    frm.add(cb2);
    frm.add(cb3);   
    frm.setVisible(true);       
}   
static class ItmLis implements ItemListener
{
    public void itemStateChanged(ItemEvent e)
    {
        if(cb1.getState()==true)
        {
            frm.setLayout(new FlowLayout(FlowLayout.LEFT));
        }
        else if(cb2.getState())
        {
            frm.setLayout(new FlowLayout(FlowLayout.CENTER));
        }
        else if(cb3.getState())
        {
            frm.setLayout(new FlowLayout(FlowLayout.RIGHT));
        }
    }
}
}
  • LayoutManager尚未实现任何、部分或整个容器存在重新连接的通知程序

  • 必须通过container.validate()通知LayoutManager;和container.repaint();(在Java7中也对JFrame使用revalidate())

  • 必须通过添加WindowKistener来终止当前JVM实例(仅覆盖终止JVM)

  • 使用Swing over the史前AWT

AWT版本

import java.awt.*;
import java.awt.event.*;
import javax.swing.SwingUtilities;
public class MyFlowLayout {
    static Checkbox cb1 = new Checkbox("Left");
    static Checkbox cb2 = new Checkbox("Center");
    static Checkbox cb3 = new Checkbox("Right");
    static Frame frm = new Frame("Frame Title");
    public MyFlowLayout() {
        frm.setLayout(new FlowLayout());
        CheckboxGroup cg = new CheckboxGroup();
        cb1.setCheckboxGroup(cg);
        cb2.setCheckboxGroup(cg);
        cb3.setCheckboxGroup(cg);
        cb1.addItemListener(new ItmLis());
        cb2.addItemListener(new ItmLis());
        cb3.addItemListener(new ItmLis());
        frm.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        frm.setSize(400, 200);
        frm.add(cb1);
        frm.add(cb2);
        frm.add(cb3);
        frm.setVisible(true);
    }
    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                MyFlowLayout fs = new MyFlowLayout();
            }
        });
    }
    private class ItmLis implements ItemListener {
        @Override
        public void itemStateChanged(ItemEvent e) {
            if (cb1.getState() == true) {
                frm.setLayout(new FlowLayout(FlowLayout.LEFT));
                validateLayout();
            } else if (cb2.getState()) {
                frm.setLayout(new FlowLayout(FlowLayout.CENTER));
                validateLayout();
            } else if (cb3.getState()) {
                frm.setLayout(new FlowLayout(FlowLayout.RIGHT));
                validateLayout();
            }
        }
        public void validateLayout() {
            frm.validate();
            frm.repaint();
        }
    }
}

摆动型

import java.awt.*;
import java.awt.event.*;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;
public class MyFlowLayout {
    static JRadioButton cb1 = new JRadioButton("Left");
    static JRadioButton cb2 = new JRadioButton("Center");
    static JRadioButton cb3 = new JRadioButton("Right");
    static JFrame frm = new JFrame("Frame Title");
    public MyFlowLayout() {
        frm.setLayout(new FlowLayout());
        ButtonGroup cg = new ButtonGroup();
        cg.add(cb1);
        cg.add(cb2);
        cg.add(cb3);
        cb1.addItemListener(new ItmLis());
        cb2.addItemListener(new ItmLis());
        cb3.addItemListener(new ItmLis());
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // or WindowListener too
        /*frm.addWindowListener(new WindowAdapter() {
         @Override
         public void windowClosing(WindowEvent e) {
         System.exit(0);
         }
         });*/
        frm.setSize(400, 200);
        frm.add(cb1);
        frm.add(cb2);
        frm.add(cb3);
        frm.setVisible(true);
    }
    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                MyFlowLayout fs = new MyFlowLayout();
            }
        });
    }
    private class ItmLis implements ItemListener {
        @Override
        public void itemStateChanged(ItemEvent e) {
            if (cb1.isSelected() == true) {
                frm.setLayout(new FlowLayout(FlowLayout.LEFT));
                validateLayout();
            } else if (cb2.isSelected() == true) {
                frm.setLayout(new FlowLayout(FlowLayout.CENTER));
                validateLayout();
            } else if (cb3.isSelected() == true) {
                frm.setLayout(new FlowLayout(FlowLayout.RIGHT));
                validateLayout();
            }
        }
        public void validateLayout() {
            frm.validate();
            frm.repaint();
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新