dude you need to change the color green to a image r8??rochel1977 said:then amila can u add that label and send it to me the full code? thx bro i am realy stuck here. what adjustment i do all go wrong. pls machan.
dude you need to change the color green to a image r8??rochel1977 said:then amila can u add that label and send it to me the full code? thx bro i am realy stuck here. what adjustment i do all go wrong. pls machan.
amila325 said:hihi thanx man....
rochel there is another way u will hv to override da paint method in JPanel class inside a class of your own...
after this you can use this class as a customized JPanelCode:class MyPanel extends JPanel { private Image img; public MyPanel(Image img) { this.img = img; //do what ever u need to do in the jpanel layouts and everything } public void paintComponent(Graphics g) { g.drawImage(img, 0, 0, null); } }
amila325 said:dude you need to change the color green to a image r8??
ya im on that this picture wont come up nerochel1977 said:machan what i want is when i click the last row of radio buttons it should display the text with the pictures as well.

amila325 said:ya im on that this picture wont come up ne![]()
![]()

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class QuoteGUI extends JFrame {
public static void main(String arg[]) {
final Image fstImg = new ImageIcon("C:/bold.jpg").getImage();
final Image secImg = new ImageIcon("iamges/italic.jpg").getImage();
final Image thrImg = new ImageIcon("C:/italic.jpg").getImage();
final JLabel quote = new JLabel("Malaysian Boleh!");
final MyPanel pquote = new MyPanel(fstImg);
JPanel pradio = new JPanel();
quote.setFont(new Font("Helvetica", Font.PLAIN, 24));
final ButtonGroup group = new ButtonGroup();
JRadioButton mahatir = new JRadioButton("Dr.Mahatir", true);
JRadioButton paklah = new JRadioButton("Pak Lah", false);
JRadioButton pckang = new JRadioButton("Phua Chu Kang", false);
//pquote.setBackground(Color.green);
pradio.setBackground(Color.yellow);
group.add(mahatir);
group.add(paklah);
group.add(pckang);
pradio.add(mahatir);
pradio.add(paklah);
pradio.add(pckang);
JCheckBox bold = new JCheckBox("BOLD");
JCheckBox italic = new JCheckBox("ITALIC");
class VoteItemListener implements ItemListener {
public void itemStateChanged(ItemEvent ex) {
String item = ((AbstractButton) ex.getItemSelectable()).getActionCommand();
boolean selected = (ex.getStateChange() == ItemEvent.SELECTED);
if (selected) {
if (item.equals("Dr.Mahatir")) {
quote.setText("Malaysian Boleh!");
pquote.setImage(fstImg);
} else if (item.equals("Pak Lah")) {
quote.setText("Work with me: )");
pquote.setImage(secImg);
} else if (item.equals("Phua Chu Kang")) {
quote.setText("Don't play-play!");
pquote.setImage(thrImg);
}
}
}
}
ItemListener itemListener = new ItemListener() {
public void itemStateChanged(ItemEvent itemEvent) {
AbstractButton abstractButton = (AbstractButton) itemEvent.getSource();
int state = itemEvent.getStateChange();
if (state == ItemEvent.SELECTED) {
quote.setFont(quote.getFont().deriveFont(quote.getFont().getStyle() + Font.ITALIC));
} else {
quote.setFont(quote.getFont().deriveFont(Font.PLAIN));
}
}
};
ItemListener itemListenerBold = new ItemListener() {
public void itemStateChanged(ItemEvent itemEvent) {
AbstractButton abstractButton = (AbstractButton) itemEvent.getSource();
int state = itemEvent.getStateChange();
if (state == ItemEvent.SELECTED) {
quote.setFont(quote.getFont().deriveFont(quote.getFont().getStyle() + Font.BOLD));
} else {
quote.setFont(quote.getFont().deriveFont(Font.PLAIN));
}
}
};
bold.addItemListener(itemListenerBold);
italic.addItemListener(itemListener);
pquote.add(quote);
pquote.add(bold);
pquote.add(italic);
ItemListener al = new VoteItemListener();
mahatir.addItemListener(al);
paklah.addItemListener(al);
pckang.addItemListener(al);
JFrame frame = new JFrame("Quote");
frame.setLayout(new BorderLayout());
frame.getContentPane().add(pquote, BorderLayout.CENTER);
frame.add(pradio, BorderLayout.PAGE_END);
frame.setSize(500, 170);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class MyPanel extends JPanel {
private Image img;
private Color c = Color.GREEN;;
public MyPanel(Image img) {
this.img = img;
//do what ever u need to do in the jpanel layouts and everything
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (img != null) {
g.drawImage(img, 0, 0, null);
} else {
setBackground(c);
}
}
public void setImage(Image img){
this.img = img;
repaint();
}
}
...LOL im not that badrochel1977 said:machan pics are with me. hehe. u just add a pic from ur pc and see machan.![]()

.... i dont knw why... it worked with absolute path.
amila325 said:LOL im not that bad
thing is it doesnt work with relative path![]()
.... i dont knw why... it worked with absolute path.
Well this is not exactly what u need i think...
but u'll b able to do it from here without a prob nw, I think![]()
JPanel panel = new JPanel();
ImageIcon icon = new ImageIcon("logo.gif");
JLabel l1 = new JLabel();
l1.setIcon(icon);
panel.add(l1);
rochel1977 said:oh is it? any way i will give it a try and see machan. thx alot for ur help. btw do u know how to center an icon image??
i got this code where the pic is in the left hand side. i need it to be in the center
Code:JPanel panel = new JPanel(); ImageIcon icon = new ImageIcon("logo.gif"); JLabel l1 = new JLabel(); l1.setIcon(icon); panel.add(l1);
JPanel panel = new JPanel();
ImageIcon icon = new ImageIcon("logo.gif");
JLabel l1 = new JLabel("",icon,JLabel.CENTER);
panel.add(l1);

amila325 said:Well this will work... i thinkCode:JPanel panel = new JPanel(); ImageIcon icon = new ImageIcon("logo.gif"); JLabel l1 = new JLabel("",icon,JLabel.CENTER); panel.add(l1);![]()
it's working for me :S :S and i used this method alsorochel1977 said:no macho. its not working. mekata flowlayout or borderlayout dalada handa one?
import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
*
* @author Amila
*/
public class LableClass extends JFrame{
public static void main(String[] args) {
JFrame frame = new JFrame("Quote");
frame.setLayout(new BorderLayout());
JPanel panel = new JPanel();
ImageIcon icon = new ImageIcon("C:/bold.jpg");
JLabel l1 = new JLabel();
l1.setHorizontalAlignment(JLabel.CENTER);
l1.setIcon(icon);
panel.add(l1);
frame.add(panel,BorderLayout.PAGE_START);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
amila325 said:it's working for me :S :S and i used this method also
Code:import java.awt.BorderLayout; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; /** * * @author Amila */ public class LableClass extends JFrame{ public static void main(String[] args) { JFrame frame = new JFrame("Quote"); frame.setLayout(new BorderLayout()); JPanel panel = new JPanel(); ImageIcon icon = new ImageIcon("C:/bold.jpg"); JLabel l1 = new JLabel(); l1.setHorizontalAlignment(JLabel.CENTER); l1.setIcon(icon); panel.add(l1); frame.add(panel,BorderLayout.PAGE_START); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
