Swing功能更强大:
1:组件设置提示信息 setToolTipText(str);
windows.setToolTipText("设置窗体风格为windows风格");
Metal.setToolTipText("设置窗体风格为Metal风格");
Motif.setToolTipText("设置窗体风格为Motif风格");
2:每一个组件都可以设置一个Icon
Icon image=new ImageIcon("ico/bomb.gif");
3:可以设置窗体的风格:
* 2.0:准许改变窗体的风格
* JFrame.setDefaultLookAndFeelDecorated(true);
* 2.1:设置风格UIManager.setLookAndFeel(风格);
2.2更新SwingUtilities.updateComponentTreeUI(f.getContentPane());
4:可以设计边框 更好的布局
Box topLeft=Box.createVerticalBox();
Box top=Box.createHorizontalBox();
demo:
/**
*
*/
package import_Jframe_view;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
/**
* 窗体的风格 和各组件的应用实例
* 1:当获得焦点的时候给予提示setToolTipText(“string”)
* 2:UIManager manages the current look and feel,
*
* 2.0:准许改变窗体的风格
* JFrame.setDefaultLookAndFeelDecorated(true);
* 2.1:设置风格UIManager.setLookAndFeel(风格);
* 2.2:更新组件SwingUtilities.updateComponentTreeUI(f.getContentPane());
* 2014-12-30
* @author LiTing
*
*/
public class CommonComponent implements ActionListener{
JFrame f=new JFrame("常用组件使用");
Icon image=new ImageIcon("ico/bomb.gif");
JButton Metal=new JButton("Metal");
JButton windows=new JButton("windows");
JButton Motif=new JButton("Motif");
ButtonGroup bg=new ButtonGroup();
JRadioButton male=new JRadioButton("男",true);
JRadioButton female=new JRadioButton("女",false);
JCheckBox married=new JCheckBox("是否已婚",false);
//下拉式选项框
String[] str=new String[]{"蓝色","绿色","红色"};
JComboBox colorChooser =new JComboBox (str);
JList colorList=new JList(str);
//定义5行20列的多行文本框
JTextArea ta=new JTextArea(5,20);
JTextField name=new JTextField(20);
public void init()
{
/**
* 创建一个panel 装载 按钮和文本
*/
JPanel bottom=new JPanel();
/**
* 当获得焦点的时候给予提示setToolTipText(“string”)
*/
windows.setToolTipText("设置窗体风格为windows风格");
Metal.setToolTipText("设置窗体风格为Metal风格");
Motif.setToolTipText("设置窗体风格为Motif风格");
bottom.add(name);
bottom.add(windows);
bottom.add(Metal);
bottom.add(Motif);
f.add(bottom,BorderLayout.SOUTH);//放置在最底下;
/**
* 创建panel装载下拉选择框 checkbox
*/
JPanel checkPanel=new JPanel();
checkPanel.add(colorChooser);
bg.add(male);
bg.add(female);
checkPanel.add(female);
checkPanel.add(male);
checkPanel.add(married);
Box topLeft=Box.createVerticalBox();
JScrollPane taJsp=new JScrollPane(ta);
topLeft.add(taJsp);
topLeft.add(checkPanel);
Box top=Box.createHorizontalBox();
top.add(topLeft);
top.add(colorList);
f.add(top);
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
//增加监听
windows.addActionListener(this);
Metal.addActionListener(this);
Motif.addActionListener(this);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//准许改变窗体的风格
JFrame.setDefaultLookAndFeelDecorated(true);
CommonComponent c=new CommonComponent();
c.init();
}
private void changeStyle(int style) throws Exception{
switch(style)
{
case 1://windows风格
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
break;
case 2://metal风格
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
break;
case 3://motif风格
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
break;
}
/**
* 记得要更新组件
*/
SwingUtilities.updateComponentTreeUI(f.getContentPane());
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
if(e.getSource()==windows)
{
changeStyle(1);
}
else if(e.getSource()==Metal)
{
changeStyle(2);
}
else if(e.getSource()==Motif)
{
changeStyle(3);
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}