记录java桌面程序swing布局的痛心
最近需要做一些桌面程序,因为我比较擅长的语言是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
原文作者:一只勤奋的代码狗
原文地址: https://blog.csdn.net/gaoxuaiguoyi/article/details/80380641
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
原文地址: https://blog.csdn.net/gaoxuaiguoyi/article/details/80380641
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
相关文章