如何将这个程序改成用MouseAdapter来接收MouseListener的事件?由于字数限制下面省略了import的部分。
publicclassCalculatorextendsJPanel{privatestaticfinallongserialVersionUID=34693840447...
public class Calculator extends JPanel{
private static final long serialVersionUID = 3469384044702699262L;
public static BufferedImage bgi;//背景图
static{
try {
bgi=ImageIO.read(new java.io.File(System.getProperty("user.dir")+"\\src\\BG.png"));//注意那个定位到工程目录
} catch (Exception e) {}
}
public void paintComponent(Graphics gx){
super.paintComponent(gx);
Graphics2D g=(Graphics2D) gx;
if(bgi!=null){
int w=getWidth();
int h=getHeight();
g.drawImage(bgi,(w-bgi.getWidth())/2,(h-bgi.getHeight())/2,this);
}
}
public static void main(String[] args)throws Exception{
JFrame f=new JFrame("");
f.setLayout(null);
f.setDefaultCloseOperation(3);
f.setSize(560,540);
f.setLocationRelativeTo(null);
Calculator IP=new Calculator();
f.setContentPane(IP);
IP.setLayout(null);
final JTextField display = new JTextField("");//文本框
display.setBounds(15, 12, 454, 90);
display.setFont(new java.awt.Font("宋体",1,30));
final JButton[][] JB=new JButton[6][4];
for(int i=0;i<6;i++){
for(int j=0;j<4;j++){
JB[i][j]=new JButton();
JB[i][j].setContentAreaFilled(false); //button背景透明
JB[i][j].setBorderPainted(false); //不显示button边框
JB[i][j].setFocusPainted(false); //Focus不显示边框
JB[i][j].setName("JB["+i+"]["+j+"]"); //setName以提供下面getName用来做按钮效果
JB[i][j].setIcon(new ImageIcon(System.getProperty("user.dir")+"\\src\\"+"JB["+i+"]["+j+"]"+"-1.png"));//设置button背景
JB[i][j].setBounds(8+j*120, 112+i*65, 109, 71);
f.add(JB[i][j]);
JB[i][j].addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
String n = ((JButton)e.getSource()).getName();
((JButton)e.getSource()).setIcon(new ImageIcon(System.getProperty("user.dir")+"\\src\\"+n+"-2.png"));
}
public void mouseExited(MouseEvent e) {
String n = ((JButton)e.getSource()).getName();
((JButton)e.getSource()).setIcon(new ImageIcon(System.getProperty("user.dir")+"\\src\\"+n+"-1.png"));
}
public void mousePressed(MouseEvent e) {
String n = ((JButton)e.getSource()).getName();
((JButton)e.getSource()).setIcon(new ImageIcon(System.getProperty("user.dir")+"\\src\\"+n+"-3.png"));
}
public void mouseReleased(MouseEvent e) {
String n = ((JButton)e.getSource()).getName();
((JButton)e.getSource()).setIcon(new ImageIcon(System.getProperty("user.dir")+"\\src\\"+n+"-2.png"));
}
});
}
}
f.add(display);
f.setResizable(false);
f.setVisible(true);
}
} 展开
private static final long serialVersionUID = 3469384044702699262L;
public static BufferedImage bgi;//背景图
static{
try {
bgi=ImageIO.read(new java.io.File(System.getProperty("user.dir")+"\\src\\BG.png"));//注意那个定位到工程目录
} catch (Exception e) {}
}
public void paintComponent(Graphics gx){
super.paintComponent(gx);
Graphics2D g=(Graphics2D) gx;
if(bgi!=null){
int w=getWidth();
int h=getHeight();
g.drawImage(bgi,(w-bgi.getWidth())/2,(h-bgi.getHeight())/2,this);
}
}
public static void main(String[] args)throws Exception{
JFrame f=new JFrame("");
f.setLayout(null);
f.setDefaultCloseOperation(3);
f.setSize(560,540);
f.setLocationRelativeTo(null);
Calculator IP=new Calculator();
f.setContentPane(IP);
IP.setLayout(null);
final JTextField display = new JTextField("");//文本框
display.setBounds(15, 12, 454, 90);
display.setFont(new java.awt.Font("宋体",1,30));
final JButton[][] JB=new JButton[6][4];
for(int i=0;i<6;i++){
for(int j=0;j<4;j++){
JB[i][j]=new JButton();
JB[i][j].setContentAreaFilled(false); //button背景透明
JB[i][j].setBorderPainted(false); //不显示button边框
JB[i][j].setFocusPainted(false); //Focus不显示边框
JB[i][j].setName("JB["+i+"]["+j+"]"); //setName以提供下面getName用来做按钮效果
JB[i][j].setIcon(new ImageIcon(System.getProperty("user.dir")+"\\src\\"+"JB["+i+"]["+j+"]"+"-1.png"));//设置button背景
JB[i][j].setBounds(8+j*120, 112+i*65, 109, 71);
f.add(JB[i][j]);
JB[i][j].addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
String n = ((JButton)e.getSource()).getName();
((JButton)e.getSource()).setIcon(new ImageIcon(System.getProperty("user.dir")+"\\src\\"+n+"-2.png"));
}
public void mouseExited(MouseEvent e) {
String n = ((JButton)e.getSource()).getName();
((JButton)e.getSource()).setIcon(new ImageIcon(System.getProperty("user.dir")+"\\src\\"+n+"-1.png"));
}
public void mousePressed(MouseEvent e) {
String n = ((JButton)e.getSource()).getName();
((JButton)e.getSource()).setIcon(new ImageIcon(System.getProperty("user.dir")+"\\src\\"+n+"-3.png"));
}
public void mouseReleased(MouseEvent e) {
String n = ((JButton)e.getSource()).getName();
((JButton)e.getSource()).setIcon(new ImageIcon(System.getProperty("user.dir")+"\\src\\"+n+"-2.png"));
}
});
}
}
f.add(display);
f.setResizable(false);
f.setVisible(true);
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询