将JColorChooser显示为帧并将颜色值传递给按钮

时间:2013-05-13 15:55:21

标签: jcolorchooser

大家好,提前致谢。

我需要JColorChooser在我的窗口中显示为顶部面板。我在其位置使用了一个按钮用于格式化。(参见图片)我还需要将colorchooser中选择的颜色值传递给数组中选定的按钮。我已经完成了大部分结构,但我仍然是Java的新手。

**我还想注意,在colorchooser对话框中我不需要'ok'或'cancel'按钮,因为一旦你选择了一种颜色,你应该可以按下你希望改变它们的多个按钮。颜色选择颜色。

View an image of my program

package main;


public class Main {
public static JPanel ToolPanel = new JPanel();  
public static JPanel GridPanel = new JPanel();// Define the Panel that Holds 256 Button Array
public static JPanel FullPanel  = new JPanel(); 
public static JButton button[] = new JButton[256];// Define button array for 256 buttons
private static JColorChooser colorChooser;  
public static JButton ColorChooserButton = new JButton("This is where the JColorChooser(colorChooser) should go. ");
public static JFrame MaineWindow = new JFrame("Kola Color Crapper");
final static String ToolPanelString = "Tool Panel";
final static String GridPanelString = "Grid Panel";


public static  ActionListener ButtonArrayActionListener = new ActionListener() {
    public void actionPerformed(ActionEvent aef) {
      if (aef.getSource() instanceof JButton) {
            ((JButton) aef.getSource()).setBackground(Color.blue);}}};

            public static  ActionListener ColorChooserButtonActionListener = new ActionListener() {
              public void actionPerformed(ActionEvent aeef) {

                  String st="Welcome";
                JOptionPane.showMessageDialog(null,st);

              }};




    public static void main( String[] args ){//Main (Run)
        // setup the gui with buttons
        CreateFullPanel();
        CreateMaineWindow();
}
//CreateToolPanel() is just used to test formatting
public static void CreateToolPanel(){
     ToolPanel.setBorder(BorderFactory.createLineBorder(Color.blue, 2));
 ToolPanel.setLayout(new GridLayout(1,0));
     ColorChooserButton.addActionListener(ColorChooserButtonActionListener);
     ToolPanel.add(ColorChooserButton);
      ColorChooserButton.setToolTipText("This is a tool tip.");
}

    public static void CreateFullPanel(){
CreateToolPanel();
CreateGridPanel();
//I want to insert CreateColorChooserPanel() where CreateToolPanel() is
//  CreateColorChooserPanel();

}


public static void CreateGridPanel() {
    GridPanel.setBorder(BorderFactory.createLineBorder(Color.blue, 2));
    GridPanel.setLayout(new GridLayout(16, 16, 0, 0));
    CreateButtonArray();// Create array of buttons

}

public static void CreateButtonArray(){
  for (int i = 0; i < button.length; i++) {
    button[i] = new JButton(Integer.toString(i + 1));
    button[i].addActionListener(ButtonArrayActionListener);
    GridPanel.add(button[i]);
    GridPanel.setToolTipText("ToolTIp does not work");
}
}

public static void CreateMaineWindow(){
    MaineWindow.setLayout(new GridLayout(2,1));//Main Window is resizable
    MaineWindow.add(ToolPanel, ToolPanelString);
    MaineWindow.add(GridPanel, ToolPanelString);
    MaineWindow.setBounds(300,300,600,400);
    MaineWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    MaineWindow.pack();// pack all the contents of Main Window
    MaineWindow.setVisible(true);//Show Main Window
    }

public static void CreateColorChooserPanel(){

}

}

0 个答案:

没有答案