@Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub Object source = e.getSource(); if (source == ok) { response = APPLY_OPTION; this.setVisible(false); } else if (source == cancel) { response = CANCEL_OPTION; this.setVisible(false); } else if (source == sizeCombo) { // get the number from the source JComboBox number = (JComboBox) source; String numberItem = (String) number.getSelectedItem(); Font temp = example.getFont(); // then set the font int newSize = Integer.parseInt(numberItem); example.setFont(new Font(temp.getFamily(), temp.getStyle(), newSize)); } else if (source == fontCombo) { JComboBox font = (JComboBox) source; String s = (String) font.getSelectedItem(); Font tmp = example.getFont(); example.setFont(new Font(s, tmp.getStyle(), tmp.getSize())); } else if (source == foreground) { Color tmp = JColorChooser.showDialog(this, "Choose text color", example.getForeground()); MenuBar.shapeLBG.setBackground(tmp); if (tmp != null) example.setForeground(tmp); } }
public void actionPerformed(ActionEvent e) { Color color = JColorChooser.showDialog(InnereKlassemitmainMethode.this, "Eine Farbe wählen", Color.red); if (color != null) { g.setColor(color); status.setText("Farbe mit JColorChooser abgeändert"); } }
private static void createAndShowGUI() { JFrame frame = new JFrame(Test6827032.class.getName()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); cc = new JColorChooser(); cc.setDragEnabled(true); frame.add(cc); frame.pack(); frame.setVisible(true); }
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(); } } }
@Override protected void fireActionPerformed(ActionEvent event) { try { this.setSelected(true); // create the dialog if this has not been done yet. if (this.dialog == null) { // Get the top level ancestor as parent for the new dialog. // This ensures that on Mac OS X the global menu bar remains // visible while the dialog is open. final Container parent = this.getTopLevelAncestor(); dialog = JColorChooser.createDialog( parent, colorChooserTitle, true, // modal colorChooser, this, // OK button handler null); // no CANCEL button handler dialog.pack(); dialog.setResizable(false); } // show the dialog colorChooser.setColor(color); dialog.setVisible(true); /* A simpler option that is displaying the ugly preview panel: Color newColor = JColorChooser.showDialog(this.getTopLevelAncestor(), colorChooserTitle, color); if (newColor != null){ // only set color and fire an event when user did not cancel. this.setColor(newColor); super.fireActionPerformed(event); }*/ } catch (Exception e) { } finally { this.setSelected(false); } }
public void mouseReleased(MouseEvent e) { if (mMouseOverButton && mPressedButton != -1) { boolean needsUpdate = false; if (mColorListMode == VisualizationColor.cColorListModeCategories) { Color newColor = JColorChooser.showDialog( mOwner, "Select category color", mCategoryColorList[mPressedButton]); if (newColor != null) { mCategoryColorList[mPressedButton] = newColor; needsUpdate = true; } } else { if (mPressedButton == cColorWedgeButton) { if (++mColorListMode > VisualizationColor.cColorListModeStraight) mColorListMode = VisualizationColor.cColorListModeHSBShort; updateColorList(-1); needsUpdate = true; } else { int index = mPressedButton * (mWedgeColorList.length - 1); Color newColor = JColorChooser.showDialog( mOwner, "Select color for min/max value", mWedgeColorList[index]); if (newColor != null) { mWedgeColorList[index] = newColor; updateColorList(-1); needsUpdate = true; } } } mPressedButton = -1; mMouseOverButton = false; if (needsUpdate) { mListener.actionPerformed( new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "colorChanged")); repaint(); } } }
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); }
static boolean customThemeDialog(Component parent) { Color customColor = JColorChooser.showDialog( parent, Locale.explain("ui.chooseBase"), // $NON-NLS-1$ ShadeConfig.theme.get().getBase()); if (customColor != null) { CUSTOM.getLookAndFeel().setBase(customColor); return true; } return false; }
/** * Display a dialog which can be used to select a color * * @param dialogTitle * @param defaultColor The currently selected color * @return The color the user selected, or null if none/cancelled */ public static Color showColorChooserDialog(String dialogTitle, Color defaultColor) { Color color = null; JColorChooser chooser = new JColorChooser(); chooser.setColor(defaultColor); while (true) { int response = JOptionPane.showConfirmDialog( IGV.getMainFrame(), chooser, dialogTitle, JOptionPane.OK_CANCEL_OPTION); if ((response == JOptionPane.CANCEL_OPTION) || (response == JOptionPane.CLOSED_OPTION)) { return null; } color = chooser.getColor(); if (color == null) { continue; } else { break; } } return color; }
public void actionPerformed(ActionEvent e) { if (e.getSource() == colorBorderButton) { Color chosenColor = JColorChooser.showDialog( colorBorderButton, messageBundle.getString("viewer.utilityPane.annotation.ink.colorBorderChooserTitle"), colorBorderButton.getBackground()); if (chosenColor != null) { // change the colour of the button background colorBorderButton.setBackground(chosenColor); annotation.setColor(chosenColor); // save the action state back to the document structure. updateCurrentAnnotation(); currentAnnotationComponent.resetAppearanceShapes(); currentAnnotationComponent.repaint(); } } }
/** Methode appelee quand l'utilisateur appuie sur le bouton. */ public void actionPerformed(ActionEvent e) { if (e.getSource() == button) { Color choosedColor = JColorChooser.showDialog(parent.dialog(), "Choose color", new Color(R, G, B)); if (choosedColor != null) { this.R = choosedColor.getRed(); this.G = choosedColor.getGreen(); this.B = choosedColor.getBlue(); parent.elementModified(); } String tmp = new String( label + Integer.toString(getRed()) + ", " + Integer.toString(getGreen()) + ", " + Integer.toString(getBlue())); while (tmp.length() < 38) { tmp = tmp + " "; } jlabel.setText(tmp); } }
/** Set the colour the user has chosen. */ public void setColour(Color color) { tcc.setColor(color); }
private void Font_Color_buttonActionPerformed( java.awt.event.ActionEvent evt) { // GEN-FIRST:event_Font_Color_buttonActionPerformed Color initialcolor = Color.BLACK; font_color = JColorChooser.showDialog(this, "Select a color", initialcolor); DisplayArea_text.setForeground(font_color); } // GEN-LAST:event_Font_Color_buttonActionPerformed
private void color_buttonActionPerformed( java.awt.event.ActionEvent evt) { // GEN-FIRST:event_color_buttonActionPerformed Color initialcolor = Color.WHITE; bg_color = JColorChooser.showDialog(this, "Select a color", initialcolor); DisplayArea_text.setBackground(bg_color); } // GEN-LAST:event_color_buttonActionPerformed
@Override public void actionPerformed(ActionEvent evt) { String command = evt.getActionCommand(); switch (command) { case "New File": { OSC = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB); Graphics osg = OSC.getGraphics(); osg.setColor(fillColor); osg.fillRect(0, 0, getWidth(), getHeight()); osg.dispose(); repaint(); break; } case "Open": { JFileChooser chooser = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG or PNG Images", "jpg", "png"); chooser.setFileFilter(filter); int returnVal = chooser.showOpenDialog(null); if (returnVal == JFileChooser.APPROVE_OPTION) { try { OSC = ImageIO.read(chooser.getSelectedFile()); Graphics2D g2 = (Graphics2D) OSC.getGraphics(); g2.scale((double) 640 / OSC.getWidth(), (double) 480 / OSC.getHeight()); g2.drawImage(OSC, 0, 0, OSC.getWidth(), OSC.getHeight(), null); } catch (Exception e) { } } repaint(); break; } case "Save": { JFileChooser fileDialog = new JFileChooser(); fileDialog.setSelectedFile(new File("image.PNG")); fileDialog.setDialogTitle("Select File to be Saved"); int option = fileDialog.showSaveDialog(null); if (option != JFileChooser.APPROVE_OPTION) return; // User canceled or clicked the dialog’s close box. File selectedFile = fileDialog.getSelectedFile(); if (selectedFile.exists()) { // Ask the user whether to replace the file. int response = JOptionPane.showConfirmDialog( null, "The file \"" + selectedFile.getName() + "\" already exists.\nDo you want to replace it?", "Confirm Save", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE); if (response != JOptionPane.YES_OPTION) return; // User does not want to replace the file. } try { boolean hasFormat = ImageIO.write(OSC, "PNG", selectedFile); if (!hasFormat) throw new Exception("PNG" + " format is not available."); } catch (Exception e) { JOptionPane.showMessageDialog( null, "Sorry, an error occurred while trying to save image."); e.printStackTrace(); } break; } case "Exit": { System.exit(0); } case "Select Drawing Color...": { Color newColor = JColorChooser.showDialog(AdvancedGUIEX1.this, "Select Drawing Color", currentColor); if (newColor != null) currentColor = newColor; break; } case "Fill With Color...": { Color newColor = JColorChooser.showDialog(AdvancedGUIEX1.this, "Select Fill Color", fillColor); if (newColor != null) { fillColor = newColor; Graphics osg = OSC.getGraphics(); osg.setColor(fillColor); osg.fillRect(0, 0, OSC.getWidth(), OSC.getHeight()); osg.dispose(); AdvancedGUIEX1.this.repaint(); } break; } case "Draw With Black": currentColor = Color.BLACK; break; case "Draw With White": currentColor = Color.WHITE; break; case "Draw With Red": currentColor = Color.RED; break; case "Draw With Green": currentColor = Color.GREEN; break; case "Draw With Blue": currentColor = Color.BLUE; break; case "Draw With Yellow": currentColor = Color.YELLOW; break; case "Curve": currentTool = Tool.CURVE; break; case "Line": currentTool = Tool.LINE; break; case "Rectangle": currentTool = Tool.RECT; break; case "Oval": currentTool = Tool.OVAL; break; case "Filled Rectangle": currentTool = Tool.FILLED_RECT; break; case "Filled Oval": currentTool = Tool.FILLED_OVAL; break; case "Smudge": currentTool = Tool.SMUDGE; break; case "Erase": currentTool = Tool.ERASE; break; } }
/** * <br> * 方法说明:事件监听。用户选择颜色触发 <br> * 输入参数:ChangeEvent e 用户选择事件 <br> * 返回类型: */ public void stateChanged(ChangeEvent e) { Color newColor = tcc.getColor(); // 获取用户选择的颜色 banner.setForeground(newColor); }
public void actionPerformed(ActionEvent e) { color = chooser.getColor(); }
public void actionPerformed(ActionEvent e) { System.out.println("actionPerformed"); if (e.getSource() == pen) // 画笔 { System.out.println("pen"); toolFlag = 0; } if (e.getSource() == eraser) // 橡皮 { System.out.println("eraser"); toolFlag = 1; } if (e.getSource() == clear) // 清除 { System.out.println("clear"); toolFlag = 2; paintInfo.removeAllElements(); repaint(); } if (e.getSource() == drLine) // 画线 { System.out.println("drLine"); toolFlag = 3; } if (e.getSource() == drCircle) // 画圆 { System.out.println("drCircle"); toolFlag = 4; } if (e.getSource() == drRect) // 画矩形 { System.out.println("drRect"); toolFlag = 5; } if (e.getSource() == colchooser) // 调色板 { System.out.println("colchooser"); Color newColor = JColorChooser.showDialog(this, "我的调色板", c); c = newColor; } if (e.getSource() == openPic) // 打开图画 { openPicture.setVisible(true); if (openPicture.getFile() != null) { int tempflag; tempflag = toolFlag; toolFlag = 2; repaint(); try { paintInfo.removeAllElements(); File filein = new File(openPicture.getDirectory(), openPicture.getFile()); picIn = new FileInputStream(filein); VIn = new ObjectInputStream(picIn); paintInfo = (Vector) VIn.readObject(); VIn.close(); repaint(); toolFlag = tempflag; } catch (ClassNotFoundException IOe2) { repaint(); toolFlag = tempflag; System.out.println("can not read object"); } catch (IOException IOe) { repaint(); toolFlag = tempflag; System.out.println("can not read file"); } } } if (e.getSource() == savePic) // 保存图画 { savePicture.setVisible(true); try { File fileout = new File(savePicture.getDirectory(), savePicture.getFile()); picOut = new FileOutputStream(fileout); VOut = new ObjectOutputStream(picOut); VOut.writeObject(paintInfo); VOut.close(); } catch (IOException IOe) { System.out.println("can not write object"); } } }
/** Action listener for the OK button in the color chooser. */ public void actionPerformed(ActionEvent e) { // ok button in color chooser was pressed. this.setColor(colorChooser.getColor()); super.fireActionPerformed(e); }
public DefaultDnDSupport() { JFrame fen = new JFrame("DnD et CCP"); fen.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER)); fen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Panel gauche JPanel panelgauche = new JPanel(); panelgauche.setLayout(new BoxLayout(panelgauche, BoxLayout.Y_AXIS)); // JTextField JTextField textField = new JTextField(10); textField.setBorder( BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder("JTextField"), BorderFactory.createEmptyBorder(5, 5, 5, 5))); textField.setDragEnabled(true); // JPasswordField JPasswordField passwordField = new JPasswordField(10); passwordField.setBorder( BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder("JPasswordField"), BorderFactory.createEmptyBorder(5, 5, 5, 5))); passwordField.setDragEnabled(true); // JFormattedTextField JFormattedTextField ftf = new JFormattedTextField("Universite de Lille 1"); ftf.setFont(new Font("Courier", Font.ITALIC, 12)); ftf.setBorder( BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder("JFormattedTextField"), BorderFactory.createEmptyBorder(5, 5, 5, 5))); ftf.setDragEnabled(true); // JTextArea JTextArea jta = new JTextArea("Master 1 informatique"); jta.setFont(new Font("Arial", Font.BOLD, 12)); jta.setBorder( BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder("JTextArea"), BorderFactory.createEmptyBorder(5, 5, 5, 5))); jta.setDragEnabled(true); // JEditorPane JEditorPane editorPane = createEditorPane(); editorPane.setDragEnabled(true); JScrollPane editorScrollPane = new JScrollPane(editorPane); editorScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); editorScrollPane.setBorder( BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder("JEditorPane"), BorderFactory.createEmptyBorder(5, 5, 5, 5))); // JColorChooser JColorChooser cc = new JColorChooser(); cc.setDragEnabled(true); cc.setBorder( BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder("JColorChooser"), BorderFactory.createEmptyBorder(5, 5, 5, 5))); // Panel droit JPanel paneldroit = new JPanel(); paneldroit.setLayout(new BoxLayout(paneldroit, BoxLayout.Y_AXIS)); // JList String[] data = { "AAC", "AEV", "ANG", "ASE", "COA", "PJE", "CAR", "PJI", "AeA", "BDA", "CALP", "FDD", "HECI", "IHM", "M3DS", "PAC", "PPD", "RdF", "SVL", "TI" }; JList liste = new JList(data); JScrollPane jscrollListe = new JScrollPane(liste); jscrollListe.setBorder( BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder("JList"), BorderFactory.createEmptyBorder(5, 5, 5, 5))); // liste.setDragEnabled(true); // JTree DefaultMutableTreeNode m1 = new DefaultMutableTreeNode("M1"); DefaultMutableTreeNode s1 = new DefaultMutableTreeNode("S1"); m1.add(s1); s1.add(new DefaultMutableTreeNode("AAC")); s1.add(new DefaultMutableTreeNode("AEV")); s1.add(new DefaultMutableTreeNode("ANG")); s1.add(new DefaultMutableTreeNode("ASE")); s1.add(new DefaultMutableTreeNode("COA")); s1.add(new DefaultMutableTreeNode("PJE")); DefaultMutableTreeNode s2 = new DefaultMutableTreeNode("S2"); m1.add(s2); s2.add(new DefaultMutableTreeNode("CAR")); s2.add(new DefaultMutableTreeNode("PJI")); s2.add(new DefaultMutableTreeNode("AeA")); s2.add(new DefaultMutableTreeNode("BDA")); s2.add(new DefaultMutableTreeNode("CALP")); s2.add(new DefaultMutableTreeNode("FDD")); s2.add(new DefaultMutableTreeNode("HECI")); s2.add(new DefaultMutableTreeNode("IHM")); s2.add(new DefaultMutableTreeNode("M3DS")); s2.add(new DefaultMutableTreeNode("PAC")); s2.add(new DefaultMutableTreeNode("PPD")); s2.add(new DefaultMutableTreeNode("RdF")); s2.add(new DefaultMutableTreeNode("SVL")); s2.add(new DefaultMutableTreeNode("TI")); JTree tree = new JTree(m1); // tree.setDragEnabled(true); JScrollPane jscrollTree = new JScrollPane(tree); jscrollTree.setBorder( BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder("JTree"), BorderFactory.createEmptyBorder(5, 5, 5, 5))); jscrollTree.setPreferredSize(new Dimension(200, 200)); // JTable String[] columnNames = {"S1", "S2"}; Object[][] data2 = { {"AAC", "CAR"}, {"AEV", "PJI"}, {"ANG", "AeA"}, {"ASE", "BDA"}, {"COA", "CALP"}, {"PJE", "FDD"}, {"", "HECI"}, {"", "IHM"}, {"", "M3DS"}, {"", "PAC"}, {"", "PPD"}, {"", "RdF"}, {"", "SVL"}, {"", "TI"} }; JTable table = new JTable(data2, columnNames); JScrollPane scrollPaneTable = new JScrollPane(table); table.setDragEnabled(true); // JFileChooser JFileChooser fc = new JFileChooser(); fc.setBorder( BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder("JFileChooser"), BorderFactory.createEmptyBorder(5, 5, 5, 5))); fc.setDragEnabled(true); panelgauche.add(textField); panelgauche.add(passwordField); panelgauche.add(ftf); panelgauche.add(jta); panelgauche.add(cc); panelgauche.add(editorScrollPane); fen.getContentPane().add(panelgauche); paneldroit.add(fc); paneldroit.add(jscrollListe); paneldroit.add(jscrollTree); paneldroit.add(scrollPaneTable); fen.getContentPane().add(paneldroit); fen.pack(); fen.setVisible(true); }