程序代码如下:
package Chapter10.JButtonExample;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class JButtonDemo extends JFrame
{
private JButton button1,button2;
public JButtonDemo()
{
super("测试按钮类");
setSize(250,100);
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e)
{ }
Container container=getContentPane();
container.setLayout(new GridLayout(1,2));
container.setBackground(Color.yellow);
button1=new JButton("按钮1");
button1.setFont(new Font("Serif",Font.PLAIN,12));
ImageIcon img1=new ImageIcon("source/wave.gif");
ImageIcon img2=new ImageIcon("source/stop.gif");
button2=new JButton("按钮2",img1);
button2.setRolloverIcon(img2);
button2.setFont(new Font("Serif",Font.PLAIN,12));
ButtonHandler handler=new ButtonHandler();
button1.addActionListener(handler);
button2.addActionListener(handler);
container.add(button1);
container.add(button2);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
JButtonDemo demo=new JButtonDemo();
demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
JOptionPane.showMessageDialog(JButtonDemo.this, "你按了:"+event.getActionCommand());
}
}
}
上述程序在命令行中运行一切正常,但是在Eclipse中运行时遇到一个小问题,正常运行时会在“按钮2”的左边显示图片并且将鼠标移动到“按钮2”的区域上时图片会自动变换,向大家请教一下这个问题该怎么解决?
非常感谢!!
package Chapter10.JButtonExample;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class JButtonDemo extends JFrame
{
private JButton button1,button2;
public JButtonDemo()
{
super("测试按钮类");
setSize(250,100);
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e)
{ }
Container container=getContentPane();
container.setLayout(new GridLayout(1,2));
container.setBackground(Color.yellow);
button1=new JButton("按钮1");
button1.setFont(new Font("Serif",Font.PLAIN,12));
ImageIcon img1=new ImageIcon("source/wave.gif");
ImageIcon img2=new ImageIcon("source/stop.gif");
button2=new JButton("按钮2",img1);
button2.setRolloverIcon(img2);
button2.setFont(new Font("Serif",Font.PLAIN,12));
ButtonHandler handler=new ButtonHandler();
button1.addActionListener(handler);
button2.addActionListener(handler);
container.add(button1);
container.add(button2);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
JButtonDemo demo=new JButtonDemo();
demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
JOptionPane.showMessageDialog(JButtonDemo.this, "你按了:"+event.getActionCommand());
}
}
}
上述程序在命令行中运行一切正常,但是在Eclipse中运行时遇到一个小问题,正常运行时会在“按钮2”的左边显示图片并且将鼠标移动到“按钮2”的区域上时图片会自动变换,向大家请教一下这个问题该怎么解决?
非常感谢!!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货