import java.awt.image.BufferedImage;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JLabel;
BufferedImage bufferedImage=null;
try {
bufferedImage=ImageIO.read(new File("pics/test.jpg"));
}
catch(IOException e) {}
Image image=bufferedImage.getScaledInstance(
bufferedImage.getWidth()/2,
bufferedImage.getHeight()/2,
Image.SCALE_SMOOTH); //将图片缩小为原来的1/2
JLabel label=new JLabel(new ImageIcon(image));
label.setHorizontalAlignment(JLabel.CENTER); //图片居中
label.setVerticalAlignment(JLabel.CENTER);
import java.awt.BorderLayout;
import javax.swing.border.Border;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JButton;
//所有Border对象由BorderFactory创建
//createEmptyBorder(int top,int left,int bottom,int right)
//参数表示边距,不设置border,按钮会充满panel,设置后按钮与panel间
//将出现2像素的空隙。设置边距仅在BorderLayout内生效。
Border border=BorderFactory.createEmptyBorder(2,2,2,2);
JPanel panel1=new JPanel();
panel1.setLayout(new BorderLayout());
panel1.setBorder(border);
JButton button1=new JButton("EmptyBorder");
panel1.add(button1,BorderLayout.CENTER);
//LineBorder(Color c,int l)
//参数表示颜色和线条粗细,效果panel周围出现线框。
Border border=BorderFactory.createLineBorder(Color.BLUE,1);
JPanel panel=new JPanel();
panel.setBorder(border);
//蚀刻边框效果
Border border=BorderFactory.createEtchedBorder();
//标题效果
Border border=BorderFactory.createTitledBorder("标题");
//指定颜色的标题效果
Border lineBorder=BorderFactory.createLineBorder(Color.BLUE,1);
Border border=BorderFactory.createTitledBorder(lineBorder,"标题");
//组合边框效果,外边为蚀刻边框,里边为标题边框。
Border titledBorder=BorderFactory.createTitledBorder("标题");
Border etchedBorder=BorderFactory.createEtchedBorder();
Border border=BorderFactory.createCompoundBorder(
etchedBorder, //外边框
titledBorder);//内边框
import javax.swing.JPanel;
import javax.swing.Box;
import javax.swing.BoxLayout;
JPanel panel=new JPanel();
//设置竖直排列,X_AXIS水平排列
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
panel.add(...);
//在竖直排列的两个控件之间空出10像素空隙
panel.add(Box.createRigidArea(new Dimension(0,10)));
panel.add(...);
import java.awt.Color;
Color c=Color.BLUE; //常见颜色
Color c=new Color(255,0,0); //RGB颜色,0-255
Color c=new Color(0.5f,0.4f,0.3f,0.6f); //rgb+透明度,0-1之间
Color c=Color.decode("0xFFFFFF"); //获取16进制颜色
16进制或RGB颜色对照表
import javax.swing.JPanel;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
JPanel panel=new JPanel();
//当有按钮等事件监听优先级较高的控件存在时,panel的键盘监听可能没有
//作用,可以在按钮监听事件中使用panel.requestFocus()让panel获得
//焦点,使键盘监听生效。
panel.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode()) {
case KeyEvent.VK_UP:
...
break; //去掉,则会继续执行语句2
case KeyEvent.VK_DOWN:
... //语句2
break;
}
}
});
import java.awt.FlowLayout;
FlowLayout flayout=new FlowLayout(FlowLayout.RIGTH);
flayout.setAlignment(FlowLayout.CENTER); //也可采用此方式
setLayout(flayout);
public class Myframe extends JFrame {
public Myframe() {
JButton b=new JButton("Hello,World!");
b.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
...
//获取Myframe对象
Myframe.this.requestFocus(); //请求焦点
}
});
}
}