Swing JToogleButton Vordergrundfarbe bei HTML String

lucifer

Mitglied
Hallo an alle.
ich habe folgende Fragen bzgl. setText mit einen HTML String :
Der Hintergrund ist, daß ich möchte, daß der Textsich auch über mehrere Zeilen erstrecken kann.

  1. Wie wird die Farbe bei Button 8 bzw. 9 ermittelt ? Beim Button 8 und 9 habe ich für den Text HTML übergeben und den Button 8 disabled. Mir ist nicht klar, warum hier verschiedene Farben für den Text verwendet werden ?
  2. Wenn ich die Schriftgröße bei den Buttons ändern möchte, müsste ich dann den getFont() && deriveFont(style, size) verwenden ? Bzgl. müsste ich für B8 und B9 den HTML String neu setzen ?




Java:
import java.awt.*;
import javax.swing.*;

public class ButtonWithHTMLText extends JFrame {
    JToggleButton b3 = new JToggleButton("T1");
    JToggleButton b4 = new JToggleButton("T2");

    JToggleButton b8 = new JToggleButton("<html>T3 diabled</html>");
    JToggleButton b9 = new JToggleButton("<html>T4 enabled</html>");

    ButtonWithHTMLText() {
        this.setLayout(new GridBagLayout());
        b4.setBackground(Color.WHITE);
        b4.setEnabled(false);
        b4.setBackground(Color.WHITE);

        b3.setText("Hallo TW1");

        b8.setEnabled(false);

        this.setLayout(new GridBagLayout());
        GridBagConstraints con3 = new GridBagConstraints() ;
        con3.gridx=0;
        con3.gridy = 0 ;
        con3.weightx = 1 ;
        con3.weighty = 1 ;
        con3.fill = GridBagConstraints.BOTH ;
        
        this.setLayout(new GridBagLayout());
        GridBagConstraints con4 = new GridBagConstraints() ;
        con4.gridx=0;
        con4.gridy = 1 ;
        con4.weightx = 1 ;
        con4.weighty = 1 ;
        con4.fill = GridBagConstraints.BOTH ;
        
        
        GridBagConstraints con8 = new GridBagConstraints() ;
        con8.gridx=0;
        con8.gridy = 2 ;
        con8.fill = GridBagConstraints.BOTH ;
        con8.weightx = 1 ;
        con8.weighty = 1 ;
        
        
        GridBagConstraints con9 = new GridBagConstraints() ;
        con9.gridx=0;
        con9.gridy = 3 ;
        con9.weightx = 1 ;
        con9.weighty = 1 ;
        con9.fill = GridBagConstraints.BOTH ;

        this.add(b3, con3);
        this.add(b4, con4);
        this.add(b8, con8);
        this.add(b9, con9);

    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ButtonWithHTMLText tmp = new ButtonWithHTMLText();
        tmp.pack();
        tmp.setVisible(true);
    }

}
 
Look&Feels haben (über den UIManager) verschiedene Farben für aktiven und inaktiven Text. Daher kommen die Farben. Die müsstest du über UIManager.getColor("Button.disabledText") und UIManager.getColor("Button.foreground") herausfinden können.

Die Schriftart kannst du entweder über HTML-Tags oder über die setFont()-Methode setzen.
 
Also der Versuch mittels UIManager Manager hat nicht geklappt bzw. sehe ich nicht, wo der Fehler ist.
Im nachfolgenden Code sollte der Button grün werden, wenn er selektiert wird.
Java:
import java.awt.*;
import java.util.*;
import java.util.Map.Entry;

import javax.swing.*;

public class TestButtonBackground extends JFrame {

    
    TestButtonBackground() {
        JButton btn = new JButton("Hallo") ;
        btn.setBackground(Color.YELLOW) ;
        btn.setForeground(Color.CYAN) ;
        this.setLayout(new GridBagLayout()) ;
        
        JToggleButton btn2 = new JToggleButton("esel");
        JRadioButton btn3 = new JRadioButton("Radio") ;
 
 
    
        Container container = this.getContentPane() ;
        container.setLayout(new GridBagLayout()) ;
        
        
        UIDefaults newDef = UIManager.getDefaults();
        Enumeration e = newDef.keys() ;
        while ( e.hasMoreElements() ) {
            String tmp = e.nextElement().toString() ;
            if (tmp!=null &&
                    (tmp.contains("background") || tmp.contains("select"))
                    && ((tmp.contains("Button") || tmp.contains("RadioButton") || tmp.contains("ToggleButton")) )) {
                System.out.println(tmp );
            }
            

        } 
        newDef.put("Button.select", Color.RED); ;
        newDef.put("ToggleButton.select", Color.GREEN) ;
        newDef.put("RadioButton.select", Color.MAGENTA) ;
        newDef.put("RadioButtonMenuItem.selectionBackground", Color.ORANGE) ;
        newDef.put("ToggleButton.shadow", Color.MAGENTA) ;
        
        
        
        btn2.setBackground(Color.CYAN) ;
        btn3.setBackground(Color.PINK) ;
        
 
        
        GridBagConstraints constraint1 = new GridBagConstraints()  ;
        constraint1.gridx = 0 ;
        constraint1.gridy = 0 ;
        constraint1.gridwidth = 1 ;
        constraint1.gridheight = 1 ;
        constraint1.weightx=1 ;
        constraint1.weighty=1 ;
        constraint1.fill = GridBagConstraints.BOTH ;
        
        
        GridBagConstraints constraint2 = new GridBagConstraints()  ;
        constraint2.gridx = 1 ;
        constraint2.gridy = 0 ;
        constraint2.gridwidth = 1 ;
        constraint2.gridheight = 1 ;
        constraint2.weightx=1 ;
        constraint2.weighty=1 ;
        constraint2.fill = GridBagConstraints.BOTH ;
        
        
        GridBagConstraints constraint3 = new GridBagConstraints()  ;
        constraint3.gridx = 2 ;
        constraint3.gridy = 0 ;
        constraint3.gridwidth = 1 ;
        constraint3.gridheight = 1 ;
        constraint3.weightx=1 ;
        constraint3.weighty=1 ;
        constraint3.fill = GridBagConstraints.BOTH ;
        
        GridBagConstraints constraint4 = new GridBagConstraints()  ;
        constraint4.gridx = 0 ;
        constraint4.gridy = 1 ;
        constraint4.gridwidth = 1 ;
        constraint4.gridheight = 1 ;
        constraint4.weightx=1 ;
        constraint4.weighty=1 ;
        constraint4.fill = GridBagConstraints.BOTH ;
        
        GridBagConstraints constraint5 = new GridBagConstraints()  ;
        constraint5.gridx = 0 ;
        constraint5.gridy = 1 ;
        constraint5.gridwidth = 1 ;
        constraint5.gridheight = 1 ;
        constraint5.weightx=1 ;
        constraint5.weighty=1 ;
        constraint5.fill = GridBagConstraints.BOTH ;
        
        
        
        container.add(btn, constraint1) ;
        container.add(btn2,constraint2) ;
        container.add(btn3, constraint3) ;
 
 
        
        
        
        System.out.println("\n") ;
        
        java.util.List<String> colorKeys = new ArrayList<String>();
        Set<Entry<Object, Object>> entries = UIManager.getLookAndFeelDefaults().entrySet();
        for (Entry entry : entries)
        {
            
            String tmp =  entry.getKey().toString() ;
              if (tmp!=null &&  tmp.contains("Button") || tmp.contains("RadioButton") || tmp.contains("ToggleButton"))  {
                  if (entry.getValue() instanceof Color)
                  {
                    colorKeys.add((String) entry.getKey());
                  }
               }
              }
            
       

       
           
        // sort the color keys
        Collections.sort(colorKeys);
        
        // print the color keys
        System.out.println("Colors:") ;
        for (String colorKey : colorKeys)
        {
          System.out.println(colorKey);
        }
        
        
        
        
        
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        TestButtonBackground tmp = new TestButtonBackground() ;
     
      
        tmp.pack() ;
        tmp.setVisible(true) ;

    }
 

Neue Themen


Zurück
Oben