1. 程式人生 > >Java學習之旅1

Java學習之旅1

1.圓角按鈕的設定

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.RoundRectangle2D;

import javax.swing.JButton;

public class RButton extends JButton {
	private static final long serialVersionUID = 39082560987930759L;
	public static final Color BUTTON_COLOR1 = new Color(250, 160, 122);
	public static final Color BUTTON_COLOR2 = new Color(255, 0, 0);
	// public static final Color BUTTON_COLOR1 = new Color(125, 161, 237);
	// public static final Color BUTTON_COLOR2 = new Color(91, 118, 173);
	public static final Color BUTTON_FOREGROUND_COLOR = Color.WHITE;
	private boolean hover;

	public RButton(String name) {
		this.setText(name);
		setFont(new Font("system", Font.PLAIN, 12));//字型格式
		setBorderPainted(false);//無邊框
		setForeground(BUTTON_COLOR2);
		setFocusPainted(false);//隱藏按鈕focus邊框
		setContentAreaFilled(false);//按鈕透明
		addMouseListener(new MouseAdapter() {
			@Override
			public void mouseEntered(MouseEvent e) {//新增滑鼠進入監聽
				setForeground(BUTTON_FOREGROUND_COLOR);
				hover = true;
				repaint();
			}

			@Override
			public void mouseExited(MouseEvent e) {//新增滑鼠離開監聽
				setForeground(BUTTON_COLOR2);
				hover = false;
				repaint();
			}
		});
	}

	@Override
	protected void paintComponent(Graphics g) {//重繪button產生圓角
		Graphics2D g2d = (Graphics2D) g.create();
		int h = getHeight();
		int w = getWidth();
		float tran = 1F;
		if (!hover) {
			tran = 0.3F;
		}

		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		GradientPaint p1;
		GradientPaint p2;
		if (getModel().isPressed()) {
			p1 = new GradientPaint(0, 0, new Color(0, 0, 0), 0, h - 1, new Color(100, 100, 100));
			p2 = new GradientPaint(0, 1, new Color(0, 0, 0, 50), 0, h - 3, new Color(255, 255, 255, 100));
		} else {
			p1 = new GradientPaint(0, 0, new Color(100, 100, 100), 0, h - 1, new Color(0, 0, 0));
			p2 = new GradientPaint(0, 1, new Color(255, 255, 255, 100), 0, h - 3, new Color(0, 0, 0, 50));
		}
		g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, tran));
		RoundRectangle2D.Float r2d = new RoundRectangle2D.Float(0, 0, w - 1, h - 1, 20, 20);
		Shape clip = g2d.getClip();
		g2d.clip(r2d);
		GradientPaint gp = new GradientPaint(0.0F, 0.0F, BUTTON_COLOR1, 0.0F, h, BUTTON_COLOR2, true);
		g2d.setPaint(gp);
		g2d.fillRect(0, 0, w, h);
		g2d.setClip(clip);
		g2d.setPaint(p1);
		g2d.drawRoundRect(0, 0, w - 1, h - 1, 20, 20);
		g2d.setPaint(p2);
		g2d.drawRoundRect(1, 1, w - 3, h - 3, 18, 18);
		g2d.dispose();
		super.paintComponent(g);
	}
}

2.標籤Jlabel文字自動換行方法:

Jlabel jl=new Jlabel(string);
string 字串的首尾新增…標籤,則自動換行;
如:

JLabel jlabel2 = new JLabel("<html>鋤禾日當午,汗滴禾下土。誰知盤中餐,粒粒皆辛苦。</html>");

而在字串中新增
會在該處強制換行:

JLabel jlabel2 = new JLabel("<html>鋤禾日當午,<br>汗滴禾下土。<br>誰知盤中餐,<br>粒粒皆辛苦。</html>");

輸出標籤結果:
鋤禾日當午,
汗滴禾下土。
誰知盤中餐,
粒粒皆辛苦。

3.視窗設定背景圖片方法(JDialog同樣)

import java.awt.Graphics;
import java.awt.Image;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class PictureJPanel extends JPanel {
	public URL picture;// 圖片位置

	public PictureJPanel(URL s1) {
		this.picture = s1;
	}

	@Override
	public void paintComponent(Graphics g) {
		ImageIcon image = new ImageIcon(picture);
		image.setImage(
				image.getImage().getScaledInstance(this.getWidth(), this.getHeight(), Image.SCALE_AREA_AVERAGING));// 這裡設定圖片的大小和視窗的大小相等
		g.drawImage(image.getImage(), 0, 0, this);// 在視窗上畫出該圖片 }}
	}
}

在窗體中呼叫該方法可完成背景圖片的設定

import java.net.URL;
import javax.swing.JFrame;

public class PictureFrame extends JFrame {
	public PictureFrame() {
		setBounds(0, 0, 1000, 500);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setUndecorated(true);// 設定出去窗體右上角最大化、最小化、關閉按鈕

		URL url = PictureFrame.class.getResource("/416690300862016439.png");
		PictureJPanel pj = new PictureJPanel(url);
		add(pj);
		
		setVisible(true);
	}

	public static void main(String[] args) {
		PictureFrame pf = new PictureFrame();

	}

}