(JDialog)对话框实现数据交换

      对话框分为模式对话框和无模式对话框。模式对话框就是在弹出对话框后,父组件阻塞直到完成对话框的操作,父组件被激活。无模式对话框就是对话框与父组件并行运行。

         JDialog(Dialog owner, String title, boolean modal) 

   其中modal为true即为模式对话框,false为无模式对话框。只要将要在对话框中显示的内容(可以是JPanel)加入JDialog中即可实现模式对话框,其中的阻塞操作是JDialog完成的。

   这里实现一个例子,完成选择多个数组的功能,并可以返回所选择的数组。

(JDialog)对话框实现数据交换_第1张图片

 

/* * To change this template, choose Tools | Templates * and open the template in the editor. */ import javax.swing.*; import java.util.*; import java.awt.*; import java.awt.event.*; /** * * @author KILLer */ public class ShowGroupDialog extends JPanel{ private Vector<ArrayList<Integer>> groupsSelected = new Vector(); public static final int ENTER = 1; public static final int CANCLE = 2; private int answer=2;//通过一个标志,来区分是否进行了选择 private JDialog dialog; private ArrayList<Integer>[] groups; private Frame parent; private JButton butEnter; private JButton butCancle; private final JList listOption; private final JList listSelected; private JLabel selectLabel,northLabel; private JPanel northPanel,centerPanel,panel2; public ShowGroupDialog(ArrayList<Integer>[] group, Frame parent, int axisNum){ this.groups=group; this.parent=parent; this.listOption = new JList(groups); this.listSelected = new JList(); this.butEnter = new JButton("Entry"); this.butCancle = new JButton("Cancle"); this.northPanel = new JPanel(new GridLayout(1, 2, 5, 10)); this.selectLabel=new JLabel("Groups Selected"); this.centerPanel = new JPanel(new GridLayout(1, 2, 5, 10)); this.northLabel = new JLabel(axisNum + " Axises " + groups.length + " Groups"); this.panel2 = new JPanel(); this.setLayout(new BorderLayout()); listOption.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { if (e.getClickCount() >= 2) { ArrayList<Integer> g = groups[listOption.getSelectedIndex()]; if (!groupsSelected.contains(g)) { groupsSelected.add(g); listSelected.setListData(groupsSelected); } } } }); listSelected.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { if (e.getClickCount() >= 2) { ArrayList<Integer> g = groupsSelected.get(listSelected.getSelectedIndex()); groupsSelected.remove(g); listSelected.setListData(groupsSelected); } } }); butEnter.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { answer = ShowGroupDialog.ENTER; dialog.setVisible(false); } }); butCancle.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { dialog.setVisible(false); } }); panel2.add(butEnter); panel2.add(butCancle); northPanel.add(northLabel); northPanel.add(selectLabel); centerPanel.add(new JScrollPane(listOption)); centerPanel.add(new JScrollPane(listSelected)); this.add(northPanel, BorderLayout.NORTH); this.add(centerPanel, BorderLayout.CENTER); this.add(panel2, BorderLayout.SOUTH); } public int showDialog(){ Frame owner = null; if (parent instanceof Frame) owner = (Frame) parent; else owner = (Frame) SwingUtilities.getAncestorOfClass(Frame.class, parent); if (dialog == null || dialog.getOwner() != owner) { dialog = new JDialog(owner, true); dialog.add(this); dialog.getRootPane().setDefaultButton(butEnter); dialog.pack(); } dialog.setLocationRelativeTo(owner); dialog.setTitle("All groups"); dialog.setVisible(true); dialog.setSize(100,200); Point p=owner.getLocation(); Dimension ownerSize=owner.getSize(); Dimension dialogSize=dialog.getSize(); double x=p.getX()+(ownerSize.getWidth()-dialogSize.getWidth())/2; double y=p.getY()+(ownerSize.getHeight()-dialogSize.getHeight())/2; dialog.setLocation((int)x, (int)y); return answer; } public Vector<ArrayList<Integer>> getSelctions(){ return this.groupsSelected; } public static void main(String[] a) { JFrame f=new JFrame(); f.setBounds(100,100,600,600); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); ArrayList<Integer>[] al=new ArrayList[5]; for(int i=0;i<al.length;i++){ al[i]=new ArrayList<Integer>(); for(int j=i*2;j<i*3+1;j++) al[i].add(j); } ShowGroupDialog sgf=new ShowGroupDialog(al, f, 16); int i=sgf.showDialog(); if(i==ShowGroupDialog.ENTER) JOptionPane.showMessageDialog(f, "OK"); else JOptionPane.showMessageDialog(f, "Cancle"); } }

 

你可能感兴趣的:(vector,null,dialog,import,tools,templates)