public SwingDnDFrame() { setTitle("SwingDnDTest"); JTabbedPane tabbedPane = new JTabbedPane(); JList list = SampleComponents.list(); tabbedPane.addTab("List", list); JTable table = SampleComponents.table(); tabbedPane.addTab("Table", table); JTree tree = SampleComponents.tree(); tabbedPane.addTab("Tree", tree); JFileChooser fileChooser = new JFileChooser(); tabbedPane.addTab("File Chooser", fileChooser); JColorChooser colorChooser = new JColorChooser(); tabbedPane.addTab("Color Chooser", colorChooser); final JTextArea textArea = new JTextArea(4, 40); JScrollPane scrollPane = new JScrollPane(textArea); scrollPane.setBorder(new TitledBorder(new EtchedBorder(), "Drag text here")); JTextField textField = new JTextField("Drag color here"); textField.setTransferHandler(new TransferHandler("background")); tabbedPane.addChangeListener( new ChangeListener() { public void stateChanged(ChangeEvent e) { textArea.setText(""); } }); tree.setDragEnabled(true); table.setDragEnabled(true); list.setDragEnabled(true); fileChooser.setDragEnabled(true); colorChooser.setDragEnabled(true); textField.setDragEnabled(true); add(tabbedPane, BorderLayout.NORTH); add(scrollPane, BorderLayout.CENTER); add(textField, BorderLayout.SOUTH); pack(); }
/** * Handle action events coming from the buttons. * * @param evt, the associated ActionEvent. */ public void actionPerformed(ActionEvent evt) { Object source = evt.getSource(); if (source instanceof JButton) { if (source == pbSave) { chosen = tcc.getColor(); onCancel(); } else { onCancel(); } } }
public ColorChooserDemo() { super(new BorderLayout()); // 设置一个标签,做广告的。也用来显示用户选择的颜色。 banner = new JLabel("欢迎使用颜色选择器!", JLabel.CENTER); banner.setForeground(Color.yellow); banner.setBackground(Color.blue); banner.setOpaque(true); banner.setFont(new Font("SansSerif", Font.BOLD, 24)); banner.setPreferredSize(new Dimension(100, 65)); JPanel bannerPanel = new JPanel(new BorderLayout()); bannerPanel.add(banner, BorderLayout.CENTER); bannerPanel.setBorder(BorderFactory.createTitledBorder("广告")); // 设置选择颜色选择器 tcc = new JColorChooser(banner.getForeground()); // 设置初始颜色 tcc.getSelectionModel().addChangeListener(this); // 给所有模式添加监听 tcc.setBorder(BorderFactory.createTitledBorder("选择颜色")); add(bannerPanel, BorderLayout.CENTER); add(tcc, BorderLayout.PAGE_END); }
/** * <br> * 方法说明:事件监听。用户选择颜色触发 <br> * 输入参数:ChangeEvent e 用户选择事件 <br> * 返回类型: */ public void stateChanged(ChangeEvent e) { Color newColor = tcc.getColor(); // 获取用户选择的颜色 banner.setForeground(newColor); }
/** Set the colour the user has chosen. */ public void setColour(Color color) { tcc.setColor(color); }