2013-12-13 30 views
0

我嘗試了來自不同網站的代碼和來自這裏的關於顏色的代碼。如何讓顏色選擇器與印刷機上的印刷機配合使用?ColorChooser通過點擊JMenu項目

我看着ColorChooser ExampleOracle Color Chooser Example,然後使用下面的代碼執行到原班:

JMenuItem clr = new JMenuItem("Font Color"); 
    clr.addActionListener(new ActionListener(){ 

     public void actionPerformed(ActionEvent e) 
     { 
      ColorChooserDemo ccd = new ColorChooserDemo(); 
      ccd.setVisible(true); 
     } 
    }); 

但這似乎做什麼也不做,當我按下菜單項。

類代碼來自oracle網頁。這些是我使用的以下課程(當然縮短了當前的問題)。我正在編寫一個記事本程序,因爲我回到了編程中,並刷新了我如何在java中執行操作的記憶。問題的另一方面是,我無法得到顏色選擇來了,當我在JMenuItem的CLR點擊(這是字體顏色)下面的代碼顯示是我到目前爲止有:

顏色選擇類別:

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

/* ColorChooserDemo.java requires no other files. */ 
@SuppressWarnings("serial") 
public class ColorChooserDemo extends JPanel 
           implements ChangeListener { 

    protected JColorChooser tcc; 
    protected JLabel banner; 

    public ColorChooserDemo() { 
     super(new BorderLayout()); 

     //Set up color chooser for setting text color 
     tcc = new JColorChooser(); 
     tcc.getSelectionModel().addChangeListener(this); 
     tcc.setBorder(BorderFactory.createTitledBorder(
              "Choose Text Color")); 

     add(tcc, BorderLayout.PAGE_END); 
    } 

    public void stateChanged(ChangeEvent e) { 
     Color newColor = tcc.getColor(); 
     FirstWindow.ta1.setForeground(newColor); 
    } 

    /** 
    * Create the GUI and show it. For thread safety, 
    * this method should be invoked from the 
    * event-dispatching thread. 
    */ 
    private static void createAndShowGUI() { 
     //Create and set up the window. 
     JFrame frame = new JFrame("ColorChooserDemo"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     //Create and set up the content pane. 
     JComponent newContentPane = new ColorChooserDemo(); 
     newContentPane.setOpaque(true); //content panes must be opaque 
     frame.setContentPane(newContentPane); 

     //Display the window. 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     //Schedule a job for the event-dispatching thread: 
     //creating and showing this application's GUI. 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGUI(); 
      } 
     }); 
    } 
} 

主類:

import java.awt.EventQueue;

public class Main{ 
    protected static Object fw; 

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

      @Override 
      public void run() { 
       // TODO Auto-generated method stub 
       try 
       { 
        FirstWindow fw = new FirstWindow(); 
        fw.setVisible(true); 
       } catch (Exception e) 
       { 
        e.printStackTrace(); 
       } 

      } 

     }); 
    } 
} 

FirstWindow類:

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


public class FirstWindow extends JFrame { 
    private static final long serialVersionUID = 1L; 
    protected JColorChooser tcc; 
    protected static JTextArea ta1; 

    public FirstWindow() { 
     super("Note Pad"); 

     Font font = new Font("Verdana", Font.BOLD, 12); 

     //Setting the size of the note pad 
     setSize(650, 745); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 

     //Create the MenuBar 
     JMenuBar mb = new JMenuBar(); 
     setJMenuBar(mb); 

     //Create the panel to hold everything in 
     JPanel p = new JPanel(); 

     //Create the Text Area 
     final JTextArea ta1 = new JTextArea(); 
     ta1.setFont(font); 
     ta1.setMargin(new Insets(5,5,5,5)); 
     ta1.setLineWrap(true); 
     ta1.setWrapStyleWord(true); 

     //Create the Scroll Pane to hold the Text Area 
     final JScrollPane sp = new JScrollPane(ta1); 
     sp.setPreferredSize(new Dimension(625,675)); 
     sp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED); 

     //Create the menu's 
     JMenu format = new JMenu("Format"); 

      //Create menu item for picking font color 
     JMenuItem clr = new JMenuItem("Font Color"); 
     clr.addActionListener(new ActionListener(){ 

      public void actionPerformed(ActionEvent e) 
      { 
       ColorChooserDemo ccd = new ColorChooserDemo(); 
       ccd.setVisible(true); 
      } 
     }); 

     //adding the menu items to the file menu tab 

     //adding menu items to the edit tab 

     //adding menu items to the format tab 
     format.add(clr); 

     //adding the menus to the menu bar 
     mb.add(format); 


     //adding the scroll pane to the panel 
     p.add(sp); 
     add(p, BorderLayout.CENTER); 

    } 
} 
+2

爲了更好地幫助越早,張貼[SSCCE ](http://sscce.org/)。 *「我想知道是否可以通過單擊JMenuItem時的動作來實現它。」*當然這是可能的。另外,這個'問題'不包含'?'。 –

回答

0

可能展現ColorChooser最簡單的方法是:

在ColorChooserDemo類,你有方法private static void createAndShowGUI(),你應該申報公開。

然後,替換爲菜單項的ActionListener以下幾點:

clr.addActionListener(new ActionListener(){ 

     public void actionPerformed(ActionEvent e) 
     { 
      ColorChooserDemo.createAndShowGUI(); 
     } 
    }); 

你ColorChooserDemo類擴展JPanel,JFrame的不是。您首先需要一個JFrame,然後添加面板,然後顯示JFrame。這是createAndShowGUI()方法中發生的情況。

編輯: 我知道你只想知道如何在選擇菜單項時顯示ColorChooserDemo。

然而,實際設置的顏色,你可能想用自己的ColorChooserDemo類跳過,並用以下替換的ActionListener代碼:

clr.addActionListener(new ActionListener(){ 

     public void actionPerformed(ActionEvent e) 
     { 
      Color c = JColorChooser.showDialog(ta1, "ColorChooserDemo", null); 
      ta1.setForeground(c); 
     } 
    }); 
+0

我也試過這個,但由於它嘗試在FirstWindow類中設置JTextArea的顏色而導致錯誤。 – Jaybro90

+0

+1謝謝,這工作!接受答案。 – Jaybro90

0

SSCE容易,不僅爲我們提供了一個解決方案,正如Andrew所建議的那樣,但它也可能幫助您理解並理解要做什麼。總之,這裏是按一個JMenuItem後打開一個顏色選擇的一個簡單的例子:

item.addActionListener(new ActionListener() { 
    @Override 
    public void actionPerformed(ActionEvent e) { 
     JColorChooser jColorChooser = new JColorChooser(); 

     JDialog jDialog = new JDialog(); 
     jDialog.setContentPane(jColorChooser); 
     jDialog.pack(); 
     jDialog.setVisible(true); 
    } 
}); 

LE:(對不起,新顏色選取自己)或只使用JColorChooser.showDialog()

+0

我試過這個,並在類和jColorChooser.getSelectionModel()。addChangeListener(listener)的末尾添加了public void stateChanged(ChangeEvent){...};到該ActionListener中,但不會工作。如果沒有使用ActionListener,它會設置顏色,但是隻有當您單擊菜單項兩次後,纔會在單擊JColorChooser內部的顏色時更改顏色。 – Jaybro90