当前位置: 技术问答>java相关
极菜问题:在JButton上加Icon
来源: 互联网 发布时间:2015-11-09
本文导语: 感觉因给挺简单 // Retrieve the icon Icon icon = new ImageIcon("E:\MyStudy\MyLesson\MyPaint\icon3.ico"); // Add or change the icon; it will appear to the left of the text button.setIcon(icon); 怎么不好用,请教 ...
感觉因给挺简单
// Retrieve the icon
Icon icon = new ImageIcon("E:\MyStudy\MyLesson\MyPaint\icon3.ico");
// Add or change the icon; it will appear to the left of the text
button.setIcon(icon);
怎么不好用,请教
|
hehe~~java是不认.ico的,要用.gif格式的图片
|
import javax.swing.*;import java.awt.*;import java.awt.event.*;
class Mywindow extends JFrame implements ActionListener
{ JButton button1,button2,button3;JTextArea text;Icon icon;
Mywindow()
{setSize(200,200);setVisible(true);
Container con=getContentPane();
text=new JTextArea();
con.setLayout(new GridLayout(1,4));
icon=new ImageIcon("tom.jpg");
button1=new JButton("Boy",icon); button2=new JButton("Girl");
button3=new JButton("friend",new ImageIcon("tom1.jpg"));
button1.setVerticalTextPosition(AbstractButton.TOP);
button1.setMnemonic('b');
con.add(button1);con.add(button2);con.add(button3);con.add(text);
button1.addActionListener(this);button3.addActionListener(this);
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);}});
}
public void actionPerformed(ActionEvent e)
{if(e.getSource()==button1)
{ text.setText(button1.getText()+button2.getText());
button2.setIcon(button1.getIcon());
button2.setHorizontalTextPosition(AbstractButton.RIGHT);button3.setEnabled(false);
}
else if(e.getSource()==button3)
{ text.append(button3.getText()); button2.setIcon(button3.getIcon());
}
}
}
public class Example25_10
{ public static void main(String args[])
{ Mywindow win=new Mywindow();win.pack();
}
}
在java中是这么用的
class Mywindow extends JFrame implements ActionListener
{ JButton button1,button2,button3;JTextArea text;Icon icon;
Mywindow()
{setSize(200,200);setVisible(true);
Container con=getContentPane();
text=new JTextArea();
con.setLayout(new GridLayout(1,4));
icon=new ImageIcon("tom.jpg");
button1=new JButton("Boy",icon); button2=new JButton("Girl");
button3=new JButton("friend",new ImageIcon("tom1.jpg"));
button1.setVerticalTextPosition(AbstractButton.TOP);
button1.setMnemonic('b');
con.add(button1);con.add(button2);con.add(button3);con.add(text);
button1.addActionListener(this);button3.addActionListener(this);
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);}});
}
public void actionPerformed(ActionEvent e)
{if(e.getSource()==button1)
{ text.setText(button1.getText()+button2.getText());
button2.setIcon(button1.getIcon());
button2.setHorizontalTextPosition(AbstractButton.RIGHT);button3.setEnabled(false);
}
else if(e.getSource()==button3)
{ text.append(button3.getText()); button2.setIcon(button3.getIcon());
}
}
}
public class Example25_10
{ public static void main(String args[])
{ Mywindow win=new Mywindow();win.pack();
}
}
在java中是这么用的