📄 imagepanel.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
/** This class is a panel that receives an image to display. */
class ImagePanel extends JPanel{
private ImageIcon icon;
String quote="";
int textWidth=420;
int left=25;
int height=50;
ImagePanel(ImageIcon icon,String s){
this(icon);
quote=s;
}
ImagePanel(ImageIcon icon){
this.icon=icon;
int w=icon.getIconWidth();
int h=icon.getIconHeight();
setPreferredSize(new Dimension(w+6,h+6));
setBorder(new TitledBorder(new EtchedBorder()));
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent m){
setBorder(new TitledBorder(new EtchedBorder(getBackground(),Color.white)));
}
public void mouseReleased(MouseEvent m){
setBorder(new TitledBorder(new EtchedBorder()));
}
});
}
public void paint(Graphics g){
super.paint(g);
icon.paintIcon(this,g,3,3);
Font font=new Font("Times new roman",Font.BOLD,18);
FontMetrics metrics=getFontMetrics(font);
g.setFont(font);
int w=metrics.stringWidth(quote);
int h=metrics.getHeight();
if(w>textWidth*2) return;
if(w<=textWidth){
left=left+(textWidth-w)/2;
g.drawString(quote,left,height);
}
else{
int l=quote.length();
String line1="",line2;
int i;
for(i=0;i<l;i++){
char ch=quote.charAt(i);
if((i>l/2)&&ch==' ')
break;
line1=line1+ch;
}
line2=quote.substring(i,l);
w=metrics.stringWidth(line1);
left=left+(textWidth-w)/2;
g.drawString(line1,left,height);
g.drawString(line2,left,height+h);
}
left=25;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -