package com.pdcss.java;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class JButtonDemo extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JButton button1, button2, button3;
public JButtonDemo() {
super("JButtonDemo");
Container container = getContentPane();
container.setLayout(new FlowLayout());
button1 = new JButton("plain button");
container.add(button1);
Icon icon = new ImageIcon(
"C:\\Documents and Settings\\Administrator\\桌面\\xx.jpg");
button2 = new JButton(icon);
container.add(button2);
button3 = new JButton("button with icon and text", icon);
container.add(button3);
ButtonHandler handler = new ButtonHandler();
button1.addActionListener(handler);
button2.addActionListener(handler);
button3.addActionListener(handler);
setSize(230, 120);
setVisible(true);
}
private class ButtonHandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String output = "";
if (e.getSource() == button1) {
output = "plain button pressed.";
} else if (e.getSource() == button2) {
output = "icon button pressed.";
} else if (e.getSource() == button3) {
output = "button with icon and text pressed.";
}
JOptionPane.showMessageDialog(null, output);
}
}
public static void main(String[] args) {
JButtonDemo jbd = new JButtonDemo();
jbd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
看到button3了吗是加文字和图片的说明,你可以试试,环境是在MyEclipse下运行的
你想改什么就改什么,图片上的字可以自己看看改吧。希望对你有所帮助!
追问啊 对的 对的 谢谢
怎么追加额外的分来着?