Question:
i face some problem in my output of my java coding?pls help me,thks a lot?
All
2013-01-12 15:23:31 UTC
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class sept2011q2 extends JFrame implements ItemListener
{
private JLabel lb1,lb2,lb3;
private JComboBox cb1,cb2;
private JPanel p1,p2;
public static void main (String [] agrs)
{
sept2011q2 x = new sept2011q2();
x.setSize(300,125);
x.setVisible(true);
x.setTitle("changing thr font style");
x.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public sept2011q2()
{
lb1=new JLabel("Font name");
cb1= new JComboBox(new Object[]{"time roman","verdana","courier new"});
cb1.setFont(new Font("Arial",Font.PLAIN,12));
lb2=new JLabel("Font Size");
cb2 =new JComboBox(new Object[]{"15","20","30"});
lb3=new JLabel ("Good Luck!");
lb3.setForeground(Color.blue);
p1=new JPanel();
p1.setLayout(new GridLayout(1,4));
p1.add(lb1);
p1.add(cb1);
p1.add(lb2);
p1.add(cb2);
p2=new JPanel();
p2.add(lb3);
setLayout(new BorderLayout());
add(p1,BorderLayout.NORTH);
add(p2,BorderLayout.SOUTH);
cb1.addItemListener(this);
cb2.addItemListener(this);
}
public void itemStateChanged(ItemEvent e)
{
if (cb1.getSelectedItem()=="times roman"&&(cb1.getSelectedItem()=="15"))
lb3.setFont(new Font("Times New Roman",Font.PLAIN,15));
else if (cb1.getSelectedItem()=="times roman"&&(cb2.getSelectedItem()=="20"))
lb3.setFont(new Font("Times New Roman",Font.PLAIN,20));
else if (cb1.getSelectedItem()=="times roman"&&(cb2.getSelectedItem()=="30"))
lb3.setFont(new Font("Times New Roman",Font.PLAIN,30));
else if (cb1.getSelectedItem()=="verdana"&&(cb2.getSelectedItem()=="15"))
lb3.setFont(new Font("Verdana",Font.PLAIN,15));
else if (cb1.getSelectedItem()=="verdana"&&(cb2.getSelectedItem()=="20"))
lb3.setFont(new Font("Verdana",Font.PLAIN,20));
else if (cb1.getSelectedItem()=="verdana"&&(cb2.getSelectedItem()=="30"))
lb3.setFont(new Font("Verdana",Font.PLAIN,30));
else if (cb1.getSelectedItem()=="courrier new"&&(cb2.getSelectedItem()=="15"))
lb3.setFont(new Font("Courrier New",Font.PLAIN,15));
else if (cb1.getSelectedItem()=="Courrier New"&&(cb2.getSelectedItem()=="20"))
lb3.setFont(new Font("Courrier New",Font.PLAIN,30));
else if (cb1.getSelectedItem()=="courrier new "&&(cb2.getSelectedItem()=="30"))
lb3.setFont(new Font("Courrier new",Font.PLAIN,30));






}
}


i can get my output but the size of font will change only if i select it, the font type didnt appear as usual. i cant figure out where is my problem
Three answers:
Kaydell
2013-01-14 00:08:13 UTC
I took your code and I got it to work even though Yahoo! Answers cut off several of your lines of code. I suggest that the next time that you post code to Yahoo! Answers that you instead post your code to:



http://pastebin.com



And then paste a link to your code on Yahoo! Answers.



But anyway, i got your code to compile and run correctly.



A key point to understand is that getSelectedItem() is always going to return an Object and we need a String for the font name and an int for the font size. So what I did is type-cast the Object to a String because we put the items into the combo boxes as String objects so we know that they are Strings. For the font name, we just use the String returned by getSelectedItem() For the font size, I call getSelectedItem() and type cast the Object returned to a String and then call parseInt() on the String to convert it to an int which we can pass as the font size to the constructor for the Font class.



Here's all of my code. It works better, it is shorter, and it is more general, supporting more fonts, and more font sizes.



http://ideone.com/CSTFq0
Patrick
2013-01-12 15:56:19 UTC
Im not great at java but do this:



private JComboBox cb1;

private JComboBox cb2;





In the initaliser:



cb1 = new JComboBox(new String[] {"Times New Roman", "Verdana", "Courier New"});

cb2 = new JComboBox(new Integer[] {15, 20, 30});





and the whole item listener method:



public void itemStateChanged(ItemEvent e) {

lb3.setFont(new Font((String) cb1.getSelectedItem(), Font.PLAIN, (Integer) cb2.getSelectedItem()));

}



Also when comparing strings do not use the == operator, use #equals(String) or #equalsIgnoreCase(String)
2016-12-03 01:20:40 UTC
i could particularly get it out in the open and cope with it. My kin has avoidance subject concerns and that i'm the undesirable one for eager to in basic terms cope with it and get it out of my existence. I hate drama.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...