Java Swing LookAndFeel Demo

简介: 不说什么了,直接贴代码吧!呀的也太直接了点这博客写的   import java.awt.BorderLayout;import java.awt.GridBagConstraints;import java.

不说什么了,直接贴代码吧!呀的也太直接了点这博客写的

 

import java.awt.BorderLayout; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import javax.swing.UIManager.LookAndFeelInfo; public class GridBagLayoutDemoPanel extends JPanel { /** * */ private static final long serialVersionUID = 566006540006934384L; private JTextField mTxtFieldA; private JTextField mTxtFieldB; private JTextField mTxtFieldC; private JButton mButtonA; private JButton mButtonB; private JButton mButtonC; private JButton mButtonD; private JButton mButtonE; public GridBagLayoutDemoPanel() { super(new GridBagLayout(), true); initComponents(); setUpListeners(); } private void initComponents() { // text field mTxtFieldA = new JTextField(); mTxtFieldB = new JTextField(); mTxtFieldC = new JTextField(); // Button mButtonA = new JButton("Button A"); mButtonB = new JButton("Button B"); mButtonC = new JButton("Button C"); mButtonD = new JButton("Button D"); mButtonE = new JButton("Button E"); // align all the labels GridBagConstraints c = new GridBagConstraints(); c.weightx = 0.5; // default is zero, all components will c.fill = GridBagConstraints.HORIZONTAL; c.anchor = GridBagConstraints.CENTER; c.gridx = 0; c.gridy = 0; c.insets = new Insets(0, 0, 0, 0); add(mButtonA, c); c.gridx = 1; c.gridy = 0; add(mButtonB, c); c.gridx = 2; c.gridy = 0; add(mButtonC, c); c.gridx = 3; c.gridy = 0; add(mButtonD, c); c.gridx = 4; c.gridy = 0; add(mButtonE, c); // align the text field from right edge GridBagConstraints txtCons = new GridBagConstraints(); txtCons.weightx = 0.5; // default is zero, all components will txtCons.fill = GridBagConstraints.HORIZONTAL; txtCons.anchor = GridBagConstraints.PAGE_END; txtCons.weighty = 1.0; // request some extra vertical space, default // zero txtCons.gridx = 4; txtCons.gridy = 1; add(mTxtFieldA, txtCons); txtCons.gridx = 3; txtCons.gridy = 1; add(mTxtFieldB, txtCons); txtCons.gridx = 2; txtCons.gridy = 1; add(mTxtFieldC, txtCons); // align the label } private void setUpListeners() { } public static void main(String[] args) { // UIManager settings /* * javax.swing.plaf.metal.MetalLookAndFeel : Metal * com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel : Nimbus * com.sun.java.swing.plaf.motif.MotifLookAndFeel : CDE/Motif * com.sun.java.swing.plaf.windows.WindowsLookAndFeel : Windows * com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel : Windows Classic */ try { LookAndFeelInfo[] mList = UIManager.getInstalledLookAndFeels(); for(LookAndFeelInfo info: mList) { System.out.println(info.getClassName() + " : " + info.getName()); } System.out.println("Java Default LookAndFeel is " + UIManager.getLookAndFeel().getName()); UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel"); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (UnsupportedLookAndFeelException e) { e.printStackTrace(); } // Create and set up the window. JFrame frame = new JFrame("GridBagLayout Demo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(new BorderLayout()); // custom icon iamge GridBagLayoutDemoPanel gridPanel = new GridBagLayoutDemoPanel(); java.net.URL imageURL = gridPanel.getClass().getResource("gridbag.jpg"); try { frame.setIconImage(ImageIO.read(imageURL)); } catch (IOException e) { System.err.println("An error occured when loading the image icon..."); } // Create and set up the content pane. JPanel subPanel = new JPanel(); subPanel.setLayout(new BorderLayout()); subPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); subPanel.add(gridPanel, BorderLayout.CENTER); frame.getContentPane().add(subPanel, BorderLayout.CENTER); // Display the window. frame.pack(); frame.setVisible(true); } }  

目录
相关文章
|
6月前
|
数据采集 Java
selenium+java入门demo
selenium+java入门demo
49 4
|
4月前
|
安全 Java API
|
4月前
|
Java
MQTT(EMQX) - Java 调用 MQTT Demo 代码
MQTT(EMQX) - Java 调用 MQTT Demo 代码
189 0
MQTT(EMQX) - Java 调用 MQTT Demo 代码
|
4月前
|
SQL Java 数据库连接
【Java】Java Swing 图书管借阅管理系统(源码+论文)【独一无二】
【Java】Java Swing 图书管借阅管理系统(源码+论文)【独一无二】
236 0
|
4月前
|
存储 数据可视化 Java
【Java】Java swing 民宿管理系统 GUI(源码+可视化界面)【独一无二】
【Java】Java swing 民宿管理系统 GUI(源码+可视化界面)【独一无二】
|
6月前
|
Java 数据安全/隐私保护
利用Java图形化界面组件Swing写一个简易的登录界面
利用Java图形化界面组件Swing写一个简易的登录界面
92 1
|
6月前
|
Java Windows
基于java Swing编写扫雷游戏设计实现(已调试)
基于java Swing编写扫雷游戏设计实现(已调试)
86 1
|
6月前
|
Java
Java Swing模拟水波纹扩散效果动画
Java Swing模拟水波纹扩散效果动画
60 6
|
6月前
|
Java
JAVA Swing自定义JScorllPanel
JAVA Swing自定义JScorllPanel
40 1
|
6月前
|
前端开发 Java UED
Java中的图形用户界面编程:Swing与JavaFX的比较与应用
Java中的图形用户界面编程:Swing与JavaFX的比较与应用
下一篇
DataWorks