📄 sunz.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Sunz extends JFrame
{
public Sunz()
{
Container con=this.getContentPane();
con.setLayout(null);
ImagePanel panelTemp=new ImagePanel("mybank.jpg");
panelTemp.setBounds(0,0,640,480);
con.add(panelTemp);
this.addWindowListener (new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
});
}
public static void main(String args[])
{
System.out.println("Starting App");
Sunz f = new Sunz();
f.setTitle("游戏");
f.setBounds(0,0,640,480);
f.show();
}
}
class ImagePanel extends JPanel implements ActionListener
{
private String strPicName;
ImageIcon white1=new ImageIcon("cat.jpg");
ImageIcon white2=new ImageIcon("fish.jpg");
public ImagePanel(String strName)
{
strPicName = strName;
this.setLayout(new GridLayout(4,3));
JButton button1=new JButton(white1);
JButton button2=new JButton(white2);
JButton button3=new JButton(white1);
JButton button4=new JButton(white2);
JButton button5=new JButton(white1);
JButton button6=new JButton(white2);
JButton button7=new JButton(white1);
JButton button8=new JButton(white2);
JButton button9=new JButton(white1);
JButton button10=new JButton(white2);
JButton button11=new JButton(white1);
button1.setBounds(0,0,160,105);
button1.setFocusPainted(false);
button1.setBorderPainted(false);
button1.setContentAreaFilled(false);
// button1.setMargin(new Insets(0,0,0,0));
button2.setBounds(0,0,160,105);
button2.setFocusPainted(false);
button2.setBorderPainted(false);
button2.setContentAreaFilled(false);
// button2.setMargin(new Insets(0,0,0,0));
button3.setBounds(0,0,160,105);
button3.setFocusPainted(false);
button3.setBorderPainted(false);
button3.setContentAreaFilled(false);
// button3.setMargin(new Insets(0,0,0,0));
button4.setBounds(0,0,160,105);
button4.setFocusPainted(false);
button4.setBorderPainted(false);
button4.setContentAreaFilled(false);
// button4.setMargin(new Insets(0,0,0,0));
button5.setBounds(0,0,160,105);
button5.setFocusPainted(false);
button5.setBorderPainted(false);
button5.setContentAreaFilled(false);
// button5.setMargin(new Insets(0,0,0,0));
button6.setBounds(0,0,160,105);
button6.setFocusPainted(false);
button6.setBorderPainted(false);
button6.setContentAreaFilled(false);
// button6.setMargin(new Insets(0,0,0,0));
button7.setBounds(0,0,160,105);
button7.setFocusPainted(false);
button7.setBorderPainted(false);
button7.setContentAreaFilled(false);
// button7.setMargin(new Insets(0,0,0,0));
button8.setBounds(0,0,160,105);
button8.setFocusPainted(false);
button8.setBorderPainted(false);
button8.setContentAreaFilled(false);
// button8.setMargin(new Insets(0,0,0,0));
button9.setBounds(0,0,160,105);
button9.setFocusPainted(false);
button9.setBorderPainted(false);
button9.setContentAreaFilled(false);
// button9.setMargin(new Insets(0,0,0,0));
button10.setBounds(0,0,160,105);
button10.setFocusPainted(false);
button10.setBorderPainted(false);
button10.setContentAreaFilled(false);
// button10.setMargin(new Insets(0,0,0,0));
button11.setBounds(0,0,160,105);
button11.setFocusPainted(false);
button11.setBorderPainted(false);
button11.setContentAreaFilled(false);
// button11.setMargin(new Insets(0,0,0,0));
add(button1);
add(button2);
add(button3);
add(button4);
add(button5);
add(button6);
add(button7);
add(button8);
add(button9);
add(button10);
add(button11);
}
public void paintComponent(Graphics g)
{
loadPic(strPicName,g);
}
public void loadPic(String picName,Graphics g)
{
ImageIcon img=new ImageIcon(picName);
int pWidth=this.getWidth();
int pHeight=this.getHeight();
int mWidth=img.getIconWidth() ;
int mHeight=img.getIconHeight() ;
int x,y;
int width,height;
if((mWidth<=pWidth)&&(mHeight<=pHeight)){
x=(pWidth-mWidth); y=(pHeight-mHeight);
width=mWidth; height=mHeight;
}else {
width=pWidth; height=pHeight;
float widthScale=(float)pWidth/(float)mWidth;
float heightScale=(float)pHeight/(float)mHeight;
if(widthScale<heightScale)
height=(int)(mHeight*widthScale);
else
width=(int)(mWidth*heightScale);
x=(pWidth-width);
y=(pHeight-height);
}
//statusBar.setText("文件:"+ this +" 大小:"+mWidth+"X"+mHeight);
g.clearRect(0,0,pWidth,pHeight);
g.drawImage(img.getImage(),x,y,width,height,this);
}
public void actionPerformed(ActionEvent e)
{
//if(e.getSource()==label)
{
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -