想要在图形界面中放置图片,需要使用到ImageIcon
上面的界面使用到了窗口拆分JSplitPane(属于容器类组件)。将窗口拆分成了两列。一列是JList, 里面输入
了一些文字,一列是JLable,上面放置了图片。布局采用的是BorderLayout
package gui;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSplitPane;
import javax.swing.JList;
import javax.swing.ImageIcon;
/**
* swing 实战窗口拆分
* Created by admin on 2017/7/10.
*/
public class JsplitPane extends JFrame{
private JSplitPane jSplitPane;
private JLabel jLabel;
private JList jList;
public static void main(String[] args){
JsplitPane jsplitPane = new JsplitPane();
}
public JsplitPane(){
String [] words = {"Java", "Python", "Golang"};
jLabel = new JLabel(new ImageIcon("D:\\image\\QQ图片20150507223217.jpg"));
jList = new JList(words);
// JSplitPane 拆分窗格,垂直拆分方式
jSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jList, jLabel);
this.add(jSplitPane);
//设置JFrame属性
this.setTitle("工程");
this.setLocation(500, 250);
this.setSize(350, 200);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}