TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
运行图:

- 代码1:Demo.java
- package demo.swing;
- import javax.swing.ImageIcon;
- public class Demo {
- /**
- * Launch the application
- *
- * @param args
- */
- public static void main(String args[]) {
- try {
- DemoWindow win = new DemoWindow();
- win.setBounds(100, 100, 400, 300);
- win.setTitle("打造专业外观-Swing");
- win.setIcon(new ImageIcon(DemoWindow.class.getResource("title.png")));
- win.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
复制代码
- 代码2:DemoWindow.java
- package demo.swing;
- import java.awt.Color;
- import java.awt.Cursor;
- import java.awt.Dimension;
- import java.awt.FontMetrics;
- import java.awt.GradientPaint;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Point;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.ComponentEvent;
- import java.awt.event.ComponentListener;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.awt.event.MouseMotionListener;
- import javax.swing.BorderFactory;
- import javax.swing.Icon;
- import javax.swing.ImageIcon;
- import javax.swing.JButton;
- import javax.swing.JComponent;
- import javax.swing.JWindow;
- public class DemoWindow extends JWindow implements MouseListener,
- MouseMotionListener, ComponentListener, ActionListener {
- private Icon northwestIcon;
- private Icon northeastIcon;
- private Icon northIcon;
- private Icon southwestIcon;
- private Icon southeastIcon;
- private Icon southIcon;
- private Icon westIcon;
- private Icon eastIcon;
- private Icon closeIcon;
- private Icon closeoverIcon;
- private Icon closedownIcon;
- // point
- private Point location;
- private Point size;
- // cursor
- private Cursor sizeCursor = new Cursor(Cursor.SE_RESIZE_CURSOR);
- private Cursor moveCursor = new Cursor(Cursor.MOVE_CURSOR);
- private Cursor defaultCursor = new Cursor(Cursor.DEFAULT_CURSOR);
- //
- private ContentPane contentPane;
- private JButton closeButton;
- // color
- private Color color1 = new Color(254, 254, 254);
- private Color color2 = new Color(204, 220, 247);
- private Color titleColor = new Color(128, 128, 128);
- // title bar
- private String title;
- private Icon icon;
- /**
- * Create the frame
- */
- public DemoWindow() {
- super();
- initIcon();
- initComponent();
- contentPane.addComponentListener(this);
- contentPane.addMouseListener(this);
- contentPane.addMouseMotionListener(this);
- //
- }
- public void setTitle(String title) {
- this.title = title;
- contentPane.repaint();
- }
- public void setIcon(Icon icon) {
- this.icon = icon;
- }
- private void initComponent() {
- contentPane = new ContentPane();
- setContentPane(contentPane);
- closeButton = new JButton();
- closeButton.setBorder(BorderFactory.createEmptyBorder());
- closeButton.setIcon(closeIcon);
- closeButton.setRolloverIcon(closeoverIcon);
- closeButton.setPressedIcon(closedownIcon);
- closeButton.addActionListener(this);
- contentPane.add(closeButton);
- }
- private void initIcon() {
- northwestIcon = new ImageIcon(DemoWindow.class.getResource("northwest.png"));
- northeastIcon = new ImageIcon(DemoWindow.class.getResource("northeast.png"));
- northIcon = new ImageIcon(DemoWindow.class.getResource("north.png"));
- southwestIcon = new ImageIcon(DemoWindow.class.getResource("southwest.png"));
- southeastIcon = new ImageIcon(DemoWindow.class.getResource("southeast.png"));
- southIcon = new ImageIcon(DemoWindow.class.getResource("south.png"));
- westIcon = new ImageIcon(DemoWindow.class.getResource("west.png"));
- eastIcon = new ImageIcon(DemoWindow.class.getResource("east.png"));
- closeIcon = new ImageIcon(DemoWindow.class.getResource("close.png"));
- closeoverIcon = new ImageIcon(DemoWindow.class.getResource("closeover.png"));
- closedownIcon = new ImageIcon(DemoWindow.class.getResource("closedown.png"));
- }
- private class ContentPane extends JComponent {
- @Override
- protected void paintComponent(Graphics g) {
- int w = getWidth();
- int h = getHeight();
- northwestIcon.paintIcon(this, g, 0, 0);
- northeastIcon.paintIcon(this, g, w - northeastIcon.getIconWidth(),0);
- g.drawImage(((ImageIcon) northIcon).getImage(), northwestIcon
- .getIconWidth(), 0, w - northeastIcon.getIconWidth(),
- northIcon.getIconHeight(), 0, 0, northIcon.getIconWidth(),
- northIcon.getIconHeight(), this);
- southwestIcon.paintIcon(this, g, 0, h- southwestIcon.getIconHeight());
- southeastIcon.paintIcon(this, g, w - southeastIcon.getIconWidth(),
- h - southeastIcon.getIconHeight());
- g.drawImage(((ImageIcon) southIcon).getImage(), southwestIcon
- .getIconWidth(), h - southIcon.getIconHeight(), w
- - southeastIcon.getIconWidth(), h, 0, 0, southIcon
- .getIconWidth(), southIcon.getIconHeight(), this);
- g.drawImage(((ImageIcon) westIcon).getImage(), 0, northeastIcon
- .getIconHeight(), westIcon.getIconWidth(), h
- - southwestIcon.getIconHeight(), 0, 0, westIcon
- .getIconWidth(), westIcon.getIconHeight(), this);
- g.drawImage(((ImageIcon) eastIcon).getImage(), w
- - eastIcon.getIconWidth(), northeastIcon.getIconHeight(),
- w, h - southeastIcon.getIconHeight(), 0, 0, eastIcon
- .getIconWidth(), eastIcon.getIconHeight(), this);
- GradientPaint gp = new GradientPaint(0, 0, color1, 0, h - 1, color2);
- ((Graphics2D) g).setPaint(gp);
- g.fillRect(westIcon.getIconWidth(), northIcon.getIconHeight(), w
- - westIcon.getIconWidth() - eastIcon.getIconWidth(), h
- - northIcon.getIconHeight() - southIcon.getIconHeight());
- // title
- if (title == null) {
- title = "";
- }
- g.setColor(titleColor);
- FontMetrics fm = getFontMetrics(getFont());
- g.drawString(title, (getWidth() - fm.stringWidth(title)) / 2,
- northIcon.getIconHeight() / 2 + fm.getHeight() / 4);
- if (icon != null) {
- icon.paintIcon(contentPane, g, getWidth() / 2
- - fm.stringWidth(title) / 2 - icon.getIconWidth() - 10,
- northIcon.getIconHeight() / 2 - icon.getIconHeight()
- / 2);
- }
- }
- }
- public void mouseClicked(MouseEvent e) {
- }
- public void mouseEntered(MouseEvent e) {
- }
- public void mouseExited(MouseEvent e) {
- }
- public void mousePressed(MouseEvent e) {
- int x = e.getX();
- int y = e.getY();
- int w = contentPane.getWidth();
- int h = contentPane.getHeight();
- if (x > northwestIcon.getIconWidth()
- && x < contentPane.getWidth() - northeastIcon.getIconWidth()
- && y > 0 && y < northIcon.getIconHeight()) {
- location = e.getLocationOnScreen();
- } else if (x > w - southeastIcon.getIconWidth() && x < w
- && y > h - southeastIcon.getIconHeight() && y < h) {
- size = e.getLocationOnScreen();
- }
- }
- public void mouseReleased(MouseEvent e) {
- location = null;
- if (size != null) {
- try {
- Point newPoint = e.getLocationOnScreen();
- Dimension d = new Dimension(getSize().width + newPoint.x
- - size.x, getSize().height + newPoint.y - size.y);
- if (d.width > 100 && d.height > 50) {
- setSize(d);
- }
- } finally {
- size = null;
- }
- }
- }
- public void mouseDragged(MouseEvent e) {
- if (location != null) {
- Point newPoint = e.getLocationOnScreen();
- setLocation(getLocation().x + newPoint.x - location.x,
- getLocation().y + newPoint.y - location.y);
- location = newPoint;
- }
- }
- public void mouseMoved(MouseEvent e) {
- int x = e.getX();
- int y = e.getY();
- int w = contentPane.getWidth();
- int h = contentPane.getHeight();
- if (x > w - southeastIcon.getIconWidth() && x < w
- && y > h - southeastIcon.getIconHeight() && y < h) {
- if (contentPane.getCursor() != sizeCursor) {
- contentPane.setCursor(sizeCursor);
- }
- } else if (x > northwestIcon.getIconWidth()
- && x < contentPane.getWidth() - northeastIcon.getIconWidth()
- && y > 0 && y < northIcon.getIconHeight()) {
- if (contentPane.getCursor() != moveCursor) {
- contentPane.setCursor(moveCursor);
- }
- } else {
- if (contentPane.getCursor() != defaultCursor) {
- contentPane.setCursor(defaultCursor);
- }
- }
- }
- //
- public void componentHidden(ComponentEvent e) {
- }
- public void componentMoved(ComponentEvent e) {
- }
- public void componentResized(ComponentEvent e) {
- closeButton.setBounds(contentPane.getWidth()
- - northeastIcon.getIconWidth() - closeIcon.getIconWidth(), 0,
- closeIcon.getIconWidth(), closeIcon.getIconHeight());
- }
- public void componentShown(ComponentEvent e) {
- }
- public void actionPerformed(ActionEvent e) {
- dispose();
- }
- }
复制代码 用Swing实现不足的地方在于顶层窗口,截至到JDK1.6发布,AWT尚未支持不规则矩形窗体和窗口半透明。
有好消息称,JDK1.7中将实现。本程序需要在JDK1.6下编译,因为用了一些新增的API,如果要改成1.6以前版
本实现也不难。但是在JRE1.6下运行,性能会明显提高。
源码下载:http://file.javaxxz.com/2014/10/31/235542437.zip |
|