2、基于swing包的新型窗体程序
2、1 窗体的建立
先看一个最为简单的窗体例子,如:
import javax.swing.*;
import java.awt.*;
public class exec
{
public static void main(String args[])
{
JFrame f=new JFrame("This is a window!");
f.setLocation(100,200);
f.setSize(200,400);
FlowLayout fl=new FlowLayout();
f.getContentPane().setLayout(fl);
JButton[] jb=new JButton[10];
for(int i=0;i<jb.length;i++)
{
jb=new JButton();
jb.setText(String.valueOf(i));
f.getContentPane().add(jb);
}
f.setVisible(true);
}
}
请注意和上述awt窗体的异同点:
1)导入的包不一样。
2)swing组件类名称和awt组件类名称相比,往往都是多个大写的“J”开头。
3)不再直接向窗体添加组件,而是通过内容面板间接添加组件,所以,在添加组件的时候,需要取得当前窗体的内容面板。
4)传统awt组件的一些方法不再鼓励使用,相反,可以使用具有相似功能的新的方法,如按钮的setLabel()方法被setText()取代,当然,传统方法仍然可以使用。这些可以在编译Java程序时,通过指定-deprecation参数来查看相关信息,如对于使用按钮setLabel()方法的swing程序,编译时键入javac -deprecation exec.java,显示警告信息为:
exec.java:22: warning: setLabel(java.lang.String) in javax.swing.AbstractButton
has been deprecated
jb.setLabel(String.valueOf(i));
^
1 warning
2、2 常用swing组件的使用
Swing组件是AWT组件的扩展,它提供了更强大和更灵活的组件集合。除了那些常用组件如按钮、复选框和标签外,Swing还包括许多新的组件,如选项板、滚动窗口、树、表格。同时,即使是常用组件,如按钮等,在Swing都增加了新功能。另外,与AWT组件不同,Swing组件实现不包括任何与平台相关的代码。Swing组件是纯Java代码,因此与平台无关。一般用轻量级(lightweight)这个术语描述这类组件。
Swing组件还能够提供可插入的外观感觉,这意味着可以方便替换一个元素的外观和行为。这种替换可以动态实现。用户甚至可以设计自己的外观和感觉。在不久的将来,利用Swing机制实现GUI组件将完全取代AWT类。
2、2、1 swing标签的使用
import javax.swing.*;
import java.awt.*;
public class exec
{
public static void main(String [] args)
{
JFrame f=new JFrame("Hello!");
f.setLocation(10,100);
f.setSize(600,400);
f.getContentPane().setLayout(new FlowLayout());
JLabel jl = new JLabel("France");
f.getContentPane().add(jl);
f.setVisible(true);
}
}
注意:可以给标签添加图片,如:
import javax.swing.*;
import java.awt.*;
public class exec
{
public static void main(String [] args)
{
JFrame f=new JFrame("Hello!");
f.setLocation(10,100);
f.setSize(600,400);
f.getContentPane().setLayout(new FlowLayout());
ImageIcon ii = new ImageIcon("e.gif");
JLabel jl = new JLabel("France", ii, JLabel.CENTER);
f.getContentPane().add(jl);
f.setVisible(true);
}
}
2、2、2 swing文本框的使用
import javax.swing.*;
import java.awt.*;
public class exec
{
public static void main(String [] args)
{
JFrame f=new JFrame("Hello!");
f.setLocation(10,100);
f.setSize(600,400);
f.getContentPane().setLayout(new FlowLayout());
JTextField tf = new JTextField("France");
tf.setColumns(10);//设置显示的列数
f.getContentPane().add(tf);
f.setVisible(true);
f.pack();//使得窗体自动调整大小,以正好显示一个文本框
}
}
注意:
JPasswordField类派生于JTextField,可以提供密码的输入方式,如:
import javax.swing.*;
import java.awt.*;
public class exec
{
public static void main(String [] args)
{
JFrame f=new JFrame("Hello!");
f.setLocation(10,100);
f.setSize(600,400);
f.getContentPane().setLayout(new FlowLayout());
JPasswordField pf = new JPasswordField("France");
pf.setColumns(10);//设置显示的列数
f.getContentPane().add(pf);
f.setVisible(true);
f.pack();//使得窗体自动调整大小,以正好显示一个文本框
}
}
2、2、3 swing按钮的使用
import javax.swing.*;
import java.awt.*;
public class exec
{
public static void main(String [] args)
{
JFrame f=new JFrame("Hello!");
f.setLocation(10,100);
f.setSize(600,400);
f.getContentPane().setLayout(new FlowLayout());
JButton jb = new JButton("Ok");
f.getContentPane().add(jb);
f.setVisible(true);
}
}
注意:可以给按钮添加图片,如:
import javax.swing.*;
import java.awt.*;
public class exec
{
public static void main(String [] args)
{
JFrame f=new JFrame("Hello!");
f.setLocation(10,100);
f.setSize(600,400);
f.getContentPane().setLayout(new FlowLayout());
ImageIcon ii = new ImageIcon("e.gif");
JButton jb = new JButton(ii);
f.getContentPane().add(jb);
f.setVisible(true);
}
}
2、2、4 swing复选钮的使用
import javax.swing.*;
import java.awt.*;
public class exec
{
public static void main(String [] args)
{
JFrame f=new JFrame("Hello!");
f.setLocation(10,100);
f.setSize(600,400);
f.getContentPane().setLayout(new FlowLayout());
JCheckBox cb1 = new JCheckBox("Java");
JCheckBox cb2 = new JCheckBox("C++");
f.getContentPane().add(cb1);
f.getContentPane().add(cb2);
f.setVisible(true);
}
}
注意:可以给复选钮添加图片,如:
import javax.swing.*;
import java.awt.*;
public class exec
{
public static void main(String [] args)
{
JFrame f=new JFrame("Hello!");
f.setLocation(10,100);
f.setSize(600,400);
f.getContentPane().setLayout(new FlowLayout());
ImageIcon ii1 = new ImageIcon("p1.gif");
ImageIcon ii2 = new ImageIcon("p2.gif");
ImageIcon ii3 = new ImageIcon("p3.gif");
JCheckBox cb1 = new JCheckBox("Java",ii1);
JCheckBox cb2 = new JCheckBox("C++",ii1);
cb1.setSelectedIcon(ii2);
cb1.setRolloverIcon(ii3);
cb2.setSelectedIcon(ii2);
cb2.setRolloverIcon(ii3);
f.getContentPane().add(cb1);
f.getContentPane().add(cb2);
f.setVisible(true);
}
}
注意:
复选钮在构造函数中的图片对象是指默认显示的图片,而setSelectedIcon()方法的功能为设置选中复选框时显示的图片,setRolloverIcon()方法的功能为设置鼠标经过复选框上方时显示的图片。
2、2、5 swing单选钮的使用
import javax.swing.*;
import java.awt.*;
public class exec
{
public static void main(String [] args)
{
JFrame f=new JFrame("Hello!");
f.setLocation(10,100);
f.setSize(600,400);
f.getContentPane().setLayout(new FlowLayout());
JRadioButton rb1 = new JRadioButton("Java");
JRadioButton rb2 = new JRadioButton("C++");
ButtonGroup bg = new ButtonGroup();
bg.add(rb1);
bg.add(rb2);
f.getContentPane().add(rb1);
f.getContentPane().add(rb2);
f.setVisible(true);
}
}
注意:
1)单选钮和复选钮的区别主要在于它一次只能选择一个,所以需要指定单选钮组来管理成为一个选项组的所有单选钮。
2)可以给单选钮添加图片,如:
import javax.swing.*;
import java.awt.*;
public class exec
{
public static void main(String [] args)
{
JFrame f=new JFrame("Hello!");
f.setLocation(10,100);
f.setSize(600,400);
f.getContentPane().setLayout(new FlowLayout());
ImageIcon ii1 = new ImageIcon("p1.gif");
ImageIcon ii2 = new ImageIcon("p2.gif");
ImageIcon ii3 = new ImageIcon("p3.gif");
JRadioButton rb1 = new JRadioButton("Java",ii1);
JRadioButton rb2 = new JRadioButton("C++",ii1);
rb1.setRolloverIcon(ii3);
rb1.setSelectedIcon(ii2);
rb2.setRolloverIcon(ii3);
rb2.setSelectedIcon(ii2);
ButtonGroup bg = new ButtonGroup();
bg.add(rb1);
bg.add(rb2);
f.getContentPane().add(rb1);
f.getContentPane().add(rb2);
f.setVisible(true);
}
}
2、2、6 swing组合框的使用
import javax.swing.*;
import java.awt.*;
public class exec
{
public static void main(String [] args)
{
JFrame f=new JFrame("Hello!");
f.setLocation(10,100);
f.setSize(600,400);
f.getContentPane().setLayout(new FlowLayout());
JComboBox jc = new JComboBox();
jc.addItem("France");
jc.addItem("Germany");
jc.addItem("Italy");
jc.addItem("Japan");
f.getContentPane().add(jc);
f.setVisible(true);
}
}
2、2、7 swing页面框的使用
import javax.swing.*;
import java.awt.*;
public class exec
{
public static void main(String [] args)
{
JFrame f=new JFrame("Hello!");
f.setLocation(10,100);
f.setSize(600,400);
f.getContentPane().setLayout(new BorderLayout());
JTabbedPane jtp = new JTabbedPane();
Panel p1= new Panel();
p1.setBackground(Color.red);
p1.add(new JButton("red"));
jtp.addTab("red",p1);
Panel p2= new Panel();
p2.setBackground(Color.green);
p2.add(new JButton("green"));
jtp.addTab("green", p2);
Panel p3= new Panel();
p3.setBackground(Color.blue);
p3.add(new JButton("blue"));
jtp.addTab("blue", p3);
f.getContentPane().add(jtp,BorderLayout.CENTER);
f.setVisible(true);
}
}
注意:在页面框中,每个页面都可以通过面板对象将更多的组件加入当前页面。
2、2、8 swing滚动条的使用
import javax.swing.*;
import java.awt.*;
public class exec
{
public static void main(String [] args)
{
JFrame f=new JFrame("Hello!");
f.setLocation(10,100);
f.setSize(600,400);
f.getContentPane().setLayout(new BorderLayout());
JPanel jp = new JPanel();
jp.setLayout(new GridLayout(20, 20));
for(int i = 0; i < 400; i++)
{
jp.add(new JButton("Button " + i));
}
int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp = new JScrollPane(jp, v, h);
f.getContentPane().add(jsp, BorderLayout.CENTER);
f.setVisible(true);
}
}
注意:滚动条作为一个不能独立使用的组件,一般要和其他组件结合才能有效的发挥功能,如给文本框、表格或者窗体添加滚动条,其中给窗体添加滚动条的做法是让窗体包含一个具有滚动条的面板。
2、2、9 swing表格的使用
import javax.swing.*;
import java.awt.*;
public class exec
{
public static void main(String [] args)
{
JFrame f=new JFrame("Hello!");
f.setLocation(10,100);
f.setSize(600,400);
f.getContentPane().setLayout(new BorderLayout());
String[] colHeads = { "Name", "Phone", "Fax" };
String[][] data =
{
{ "Gail", "4567", "8675" },
{ "Ken", "7566", "5555" },
{ "Viviane", "5634", "5887" },
{ "Melanie", "7345", "9222" },
{ "Anne", "1237", "3333" },
{ "John", "5656", "3144" },
{ "Matt", "5672", "2176" },
{ "Claire", "6741", "4244" },
{ "Erwin", "9023", "5159" },
{ "Ellen", "1134", "5332" },
{ "Jennifer", "5689", "1212" },
{ "Ed", "9030", "1313" },
{ "Helen", "6751", "1415" }
};
JTable table = new JTable(data, colHeads);
int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp = new JScrollPane(table, v, h);
f.getContentPane().add(jsp, BorderLayout.CENTER);
f.setVisible(true);
}
}
注意:
滚动条的添加更有助于优化界面
2、2、10 swing树状视图的使用
import javax.swing.*;
import java.awt.*;
import javax.swing.tree.*;
public class exec
{
public static void main(String [] args)
{
JFrame f=new JFrame("Hello!");
f.setLocation(10,100);
f.setSize(600,400);
f.getContentPane().setLayout(new BorderLayout());
DefaultMutableTreeNode top = new DefaultMutableTreeNode("Options");
DefaultMutableTreeNode a = new DefaultMutableTreeNode("A");
top.add(a);
DefaultMutableTreeNode a1 = new DefaultMutableTreeNode("A1");
a.add(a1);
DefaultMutableTreeNode a2 = new DefaultMutableTreeNode("A2");
a.add(a2);
DefaultMutableTreeNode b = new DefaultMutableTreeNode("B");
top.add(b);
DefaultMutableTreeNode b1 = new DefaultMutableTreeNode("B1");
b.add(b1);
DefaultMutableTreeNode b2 = new DefaultMutableTreeNode("B2");
b.add(b2);
DefaultMutableTreeNode b3 = new DefaultMutableTreeNode("B3");
b.add(b3);
JTree tree = new JTree(top);
f.getContentPane().add(tree, BorderLayout.CENTER);
f.setVisible(true);
}
}
[此贴子已经被作者于2010-12-12 07:56:46编辑过]