/** * */ 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下运行的 你想改什么就改什么,图片上的字可以自己看看改吧。希望对你有所帮助!
一枕庭前雪
2023-03-15 07:38:02
JButton jb = new JButton(icon); jb.setText("1"); jb.setName("1"); 其他的一样的