You are on page 1of 5

import javax.swing.*; import java.awt.*; import java.awt.event.

*; public class StyleArrays extends JPanel implements ActionListener { //declare the labels, panels, variables and radio buttons private private private private JLabel saying; JRadioButton style1, style2, style3, style4; JRadioButton size1, size2, size3, size4; JPanel top, right, left;

JRadioButton[] size = new JRadioButton[7]; JRadioButton[] font = new JRadioButton[7]; String[] fonts = {"Arial", "Thonburi", "Rockwell", "Century Gothic"}; int[] sizes = {18, 22, 26, 30}; //declare the variables used later in the code the set the font and style //private String myFont = "Arial"; //private int size = 18; //Constructor //----------------------------------------------------------------public StyleArrays() { //set the layout of the Layouts panel that will contain all of the other panels setLayout (new BorderLayout()); setBackground (Color.red); //create the new panels that will go inside the Layouts panel top= new JPanel(); right= new JPanel(); left= new JPanel(); saying = new JLabel ("Say it with style!"); // saying.setFont (new Font (myFont, Font.PLAIN, size)); //set the layout and color of the top panel, and add the saying add(top, BorderLayout.NORTH); top.setBackground (Color.yellow); top.add(saying);

//create size radio buttons size1 = new JRadioButton ("18", true); size1.setBackground (Color.red);

size2 = new JRadioButton ("22"); size2.setBackground (Color.red); size3 = new JRadioButton ("26"); size3.setBackground (Color.red); size4 = new JRadioButton ("30"); size4.setBackground (Color.red); //add listeners for each size buttons size1.addActionListener size2.addActionListener size3.addActionListener size4.addActionListener (this); (this); (this); (this);

//add these buttons to this button group ButtonGroup group1 = new ButtonGroup(); group1.add (size1); group1.add (size2); group1.add (size3); group1.add (size4);

//set the layout and color of the left panel add(left, BorderLayout.WEST); //add the left panel to the border layout left.setLayout (new BoxLayout (left, BoxLayout.Y_AXIS)); //set the layout of left to box layout left.setBackground (Color.red); //set the color to red //display the buttons on the panel left.add (size1); left.add (size2); left.add (size3); left.add (size4); //This section deals with the panel that contains the STYLE information

add(right, BorderLayout.EAST); //add the right panel to the border layout right.setLayout (new GridLayout (2, 2)); //set the layout of right panel to grid layout right.setBackground (Color.red); // set the background color of the panel to red //create style radio buttons style1 = new JRadioButton ("Arial", true); style1.setBackground (Color.red); style2 = new JRadioButton ("Thonburi"); style2.setBackground (Color.red); style3 = new JRadioButton ("Rockwell");

style3.setBackground (Color.red); style4 = new JRadioButton ("Century Gothic"); style4.setBackground (Color.red);

//add listeners for each style1.addActionListener style2.addActionListener style3.addActionListener style4.addActionListener

style button (this); (this); (this); (this);

//add these buttons to this button group ButtonGroup group2 = new ButtonGroup(); group2.add (style1); group2.add (style2); group2.add (style3); group2.add (style4); //display right.add right.add right.add right.add the buttons on the panel (style1); (style2); (style3); (style4);

} //***************************************************************** // Represents the listener for both check boxes. //*****************************************************************

//***************************************************************** // Represents the listener for the radio buttons. //***************************************************************** public void actionPerformed (ActionEvent event) { Object source = event.getSource(); if (source == size1) //if the event is that the size1 button is selected size = 18; //assign 18 to the variable if (source == size2) size = 22; if (source == size3) size = 26;

if (source == size4) size = 30; if (source == style1) myFont = "Arial"; if (source == style2) myFont = "Thonburi"; if (source == style3) myFont = "Rockwell"; if (source == style4) myFont = "Century Gothic"; saying.setFont (new Font (myFont, Font.PLAIN, size)); //display the saying with the current value of 'myFont' and 'style' } public static void main (String[] args) { JFrame frame = new JFrame ("Style Arrays"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); StyleArrays panel = new StyleArrays(); frame.getContentPane().add (panel); frame.pack(); frame.setVisible(true); } }

Another sample
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. import javax.swing.JFrame; import javax.swing.JRadioButton; import java.awt.FlowLayout; import java.awt.Color; public class ChangeJavaRadioButtonTextColor { public static void main(String[]args) { JRadioButton a=new JRadioButton("I AM A RADIO BUTTON"); JFrame b=new JFrame("CHANGE RADIO BUTTON TEXT COLOR"); b.setLayout(new FlowLayout());

15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25.

//METHOD setForeground will change radio button's text color //You can see more color in Color class on Java API a.setForeground(Color.GREEN); b.add(a); b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); b.setSize(400,400); b.setVisible(true); } }

You might also like