This site requires JavaScript, please enable it in your browser!
Greenfoot back
riachiyo
riachiyo wrote ...

2015/3/14

Help! JComboBox and JCheckBox

riachiyo riachiyo

2015/3/14

#
Hello! I would like to ask for help. I've been trying to make the jlabel bold and italic at the same time but I don't know how to do it. If I click both of the jcheckboxes, it's either the bold or italic that works. I also need to make a JComboBox which will change the font size and font of the JLabel but I really don't know how to do it. Here is my code so far:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

@SuppressWarnings("unchecked")

public class FinalsLabExam  extends JFrame implements ActionListener, ItemListener{
	
		JToolBar toolbar = new JToolBar();
		
		JButton btnBold  = new JButton(new ImageIcon("bold.jpg"));
		
		JLabel lblMessage = new JLabel("Animo La Salle", SwingConstants.CENTER);
		
		JCheckBox chkBold = new JCheckBox("Bold");
		JCheckBox chkItalic = new JCheckBox("Italic");
		
  		JComboBox cboFont;
  
  		JComboBox cboSize;
  		
 		Font f = new Font("Courier New", Font.PLAIN, 36);
 		Font fTahoma = new Font("Tahoma", Font.PLAIN,36);
 		Font fVerdana =  new Font("Verdana", Font.PLAIN,36);
 		Font fArial = new Font("Arial", Font.PLAIN,36);
 		Font boldItalicFont = new Font("serif", Font.BOLD + Font.ITALIC, 14);
 			
  		JMenuBar menubar = new JMenuBar();
    	JMenu mnuFormat, mnuFont, mnuColor, mnuBGColor, mnuFace, mnuFGColor;
    	JMenuItem mnuTahoma, mnuVerdana, mnuArial, mnuRed, mnuGreen, mnuBlue, mnuYellow, mnuMagenta, mnuOrange;
    	
    	public FinalsLabExam(){
    	cboFont = new JComboBox();
  		cboFont.addItem("Arial");
  		cboFont.addItem("Helvetica");
  		cboFont.addItem("Verdana");
  		cboFont.addItem("Times New Roman");
  		cboFont.addItem("Tahoma");
  		
  		cboSize = new JComboBox();
  		cboSize.addItem("28");
  		cboSize.addItem("34");
  		cboSize.addItem("48");
  		cboSize.addItem("56");
  		cboSize.addItem("72");
    	
    	mnuFormat = new JMenu("Format");
    	mnuFont= new JMenu("Font");
    	mnuColor = new JMenu("Color");
    	mnuBGColor= new JMenu("Background Color");
    	mnuFace= new JMenu("Face");
    	mnuFGColor= new JMenu("Foreground Color");
    	
    	mnuFormat.setMnemonic('f');
    	mnuFont.setMnemonic('f');
    	mnuBGColor.setMnemonic('b');
    	mnuFace.setMnemonic('f');
    	mnuFGColor.setMnemonic('f');
    	mnuColor.setMnemonic('c');
    	
    	mnuTahoma = new JMenuItem("Tahoma", 't');
    	mnuVerdana = new JMenuItem("Verdana", 'v');
    	mnuArial = new JMenuItem("Arial", 'a');
    	
    	mnuRed  = new JMenuItem("Red", 'r');
    	mnuGreen  = new JMenuItem("Green", 'g');
    	mnuBlue  = new JMenuItem("Blue", 'b');
    	
    	mnuYellow = new JMenuItem("Yellow",'y');
    	mnuMagenta = new JMenuItem ("Magenta",'m');
    	mnuOrange = new JMenuItem ("Orange",'o');
    	
    	// ICONS
    	mnuRed.setIcon(new ImageIcon("D://red.png"));
    	mnuGreen.setIcon(new ImageIcon("D://green.png"));
    	mnuBlue.setIcon(new ImageIcon("D://blue.png"));
    	
    	mnuOrange.setIcon(new ImageIcon("D://orange.png"));
    	mnuYellow.setIcon(new ImageIcon("D://yellow.png"));
    	mnuMagenta.setIcon(new ImageIcon("D://m.png"));
    	
    	setJMenuBar(menubar);
    	
    	menubar.add(mnuFormat);
    	
    	mnuFormat.add(mnuFont);
    		mnuFont.add(mnuFace);
    			mnuFace.add(mnuTahoma);
    			mnuFace.add(mnuVerdana);
    			mnuFace.add(mnuArial);
    			
    	mnuFormat.add(mnuColor);
    			mnuColor.add(mnuBGColor);
    				mnuBGColor.add(mnuOrange);
    				mnuBGColor.add(mnuMagenta);
    				mnuBGColor.add(mnuYellow);
    			mnuColor.add(mnuFGColor);	
    				mnuFGColor.add(mnuRed);
    				mnuFGColor.add(mnuGreen);
    				mnuFGColor.add(mnuBlue);
    					
    	
    	//accelerators
    	
    	mnuTahoma.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, 2));
    	mnuVerdana.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, 2));
    	mnuArial.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, 2));
    	  	
    	mnuRed.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, 2));
    	mnuGreen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G, 2));
    	mnuBlue.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B, 2));
    	
    	mnuOrange.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, 2));
    	mnuYellow.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Y, 2));
    	mnuMagenta.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_M, 2));
    	

    	mnuTahoma.addActionListener(this);
    	mnuVerdana.addActionListener(this);
    	mnuArial.addActionListener(this);
    	mnuRed.addActionListener(this);
    	mnuGreen.addActionListener(this);
    	mnuBlue.addActionListener(this);
    	mnuOrange.addActionListener(this);
    	mnuMagenta.addActionListener(this);
    	mnuYellow.addActionListener(this);
    	
   		
   		//LABEL 
   		Container c = getContentPane();
    	c.setLayout(new BorderLayout())		;
    	lblMessage.setBounds(80,100,600,100);
	   	c.add(lblMessage);
    	lblMessage.setFont(f);
    	//and toolbars
    	c.add(toolbar, BorderLayout.NORTH);
    	toolbar.add(chkBold);	
    	toolbar.add(chkItalic);
    	toolbar.add(cboFont);
    	toolbar.add(cboSize);
    	chkBold.addItemListener(this);	
    	chkItalic.addItemListener(this);	
		cboFont.addActionListener(this);
		cboSize.addActionListener(this);
		
    	setDefaultCloseOperation(DISPOSE_ON_CLOSE);
      	setVisible(true);
      	setTitle("Finals Laboratory Exam");
    	setSize(500,400);
    	setLocation(200,200);
    	setResizable(false);
    
   		 }
    
   public void actionPerformed(ActionEvent e){
    
   		if (e.getSource()==mnuTahoma)
   			lblMessage.setFont(fTahoma);
   		else if (e.getSource()== mnuVerdana)
   			lblMessage.setFont(fVerdana);
   		else if (e.getSource()== mnuArial)
   			lblMessage.setFont(fArial);
   		else if (e.getSource()== mnuRed)
   			lblMessage.setForeground(Color.RED);
   		else if (e.getSource()== mnuGreen)
   			lblMessage.setForeground(Color.GREEN);
   		else if (e.getSource()== mnuBlue)
   			lblMessage.setForeground(Color.BLUE);
		else if (e.getSource()== mnuOrange)
   			getContentPane().setBackground(Color.ORANGE);
   		else if (e.getSource()== mnuMagenta)
   			getContentPane().setBackground(Color.MAGENTA);
   		else if (e.getSource()== mnuYellow)
   			getContentPane().setBackground(Color.YELLOW); 	
   				
   		 	
        }
   
   
		public void itemStateChanged (ItemEvent e) {
		if (e.getSource()==chkBold){
		
		if (e.getStateChange() == ItemEvent.SELECTED)
			lblMessage.setFont(lblMessage.getFont().deriveFont(Font.BOLD));
		if (e.getStateChange() == ItemEvent.DESELECTED)
			lblMessage.setFont(lblMessage.getFont().deriveFont(Font.PLAIN));
			
	}
		if (e.getSource() == chkItalic){
			if (e.getStateChange() == ItemEvent.SELECTED)
			lblMessage.setFont(lblMessage.getFont().deriveFont(Font.ITALIC));
		    if (e.getStateChange() == ItemEvent.DESELECTED)
			lblMessage.setFont(lblMessage.getFont().deriveFont(Font.PLAIN));
		}
	
	
	}

  
   public static void main(String[] args) {
        new FinalsLabExam();
    }
}
danpost danpost

2015/3/15

#
This should work. Change the 'itemStateChanged' method to this:
public void itemStateChanged (ItemEvent e) {
    if (e.getSource() == chkBold || e.getSource() == chkItalic)
    {
        int style = (chkBold.isSelected() ? 1 : 0)+(chkItalic.isSelected() ? 2 : 0);
        lblMessage.setFont(lblMessage.getFont().deriveFont(style));
    }
}
Appending the following to your 'actionPerformed' method should solve the rest:
else if (e.getSource()== cboSize)
{
    int size = Integer.parseInt((String)cboSize.getSelectedItem());
    lblMessage.setFont(lblMessage.getFont().deriveFont((float)size));
}
else if (e.getSource()== cboFont)
{
    int size = Integer.parseInt((String)cboSize.getSelectedItem());
    int style = (chkBold.isSelected() ? 1 : 0)+(chkItalic.isSelected() ? 2 : 0);
    Font font = new Font((String)cboFont.getSelectedItem(), style, size);
    lblMessage.setFont(font);
}
You need to login to post a reply.