最近需要做一些桌面程序,因爲我比較擅長的語言是java,php;只能選擇java來做,於是先仿照一些桌面程序來佈局練下手,下面就是代碼記錄了,講真過程有點痛苦,一度的想換javafx,但是羣裏朋友說,javafx是屬於oracle jre協議裏面的,要小心使用,於是選擇了免費並且沒有後顧之憂的swing。
算了直接上代碼了,裏面的就是簡單的佈局和調整,使用了swing的美化包substance.jar。
Login.java
package com.qianku.project;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
public class Login extends JFrame{
public JFrame frame;
// 得到顯示器屏幕的寬高
public int width = Toolkit.getDefaultToolkit().getScreenSize().width;
public int height = Toolkit.getDefaultToolkit().getScreenSize().height;
// 定義窗體的寬高
public int windowsWedth = 662;
public int windowsHeight = 442;
private void initialize() {
frame = this;
frame.setLayout(new BorderLayout(2,2));
JPanel topPanel = new TopPanel("image/logintitle_bg.png");
topPanel.setBackground(Color.red);
JPanel leftPanel = new JPanel();
leftPanel.setBackground(Color.white);
JPanel rightPanel = new JPanel();
rightPanel.setBackground(Color.white);
rightPanel.setLayout(null);
frame.add("North", topPanel); //將按鈕添加到窗口中
frame.add("East", rightPanel);
frame.add("Center",leftPanel);
JLabel label = new JLabel();
ImageIcon icon = new ImageIcon("image/login_banner.png");
label.setIcon(icon);
leftPanel.add(label);
JLabel label01 = new JLabel("帳號登錄");
label01.setBounds(50, 20, 200, 20);
JTextField name = new JTextField();
name.setBounds(50, 100, 200, 20);
name.setColumns(20);
JTextField pass = new JTextField();
pass.setBounds(50, 130, 200, 20);
pass.setColumns(20);
//登陸按鈕
JButton loginBtn = new JButton("登錄");
loginBtn.setBounds(50, 160, 200, 40);
rightPanel.add(label01);
rightPanel.add(name);
rightPanel.add(pass);
rightPanel.add(loginBtn);
topPanel.setPreferredSize(new Dimension(0, 80));
rightPanel.setPreferredSize(new Dimension(300, 0));
frame.setBounds((width - windowsWedth) / 2,
(height - windowsHeight) / 2, windowsWedth, windowsHeight);
//設置去掉邊框
//frame.setUndecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
loginBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new MainFrame().initialize();
}
});
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
try {
/*
* 想要修改皮膚的話,只需要更改,下面這個函數的參數,具體改成什麼樣,可以打開,Referenced Libraries -> 點擊substance.jar -> 找到org.jvnet.substance.skin這個包 -> 將下面的SubstanceDustCoffeeLookAndFeel 替換成 剛剛打開的包下的任意一個“Substance....LookAndFeel”即可
*/
UIManager.setLookAndFeel(new org.jvnet.substance.skin.SubstanceBusinessLookAndFeel());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Login login = new Login();
login.initialize();
}
});
}
}
class TopPanel extends JPanel
{
private ImageIcon pic;
private Image image;
private int width;
private int height;
public TopPanel(String imgUrl) {
pic = new ImageIcon(imgUrl);
image = pic.getImage();
width = pic.getIconWidth();
height = pic.getIconHeight();
this.setSize(width, height);
}
@Override
public void paint(Graphics arg0) {
super.paint(arg0);
arg0.drawImage(image, 0, 0, width, height,null);
}
//獲取作爲背景圖片的寬度
public int getdefaultWidth(){
return width;
}
//獲取作爲背景圖片的高度
public int getdefaultHeight(){
return height;
}
}
MainFrame.java
package com.qianku.project;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainFrame extends JFrame{
public JFrame frame;
public int width = Toolkit.getDefaultToolkit().getScreenSize().width;
public int height = Toolkit.getDefaultToolkit().getScreenSize().height;
public int windowsWedth = 1080;
public int windowsHeight = 668;
public void initialize()
{
frame = this;
frame.setLayout(new BorderLayout(2,2));
JPanel topPanel = new JPanel();
topPanel.setBackground(Color.red);
JPanel bottomPanel = new JPanel();
bottomPanel.setBackground(Color.green);
bottomPanel.setLayout(new BorderLayout(2,2));
JPanel leftPanel = new MainLeftPanel();
JPanel rightPanel = new MainRightPanel();
bottomPanel.add("West", leftPanel);
bottomPanel.add("Center", rightPanel);
frame.add("North", topPanel);
frame.add("Center",bottomPanel);
topPanel.setPreferredSize(new Dimension(0, 80));
frame.setBounds((width - windowsWedth) / 2,
(height - windowsHeight) / 2, windowsWedth, windowsHeight);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
new MainFrame().initialize();
}
}
MainLeftPanel.java
package com.qianku.project;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JPanel;
public class MainLeftPanel extends JPanel{
public MainLeftPanel()
{
this.setBackground(Color.yellow);
this.setPreferredSize(new Dimension(150, 0));
}
}
MainRightPanel.java:
package com.qianku.project;
import java.awt.Color;
import javax.swing.JPanel;
public class MainRightPanel extends JPanel{
public MainRightPanel()
{
this.setBackground(Color.pink);
}
}
swing寫桌面程序,很難受。附上源碼下載鏈接 : https://download.csdn.net/download/gaoxuaiguoyi/10425601