M
michi2
Gast
Ich breuchte eine Möglichkeit wie der Benuzer meines Programms (Swing; java 1.4) 1 von 6 vordefinirten Farben bzw. 1 von 6 Symbolen (icons) einfach auswählen kann.
Danke!
Danke!
/*
* Vier_AusSechsFarben.java
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Vier_AusSechsFarben extends JFrame {
public Vier_AusSechsFarben() {
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(400,300);
setLocationRelativeTo(null);
//JComponents erzeugen:
toolbar = new JToolBar();
paletteBtn = new JButton("Farben wählen");
paletteBtn2 = new JButton("Symbole wählen");
mainPanel = new JPanel();
//Layout:
toolbar.setFloatable(false);
toolbar.add(paletteBtn);
toolbar.add(paletteBtn2);
getContentPane().add(toolbar, BorderLayout.NORTH);
getContentPane().add(mainPanel,BorderLayout.CENTER);
//Listeners:
paletteBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
paletteBtnActionPerformed(evt);
}
});
paletteBtn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
paletteBtn2ActionPerformed(evt);
}
});
}
private void paletteBtnActionPerformed(java.awt.event.ActionEvent evt) {
chooser = new ColorChooser(this,true,false);
Color[] colors = chooser.getSelectedColors();
mainPanel.removeAll();
for (int i = 0; i < colors.length; i++) {
JButton btn = new JButton();
btn.setBackground(colors[i]);
mainPanel.add(btn);
}
validate();
repaint();
}
private void paletteBtn2ActionPerformed(java.awt.event.ActionEvent evt) {
chooser = new ColorChooser(this,true,true);
Icon[] icons = chooser.getSelectedIcons();
mainPanel.removeAll();
for (int i = 0; i < icons.length; i++) {
JButton btn = new JButton();
btn.setIcon(icons[i]);
mainPanel.add(btn);
}
validate();
repaint();
}
public static void main(String args[]) { new Vier_AusSechsFarben().setVisible(true); }
private ColorChooser chooser;
private JButton paletteBtn,paletteBtn2;
private JToolBar toolbar;
private JPanel mainPanel ;
}
class ColorChooser extends JDialog implements ActionListener{
public ColorChooser(Frame parent, boolean modal, boolean isIconChooser) {
super(parent, modal);
this.isIconChooser=isIconChooser;
setSize(380,170);
setLocationRelativeTo(parent);
setUndecorated(true);
//JComponents erzeugen:
colorChooser = new JPanel();
chooser = new JPanel();
preview = new JPanel();
preview1 = new JButton();
preview2 = new JButton();
preview3 = new JButton();
preview4 = new JButton();
okB = new JButton("OK");
//Layout:
chooser.setLayout(new GridLayout(6, 1, 1, 1));
chooser.setPreferredSize(new Dimension(50, 120));
preview.setPreferredSize(new Dimension(240, 40));
preview.add(preview1);
preview.add(preview2);
preview.add(preview3);
preview.add(preview4);
if(!isIconChooser){
initColors();
for( int i =0; i<colors.length; i++ ){
addButton(colors[i]);
}
}else{
initIcons();
for( int i =0; i<icons.length; i++ ){
addButton(icons[i]);
}
}
colorChooser.add(chooser);
colorChooser.add(preview);
colorChooser.add(okB);
getContentPane().add(colorChooser, BorderLayout.CENTER);
//Listener:
okB.addActionListener(this);
okB.setEnabled(false);
setVisible(true);
}
private JButton addButton(Object obj) {
JButton button = new JButton();
if(!isIconChooser){
button.setBackground((Color) obj);
}else{
button.setIcon((Icon) obj);
}
button.setRolloverEnabled(false);
chooser.add(button);
button.addActionListener(this);
return(button);
}
public void actionPerformed(ActionEvent e) {
if( e.getSource() == okB ){
setVisible(false);
}else{
if(!isIconChooser){
JButton button = (JButton)e.getSource();
Color bg = button.getBackground();
if(preview1.getBackground().equals(DEFAULT_COLOR)) preview1.setBackground(bg);
else if(preview2.getBackground().equals(DEFAULT_COLOR)) preview2.setBackground(bg);
else if(preview3.getBackground().equals(DEFAULT_COLOR)) preview3.setBackground(bg);
else if(preview4.getBackground().equals(DEFAULT_COLOR)) {
preview4.setBackground(bg);
okB.setEnabled(true);
} else{
preview1.setBackground(bg);
preview2.setBackground(DEFAULT_COLOR);
preview3.setBackground(DEFAULT_COLOR);
preview4.setBackground(DEFAULT_COLOR);
okB.setEnabled(false);
}
}else{
JButton button = (JButton)e.getSource();
Icon bg = button.getIcon();
if(preview1.getIcon()==null) preview1.setIcon(bg);
else if(preview2.getIcon()==null) preview2.setIcon(bg);
else if(preview3.getIcon()==null) preview3.setIcon(bg);
else if(preview4.getIcon()==null) {
preview4.setIcon(bg);
okB.setEnabled(true);
} else{
preview1.setIcon(bg);
preview2.setIcon(null);
preview3.setIcon(null);
preview4.setIcon(null);
okB.setEnabled(false);
}
}
}
}
public Color[] getSelectedColors(){
Color[] selectedColors = new Color[4];
selectedColors[0] = preview1.getBackground();
selectedColors[1] = preview2.getBackground();
selectedColors[2] = preview3.getBackground();
selectedColors[3] = preview4.getBackground();
return selectedColors;
}
public Icon[] getSelectedIcons(){
Icon[] selectedIcons = new Icon[4];
selectedIcons[0] = preview1.getIcon();
selectedIcons[1] = preview2.getIcon();
selectedIcons[2] = preview3.getIcon();
selectedIcons[3] = preview4.getIcon();
return selectedIcons;
}
private void initColors() {
int numColors = colorValues.length / 3;
colors = new Color[numColors];
for (int i = 0; i < numColors ; i++) {
colors[i] = new Color( colorValues[(i*3)], colorValues[(i*3)+1], colorValues[(i*3)+2] );
}
}
private void initIcons() {
int numIcons = iconValues.length;
icons = new Icon[numIcons];
for (int i = 0; i < numIcons; i++) {
icons[i] = new ImageIcon( iconValues[i] );
}
}
private final Color DEFAULT_COLOR = (new JButton()).getBackground();
private JPanel colorChooser, preview;
private JPanel chooser;
private JButton preview1, preview2, preview3, preview4, okB;
private Color[] colors;
private boolean isIconChooser;
private int[] colorValues = {
255, 255, 255,
0, 255, 255,
0, 0, 255,
255, 0 , 0,
0, 255, 0,
0, 0, 0,
};
private Icon[] icons;
private String[] iconValues = {
"toolbarButtonGraphics\\general\\About16.gif",
"toolbarButtonGraphics\\general\\Add16.gif",
"toolbarButtonGraphics\\general\\AlignBottom16.gif",
"toolbarButtonGraphics\\general\\Bookmarks16.gif",
"toolbarButtonGraphics\\general\\ComposeMail16.gif",
"toolbarButtonGraphics\\general\\ContextualHelp16.gif",
};
}
michi2 hat gesagt.:Hobbit_Im_Blutrausch: Ja, aber mit button brauch ich dann 4*6Buttons und besonders schön ist das nicht.