user1809300
user1809300

Reputation: 723

changing the Frame font and the font of all the components in the Frame

i have the following given code for an exercise and i try to find out why the font in JPanel is different from the Panel font. i noticed that there is a setFont method that changes the font of Frame but the font of the Panel within the Frame also changed but the font in JPanel didnt change,why is that:

import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 
public class MyAppCheck extends Frame { 
public MyAppCheck (String title) { 
    super(title); 
    setFont(new Font("Verdana", Font.BOLD, 12)); 
    setLayout(new GridLayout(1,3)); 
    Panel cboxes = new Panel(); 
    cboxes.setLayout(new GridLayout(0,1)); 
    Color c = new Color(80,120,230); 
    cboxes.setBackground(c); 
    Checkbox c1 = new Checkbox("Windows");  Checkbox c2 = new Checkbox("Linux"); 
    Checkbox c3 = new Checkbox("IRIX"); 
    Checkbox c4 = new Checkbox("MacOS");

    cboxes.add(c1); 
    cboxes.add(c2); 
    cboxes.add(c3); 
    cboxes.add(c4); 
    c2.setState(true); 
    c4.setState(true); 
    JPanel cboxgrp = new JPanel(); 
    cboxgrp.setLayout(new GridLayout(0,1)); 
    cboxgrp.setBackground(Color.YELLOW); 
    cboxgrp.setBorder(BorderFactory.createTitledBorder("Languages")); 
    CheckboxGroup progLang = new CheckboxGroup(); 
    Checkbox cg1 = new Checkbox("Pascal", false, progLang); 
    Checkbox cg2 = new Checkbox("Java", false, progLang); 
    Checkbox cg3 = new Checkbox("Basic", false, progLang); 
    Checkbox cg4 = new Checkbox("C", false, progLang); 
    Checkbox cg5 = new Checkbox("C++", false, progLang);

    cboxgrp.add(cg1); 
    cboxgrp.add(cg2); 
    cboxgrp.add(cg3); 
    cboxgrp.add(cg4); 
    cboxgrp.add(cg5); 
    cg3.setState(true); 
    add(cboxes); 
    add(cboxgrp); 
                    System.out.println("Font_1:"+cboxes.getFont());
                    System.out.println("Font_2:"+cboxgrp.getFont());
    addWindowListener(new WindowAdapter() { 
        public void windowClosing(WindowEvent evt) { 
            System.exit(0); 
        } 
    }); 
} 

public static void main(String[] args){ 
    MyAppCheck app2 = new MyAppCheck("Application Window"); 
    app2.setSize(200, 200); 
    app2.setVisible(true); 

} 
}

sorry for my bad english ...and also just to tell you that i am a newbie in awt/swing

Upvotes: 1

Views: 1029

Answers (1)

tenorsax
tenorsax

Reputation: 21223

java.awt.Panel picks a default system font; javax.swing.JPanel takes is it from UIManager. Note that Mixing Heavyweight and Lightweight Components is a bad idea.

Upvotes: 1

Related Questions