Java Swing框架介绍
Swing 是一个为Java设计的GUI工具包。(PC的一个UI库)
- Swing是JAVA基础类的一部分。
- Swing包括了图形用户界面(GUI)器件如:文本框,按钮,分隔窗格和表。
- Swing提供许多比AWT更好的屏幕显示元素。它们用纯Java写成,所以同Java本身一样可以跨平台运行,这一点不像AWT。它们是JFC的一部分。它们支持可更换的面板和主题(各种操作系统默认的特有主题),然而不是真的使用原生平台提供的设备,而是仅仅在表面上模仿它们。这意味着你可以在任意平台上使用JAVA支持的任意面板。轻量级组件的缺点则是执行速度较慢,优点就是可以在所有平台上采用统一的行为。
实际用法
简单的就是控件的调用,上代码
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.mycompany.dangeranimal.resource;
import java.util.List;
/**
*
*
*/
public class danger {
public static void main(String[] args) {
ColorTableCellRenderer ct = new ColorTableCellRenderer();
CsvManager csv = new CsvManager();
List<String> country_List = csv.ReadCsv();//读取CSV,获取国家名称
List<String> num_List = csv.ReadCsvGetNums();//读取CSV,获取数量名称
System.out.println("国家名列表:");
//f//or(int j = 0 ; j < country_List.size();j++)
//{
System.out.println(country_List);
//
DrawPicture dp = new DrawPicture();//绘制图像
dp.initWindow(country_List,num_List);
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.mycompany.dangeranimal.resource;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.*;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.BorderFactory;
import java.util.List;
import javax.swing.table.DefaultTableModel;
import java.util.Vector;
import java.net.URL;
import javax.swing.*;
import javax.swing.table.*;
/**
*
* @author
*/
public class DrawPicture {
//JF窗体设置
JFrame jf = new JFrame(); //设置全局变
JTable table = new JTable();
JPanel jp = new JPanel();
/*创建初始化显示界面*/
public void initWindow(List<String> country_list,List<String> num_list)
{
//设置窗体大小以及初始化尺寸,标题
jf.setLayout(new BorderLayout(10,5)); //默认为0,0;水平间距10,垂直间距5
jf.setMinimumSize(new Dimension(500,700)); //设置最小大小
jf.setMaximumSize(new Dimension(1500,1600)); //设置窗体最大大小
jf.setTitle("Elephas maximus In Asia"); //设置显示窗口标题
jf.setSize(850,700); //设置窗口显示尺寸
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //置窗口是否可以关;
//add title
Label jl = new Label("Elephas maximus In Asia",Label.CENTER);
jf.add(jl,BorderLayout.NORTH);//重新添加标题置顶
//设置表格相关信息
table = new JTable(7,6);//创建表格:参数:设置行列数
Vector vData = new Vector();
Vector vName = new Vector();
vName.add("column1");
vName.add("column2");
vName.add("column3");
vName.add("column4");
vName.add("column5");
vName.add("column6");
//add data to table
for(int i = 0; i <country_list.size();i++)
{
Vector vRow = new Vector();
vRow.add(" "+country_list.get(i));
ImageIcon elephant_icon = new ImageIcon("resource/elephant.png");//projects根目录下
ImageIcon empty_icon = new ImageIcon("resource/demo.jpeg");
//String转int 获取需要表示的数量
String cur_val =num_list.get(i);
int i_val = Integer.parseInt( cur_val);
int number = i_val/250;
int rest_num = 5-number;
for(int j = 0; j < number; j++)
{
vRow.add(elephant_icon);//添加图标
}
for(int k = 0; k < rest_num ; k++)
{
vRow.add(empty_icon);//添加图标
}
vData.add(vRow.clone());
}
DefaultTableModel model = new DefaultTableModel(vData, vName);
table.setModel(model);
table.setBorder(BorderFactory.createLineBorder(Color.BLACK,1));//设置边框颜色
table.setEnabled(false); //set not edittable
table.setShowHorizontalLines(true);
table.setShowVerticalLines(true);
table.setGridColor(Color.BLACK);//设置网格线颜色
table.setRowHeight(80);//设置行宽
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.getColumnModel().getColumn(0).setPreferredWidth(170);
table.getColumnModel().getColumn(1).setPreferredWidth(170);
table.getColumnModel().getColumn(2).setPreferredWidth(170);
table.getColumnModel().getColumn(3).setPreferredWidth(170);
table.getColumnModel().getColumn(4).setPreferredWidth(170);
table.getColumnModel().getColumn(5).setPreferredWidth(170);
table.getColumnModel().getColumn(1).setCellRenderer(new ImageRender());
table.getColumnModel().getColumn(2).setCellRenderer(new ImageRender());
table.getColumnModel().getColumn(3).setCellRenderer(new ImageRender());
table.getColumnModel().getColumn(4).setCellRenderer(new ImageRender());
table.getColumnModel().getColumn(5).setCellRenderer(new ImageRender());
table.setBackground(Color.YELLOW);
jf.add(table,BorderLayout.CENTER);//重新添加标题置顶
JLabel bottom_label = new JLabel("KEY");
JLabel description_label = new JLabel(" = 250 numbers");
JLabel l=new JLabel();
ImageIcon ji = new ImageIcon("resource/elephant.png");
l.setIcon(ji);
l.setBounds(10, 10, ji.getIconWidth(),ji.getIconHeight());
jp.add(bottom_label);
jp.add(l,new Integer(Integer.MIN_VALUE));
jp.add(description_label);
jp.setBackground(Color.YELLOW);
jf.add(jp,BorderLayout.SOUTH);
jf.getContentPane().setBackground(Color.YELLOW);
jf.setVisible(true); //设置窗口是否可见
}
}
//渲染前端
class ColorTableCellRenderer extends DefaultTableCellRenderer
{
DefaultTableCellRenderer renderer=new DefaultTableCellRenderer();
//该类继承与JLabel,Graphics用于绘制单元格,绘制红线
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2=(Graphics2D)g;
final BasicStroke stroke=new BasicStroke(2.0f);
g2.setColor(Color.RED);
g2.setStroke(stroke);
g2.drawLine(0,getHeight()/2,getWidth(),getHeight()/2);
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.mycompany.dangeranimal.resource;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
/**
* 重载TableCellRenderer的方法:使得Table可以添加图片
* @author
*/
public class ImageRender implements TableCellRenderer{
@SuppressWarnings("unchecked")
@Override
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected,
boolean hasFocus, int rowIndex, int columnIndex) {
if( value instanceof Image ){
JLabel jLabel = new JLabel();
jLabel.setLayout(new BorderLayout());//设置布局
jLabel.setIcon(new ImageIcon((Image)value));//给jlable设置图片
return jLabel;
}
else if( value instanceof File ) {
try {
return new JLabel(new ImageIcon(ImageIO.read((File)value)));
} catch(IOException ex) {
throw new RuntimeException(ex.getMessage(), ex);
}
}
else {
String val = String.valueOf(value);
try {
return new JLabel(new ImageIcon(ImageIO.read(new File(val))));
} catch(IOException ex) {
throw new RuntimeException(ex.getMessage(), ex);
}
}
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.mycompany.dangeranimal.resource;
import java.util.List;
/**
*
*
*/
public class danger {
public static void main(String[] args) {
ColorTableCellRenderer ct = new ColorTableCellRenderer();
CsvManager csv = new CsvManager();
List<String> country_List = csv.ReadCsv();//读取CSV,获取国家名称
List<String> num_List = csv.ReadCsvGetNums();//读取CSV,获取数量名称
System.out.println("国家名列表:");
//f//or(int j = 0 ; j < country_List.size();j++)
//{
System.out.println(country_List);
//
DrawPicture dp = new DrawPicture();//绘制图像
dp.initWindow(country_List,num_List);
}
}