在Java中使用Swing来设计图形用户界面(GUI),使用Swing开发的Java应用程序,界面不受本地系统的限制,移植到其他平台上界面不会改变。
Swing的基本器件包括窗体(JFrame),面板(JPanel),组件(标签,列表,按钮……),我们可以可以这样理解三者间的关系:GUI就像一张桌子摆满了丰盛食物,JFrame就是这张桌子,组件就是桌子上不同的食物,而JPanel就是摆放这些食物的餐具。餐具的意义是使纷繁复杂的“食物”摆放的井然有序,JPanel起到同样的作用。
摆放井然有序的桌面
Swing基本器件之间的关系
窗体
JFrame 架构窗体是一个容器,是Swing程序中各个组建的载体,可以将JFrame看作是承载这些Swing组件的容器。在开发应用程序时可以通过继承java.swing.JFrame 类创建一个窗体,在这个窗体中添加组建,同时为组件设置事件。由于该窗体继承了JFrame类,所以它拥有一些最大化、最小化、关闭的按钮。
JDialog窗体是Swing组件中的对话框,功能是从一个窗体弹出另一个窗体。JDialog窗体实质上就是另一种类型的窗体,它与JFrame窗体类似,JDialog窗体在使用时需要调用getContentPane()方法将窗体转换为容器,然后在容器中设置窗体的特性。
面板
面板是一个Swing容器,它可以作为容器容纳其他组件,但必须被添加到其他容器中。Swing常用的面板包括JPanel面板和JScrollPane面板。
JPanel面板可以聚集一些组件来布局,在设置界面时,可能会遇到在一个较小的容器中显示一个较大部分的内容,这时可以使用JScrollPane面板。JScrollPane面板是带滚动条的面板,它也是一种容器,但是JScrollPane只能放置一个组件,并且不可以使用布局管理器。如果需要在JScrollPane面板中放置多个组件,需要将多个组件放置在JPanel面板上,然后将JPanel面板作为一个整体组件添加在JScrollPane面板上。
标签组件与图标
标签由JLabel类定义,它的父类为JComponent类。标签可以显示一行只读文本、一个图像或者带图像的文本,它不产生任何类型的事件,只是简单地显示文本和图片。
按钮组件
按钮在Swing中是较为常见的组件,它用于出发特定动作。Swing中提交按钮由JButton对象表示,单选按钮组件由JRadioButton对象表示,复选框有JCheckBox类对象表示,
列表组件
Swing中提供了下拉列表框与列表框两种列表组件。下拉列表使用JComboBox类对象表示。列表框由JList类对象表示。
文本框组件
文本框又来显示或编辑一个单行文本,在Swing中通过JTextField类对象创建。
除此之外,还有密码框组件,文本域组件,表格组件,树组件等等。
下面我们以温度显示界面的应用来演示各个部分的用法:
WindowMunu类及main()方法
public class WindowMenu {
public static void main(String[] args) {
// TODO Auto-generated method stub
int frameWidth = 800; //frame宽度
int frameHeight = 800; //frame高度
int[] highTemperature = {28,27,32,30,31,27,27,27,28,28}; // 最高气温列表
int[] lowTemperature = {19,19,19,18,20,18,16,15,16,19}; // 最低气温列表
/*
* JFrame 界面设置
*/
JFrame windowmenu=new JFrame("温度显示界面");
windowmenu.setVisible(true);
windowmenu.setSize(frameWidth,frameHeight);
Toolkit kit = Toolkit.getDefaultToolkit(); //定义工具包
Dimension screenSize = kit.getScreenSize(); //获取屏幕的尺寸
int screenWidth = screenSize.width; //获取屏幕的宽
int screenHeight = screenSize.height; //获取屏幕的高
windowmenu.setLocation(screenWidth/2-frameWidth/2, screenHeight/2-frameHeight/2);//设置窗口居中显示
windowmenu.setResizable(false);
windowmenu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/*
* Panel1 格式设置
*/
textLabel label10 = new textLabel("当天最高温度",Color.BLACK,40);
textLabel label11 = new textLabel("°C",Color.BLACK,40);
numLabel label13 = new numLabel(String.valueOf(highTemperature[highTemperature.length-1]),Color.RED,50);
JPanel mypanel1 = new JPanel();
mypanel1.setLayout(new GridLayout(1,3,1,1));
mypanel1.add(label10);
mypanel1.add(label13);
mypanel1.add(label11);
/*
* Panel2 格式设置
*/
textLabel label20 = new textLabel("当天最低温度",Color.BLACK,40);
textLabel label21 = new textLabel("°C",Color.BLACK,40);
numLabel label23 = new numLabel(String.valueOf(lowTemperature[lowTemperature.length-1]),Color.BLUE,50);
JPanel mypanel2 = new JPanel();
mypanel2.setLayout(new GridLayout(1,2,1,1));
mypanel2.add(label20);
mypanel2.add(label23);
mypanel2.add(label21);
/*
* Panel3格式设置
* 将Panel1和Panel2放在一起构成Panel3
*/
JPanel mypanel3 = new JPanel();
mypanel3.setLayout(new GridLayout(2,1,1,1));
mypanel3.add(mypanel1);
mypanel3.add(mypanel2);
/*
* Panel4格式设置
* 在Panel4中放置历史温度曲线
*/
JPanel mypanel4 = new JPanel();
mypanel4.setLayout(new GridLayout(1,1,1,1));
DrawPicture picture = new DrawPicture(frameWidth,frameHeight,highTemperature,lowTemperature);
// 将Fram的尺寸作为picture尺寸的参考
mypanel4.add(picture);
/*
*温度显示布局
*/
windowmenu.setLayout(new GridLayout(0,1,5,5));
windowmenu.add(mypanel3);
windowmenu.add(mypanel4);
}
}
textLabel类
/*
* 绘制标签类
*/
public class textLabel extends JLabel {
private String str; //字符串
private Color color; //标签颜色
private int fontSize; //字体大小
public textLabel(){
}
public textLabel(String s, Color c, int f){
this.str = s;
this.color = c;
this.fontSize = f;
setText(str);
setFont(new Font(null,Font.PLAIN,fontSize));
setBackground(Color.BLACK);
setForeground(color);
}
}
numLabel类
/*
* 显示数值类
*/
public class numLabel extends JLabel {
private String str; // 需要显示的字符串
private Color color; //字体颜色
private int fontSize; //字体大小
//private String icon;
public numLabel(){
}
public numLabel(String s, Color c, int f){
this.str = s;
this.color = c;
this.fontSize = f;
//this.icon = "timg.jpg";
// JLabel label = new JLabel("°C",JLabel.CENTER);
setText(str);
setFont(new Font(null,Font.PLAIN,fontSize));
setBackground(Color.BLACK);
setForeground(color);
// setIcon(new ImageIcon(icon));
}
}
DrawPicture类
/*
* 绘制温度历史曲线类
*/
public class DrawPicture extends JPanel{
private int width; //JFrame面板宽度
private int height; //JFrame面板高度
private int offSet = 50; //位置偏置
private int[] highTemperature;// {28,27,32,30,31,27,27,27,28,28};
private int[] lowTemperature;// {19,19,19,18,20,18,16,15,16,19};
public DrawPicture(){
}
public DrawPicture(int w,int h,int[] hT, int[] lT){
this.width = w;
this.height = h;
this.highTemperature = hT;
this.lowTemperature = lT;
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.BLACK);
g.setFont(new Font(null,Font.BOLD,30));
g.drawLine(offSet, height/2-offSet, width-offSet, height/2-offSet); // x轴
g.drawLine(offSet, height/2-offSet, offSet, 0); //y轴
g.drawString("历史温度曲线", width/2-2*offSet, height/2-2*offSet);
for(int i=0;i<9;i++){
g.setColor(Color.RED);
g.setFont(new Font(null,Font.BOLD,30));
g.drawLine(i*70+50,300-highTemperature[i]*8,(i+1)*70+50,300-highTemperature[i+1]*8);
}
for(int i=0;i<9;i++){
g.setColor(Color.BLUE);
g.setFont(new Font(null,Font.BOLD,30));
g.drawLine(i*70+50,300-lowTemperature[i]*8,(i+1)*70+50,300-lowTemperature[i+1]*8);
}
}
}
输出结果
完
2018/9/16