JavaGUI-Swing
2015年12月31日
Swing继承AWT的事件机制、基础容器、布局,在容器中针对JVM编程,模拟不同平台的组件,实现所有平台的组件一致性,减少了平台依赖(基本容器各平台基本相同,但依然有依赖),但这需要更多的内存,速度会减慢。
Swing在AWT的基础上重写了所有的组件(在原组件的名字前加上J前缀(果然是Java的亲儿子)),扩展了原有的组件和形式(UIManager)。
AWT使用方法参见:JavaGUI-AWT.docx
JFrame f=new JFrame();
参考:http://blog.csdn.net/aeolus1019/article/details/7980950
/**
* Demo
*/
package lee;
importjava.awt.BorderLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.JButton;
importjavax.swing.JFrame;
/**
* @author [email protected]
*
*/
public class Main{
/**
*@param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame f=new JFrame();
f.setLayout(new BorderLayout());
f.setBounds(30,30,200,200);
f.setVisible(true);
f.setTitle("Hello,World!");
JButton btn1=newJButton("btn1");
f.add(btn1,BorderLayout.NORTH);
btn1.addActionListener(new ActionListener() {
@Override
public voidactionPerformed(ActionEvent e) {
// TODO Auto-generatedmethod stub
System.out.println("btn1,click");
}
});
f.pack();
}
}
UIManager.setLookAndFeel(newMetalLookAndFeel());
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
SwingUtilities.updateComponentTreeUI(f);
参考:http://tieba.baidu.com/p/1607650603
//Main.java
/**
* Demo
*/
package lee;
import java.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
importjavax.swing.event.*;
importjavax.swing.plaf.metal.MetalLookAndFeel;
importjavax.swing.plaf.nimbus.NimbusLookAndFeel;
/**
* @author [email protected]
*
*/
public class Main{
static int laf;
/**
*@param args
*/
public static void main(String[] args)
throwsUnsupportedLookAndFeelException {
// TODO Auto-generated method stub
//ew SwingComponent().init();
final JFrame f = new JFrame();
f.setLayout(new FlowLayout());
f.setBounds(30, 30, 200, 200);
f.setVisible(true);
f.setTitle("Hello,World!");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//get laf
for(UIManager.LookAndFeelInfoinfo:UIManager.getInstalledLookAndFeels()){
System.out.println(info.getName()+"="+info);
}
//view laf
JButton btn1 = newJButton("btn1");
f.add(btn1);
JCheckBox cb = newJCheckBox("cb");
f.add(cb);
JComboBox<String> combo = newJComboBox<>(new String[]{"aa","bb","cc"});
f.add(combo);
btn1.addActionListener(newActionListener() {
@Override
public voidactionPerformed(ActionEvent e) {
// TODO Auto-generatedmethod stub
try {
switch (laf) {
case 0:
UIManager.setLookAndFeel(newMetalLookAndFeel());
break;
case 1:
UIManager.setLookAndFeel(newNimbusLookAndFeel());
break;
case 2:
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
break;
case 3:
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
break;
case 4:
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
break;
default:
break;
}
} catch (UnsupportedLookAndFeelException| ClassNotFoundException | InstantiationException | IllegalAccessException e1){
// TODOAuto-generated catch block
e1.printStackTrace();
}
SwingUtilities.updateComponentTreeUI(f);
System.out.println(laf++);
if(5==laf){
laf=0;
}
}
});
f.pack();
}
}
Object:基类
-|Container:容器
--|Window
---|JWindow
---|JDialog
---|Frame
--|Panel
--|JComponent:基本组件
---|JButton等具体组件
---|JMenuBar:菜单栏
参考:http://blog.sina.com.cn/s/blog_90b03d9201015v05.html
保留原有的布局,增加了BoxLayout。
//Main.java
/**
* Demo
*/
package lee;
importjava.awt.BorderLayout;
importjava.awt.Button;
importjava.awt.CardLayout;
importjava.awt.FlowLayout;
importjava.awt.Frame;
import javax.swing.Box;
import javax.swing.BoxLayout;
/**
* @author [email protected]
*
*/
public class Main{
/**
*@param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Frame f=new Frame();
f.setLayout(new BorderLayout());
f.setBounds(30,30,200,200);
f.setVisible(true);
f.setTitle("Hello,World!");
Box hori=Box.createHorizontalBox();
hori.add(new Button("1"));
f.add(hori,BorderLayout.NORTH);
Box ver=Box.createVerticalBox();
ver.add(new Button("2"));
f.add(ver,BorderLayout.WEST);
Box ver2=Box.createVerticalBox();
ver2.add(new Button("3"));
ver2.add(new Button("4"));
ver2.add(new Button("5"));
f.add(ver2,BorderLayout.CENTER);
f.pack();
}
}
Action是ActionListener的子接口,一般使用Action的实现类AbstractAction(具有默认实现)作为直接基类。
class MyAction extends AbstractAction{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("MyAction1");
}
mya.putValue(Action.NAME,"xx");
mya.putValue(Action.SHORT_DESCRIPTION,"short_xx");
mya.putValue(Action.LONG_DESCRIPTION,"long_xx");
mya.putValue(Action.ACCELERATOR_KEY,KeyStroke.getKeyStroke('B',InputEvent.CTRL_MASK));
mya.putValue(Action.MNEMONIC_KEY,KeyEvent.VK_A);
mya.putValue(Action.DISPLAYED_MNEMONIC_INDEX_KEY,1);
btn1.setAction(mya);
//Main.java
/**
* Demo
*/
package lee;
import java.awt.*;
importjava.awt.event.*;
importjava.beans.PropertyChangeListener;
importjavax.swing.*;
/**
* @author [email protected]
*
*/
public class Main{
static int laf;
/**
*@param args
*/
public static void main(String[] args)
throwsUnsupportedLookAndFeelException {
// TODO Auto-generated method stub
//ew SwingComponent().init();
final JFrame f = new JFrame();
f.setLayout(new FlowLayout());
f.setBounds(30, 30, 200, 200);
f.setVisible(true);
f.setTitle("Hello,World!");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton btn1=new JButton();
f.add(btn1);
MyAction mya=new MyAction();
btn1.setAction(mya);
mya.putValue(Action.NAME,"xx");
mya.putValue(Action.SHORT_DESCRIPTION,"short_xx");
mya.putValue(Action.LONG_DESCRIPTION,"long_xx");
mya.putValue(Action.ACCELERATOR_KEY,KeyStroke.getKeyStroke('B',InputEvent.CTRL_MASK));
mya.putValue(Action.MNEMONIC_KEY,KeyEvent.VK_A);
mya.putValue(Action.DISPLAYED_MNEMONIC_INDEX_KEY,1);
JButton btn2=new JButton();
f.add(btn2);
btn2.setAction(mya);
//menu
JMenuBar mb = new JMenuBar();
f.setJMenuBar(mb);
JMenu m = newJMenu("File");
mb.add(m);
JMenuItem miOpen = new JMenuItem();
m.add(miOpen);
miOpen.setAction(mya);
f.pack();
}
}
class MyAction extends AbstractAction{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("MyAction1");
}
}
标签Name:用于设置Button或Menu的文本。,
快捷键:菜单时使用ACCELERATOR_KEY(CTRL+KEY),按钮时使用NMEMINC_KEY(ALT+KEY)。
快捷键助记符:显示NAME的第N(从0开始)个字符作为快捷键符。
Tooltip:SHORT_DESCRIPTION,鼠标悬浮时显示的文字。
//main.java
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/**
* Demo
*/
package lee;
import java.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
/**
* @author [email protected]
*
*/
public class Main{
static int laf;
/**
*@param args
*/
public static void main(String[] args)
throwsUnsupportedLookAndFeelException {
// TODO Auto-generated method stub
//ew SwingComponent().init();
final JFrame f = new JFrame();
f.setLayout(new FlowLayout());
f.setBounds(30, 30, 200, 200);
f.setVisible(true);
f.setTitle("Hello,World!");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar mb = new JMenuBar();
f.setJMenuBar(mb);
JMenu m = newJMenu("File");
mb.add(m);
JMenuItem miOpen = newJMenuItem("Open");
m.add(miOpen);
miOpen.setAccelerator(KeyStroke.getKeyStroke('O',InputEvent.CTRL_MASK));
miOpen.addActionListener(newActionListener() {
@Override
public voidactionPerformed(ActionEvent e) {
// TODO Auto-generatedmethod stub
System.out.println("Open");
}
});
f.pack();
}
}
paint()实现双缓冲,并实现对组件本身(paintComponent()),边框(paintBorder()),子组件(paintChildren())的顺序调用。所以一般重写paintComponent()进行组件重绘,重写paint()将破坏双缓冲,并有可能错误使用三个绘制函数。
参考:http://bbs.csdn.net/topics/390585720、Swing源码
class MyPanel extends JPanel{
@Override
protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
g.drawOval(0, 0, 100, 100);
g.drawString("abc", 50,50);
System.out.println("xx");
}
MyPanel p = new MyPanel();
p.setBounds(0, 0, 100, 100);
p.setLayout(null);
JButton btn1 = newJButton("xx");
btn1.setBounds(50, 0, 50, 30);
p.add(btn1);
JButton btn2 = newJButton("xx");
btn2.setBounds(0, 70, 50, 30);
p.add(btn2);
f.add(p);
//Main.java
package lee;
importjava.awt.Graphics;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
public class Main{
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame f=new JFrame();
f.setBounds(0, 0, 200, 200);
f.setTitle("Hello,World!");
f.setVisible(true);
f.setLayout(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyPanel p = new MyPanel();
p.setBounds(0, 0, 100, 100);
p.setLayout(null);
JButton btn1 = newJButton("xx");
btn1.setBounds(50, 0, 50, 30);
p.add(btn1);
JButton btn2 = newJButton("xx");
btn2.setBounds(0, 70, 50, 30);
p.add(btn2);
f.add(p);
}
}
class MyPanel extends JPanel{
@Override
protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
g.drawOval(0, 0, 100, 100);
g.drawString("abc", 50,50);
System.out.println("xx");
}
}
参考:http://blog.csdn.net/caixiexin/article/details/6626772
http://akalius.iteye.com/blog/155207
http://www.xuebuyuan.com/447206.html
可以绘制2D图形,抗锯齿等。
使用时由Graphics转换得到。
示例
//Main.java
package lee;
importjava.awt.Graphics;
importjava.awt.Graphics2D;
importjava.awt.RenderingHints;
importjava.awt.geom.Rectangle2D;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
public class Main{
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame f=new JFrame();
f.setBounds(0, 0, 200, 200);
f.setTitle("Hello,World!");
f.setVisible(true);
f.setLayout(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyPanel p = new MyPanel();
p.setBounds(0, 0, 100, 100);
p.setLayout(null);
JButton btn1 = newJButton("xx");
btn1.setBounds(50, 0, 50, 30);
p.add(btn1);
JButton btn2 = newJButton("xx");
btn2.setBounds(0, 70, 50, 30);
p.add(btn2);
f.add(p);
}
}
class MyPanelextends JPanel{
@Override
protected void paintComponent(Graphics g){
// TODO Auto-generated method stub
Graphics2D g2d = (Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2d.drawOval(0, 0, 100, 100);
g2d.drawString("abc", 50,50);
Rectangle2D.Float rect=new Rectangle2D.Float(20, 20, 50,50);
g2d.draw(rect);
System.out.println("xx");
}
}