大家好,提前致谢。
我需要 JColorChooser 在我的窗口中显示为顶部面板。我在其位置使用了一个按钮进行格式化。(见图)我还需要将所选颜色值从颜色选择器传递到数组中的所选按钮。我已经完成了大部分结构,但我对 Java 还是很陌生。
** 我还想指出,我不需要颜色选择器对话框中的“确定”或“取消”按钮,因为一旦您选择了一种颜色,您应该能够按任意数量的按钮将其颜色更改为选中颜色。
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(){
}
}