Java6学习笔记27——Swing的Look和Feel

1.Look指的是界面长什么样

Feel指的是它如何相应用户操作

2.平台相关性

1)Solaris或Linux(有GTK)——GTK+

2)Solaris或者Linux——Motif

3)AIX、HP、Windows或Mac——自带的GUI

3.Metal Look and Feel

Java自带的GUI

4.举例

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import java.io.File;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class SelectLookFeel extends JFrame implements ActionListener {
JFileChooser chooser;
public static void main(String arg[]) {
String laf;
UIManager.LookAndFeelInfo info[];
info = UIManager.getInstalledLookAndFeels();
for(int i=0; i<info.length; i++)
System.out.println(i + ". " + info[i].getClassName());//列出支持的OS的类型
System.out.print(" ");
int index = getIndex(info.length);
laf = info[index].getClassName();//取得Look and Feel的类名
try {
UIManager.setLookAndFeel(laf);//设置Look and Feel
} catch(ClassNotFoundException e) {
System.out.println("Exception: class not found");
} catch(InstantiationException e) {
System.out.println("Unable to instantiate class");
} catch(IllegalAccessException e) {
System.out.println("Illegal access");
} catch(UnsupportedLookAndFeelException e) {
System.out.println("Unsupported look and feel");
}
new SelectLookFeel();
}
public SelectLookFeel() {
JButton button;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocation(250,150);
Container pane = getContentPane();
pane.setLayout(new FlowLayout());
button = new JButton("Open");
button.addActionListener(this);
pane.add(button);
button = new JButton("Save");
button.addActionListener(this);
pane.add(button);
pack();

chooser = new JFileChooser(new File("."));
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

setVisible(true);
}
public void actionPerformed(ActionEvent e) {
int retval;
String selection = e.getActionCommand();
if(selection.equals("Open")) {
retval = chooser.showOpenDialog(this);
if(retval == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
System.out.println("File to open " + file);
}
} else if(selection.equals("Save")) {
File file = new File("mongrove.tiff");
chooser.setSelectedFile(file);
retval = chooser.showSaveDialog(this);
if(retval == JFileChooser.APPROVE_OPTION) {
file = chooser.getSelectedFile();
System.out.println("File to save " + file);
}
}
}
private static int getIndex(int max) {
byte b[] = new byte[1];

try {
System.in.read(b);//获取输入
} catch(Exception e) {
System.out.println(e);
}
int value = (int)b[0] - (int)'0';//得到输入的索引
if(value < 0)
return(0);
if(value > max)
return(max);
return(value);
}
}

你可能感兴趣的:(java,linux,swing,Solaris,AIX)