Java Swing JLabel、JPanel、JList、窗口放大缩小、最小化
父Panel设置背景图:
jPanel1 = new JPanel() {
public void paintComponent(Graphics g) {
ImageIcon icon = new ImageIcon(this.getClass().getClassLoader().getResource("images/top_title_bg.png"));
// 图片随窗体大小而变化
g.drawImage(icon.getImage(), 0, 0, this.getSize().width,this.getSize().height,this);
}
};
子Panel透明:
jPanel3.setBackground(null); jPanel3.setOpaque(false);
JLabel的背景颜色:
jLabel.setOpaque(true); jLabel.setBackground(Color.RED);
Swing JList 添加JLabel(Renderer):
jList1 = new javax.swing.JList();
DefaultListModel dlm = new DefaultListModel();
dlm.addElement(new JLabel("yzmm", new ImageIcon(this.getClass().getClassLoader().getResource("images/menu_bar_home_icon.png")), SwingConstants.CENTER));
dlm.addElement(new JLabel("我的所有文件", new ImageIcon(this.getClass().getClassLoader().getResource("images/menu_bar_file_icon.png")), SwingConstants.CENTER));
dlm.addElement(new JLabel("桌面", new ImageIcon(this.getClass().getClassLoader().getResource("images/menu_bar_desktop_icon.png")), SwingConstants.CENTER));
jList1.setCellRenderer(new DefaultListCellRenderer(){
private static final long serialVersionUID = 1L;
@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
if (value instanceof JLabel) {
this.setText(((JLabel) value).getText());
this.setIcon(((JLabel) value).getIcon());
}
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
} else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
return this;
}
});
jList1.setModel(dlm);
jList1.setBorder(null);
jList1.setBackground(null);
jList1.setOpaque(false);
jScrollPane1.setViewportView(jList1);
可拖动:
addMouseListener(new MouseAdapter() {
/**
* 按下(mousePressed 不是点击,而是鼠标被按下没有抬起
*/
public void mousePressed(MouseEvent e) {
origin.x = e.getX(); //当鼠标按下的时候获得窗口当前的位置
origin.y = e.getY();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
/**
* 拖动(mouseDragged 指的不是鼠标在窗口中移动,而是用鼠标拖动)
*/
public void mouseDragged(MouseEvent e) {
Point p = getLocation();//当鼠标拖动时获取窗口当前位置
//设置窗口的位置
//窗口当前的位置 + 鼠标当前在窗口的位置 - 鼠标按下的时候在窗口的位置
setLocation(p.x + e.getX() - origin.x, p.y + e.getY() - origin.y);
}
});
居中(初始化末尾处加):
int screenWidth = java.awt.Toolkit.getDefaultToolkit().getScreenSize().width; int screenHeight = java.awt.Toolkit.getDefaultToolkit().getScreenSize().height; Dimension d = getPreferredSize(); this.setBounds(screenWidth / 2 - (d.width/2), screenHeight / 2 - (d.height/2), d.width, d.height);
窗口放大缩小:
Dimension d1 = this.getContentPane().getSize();
Dimension d2 = this.getPreferredSize();
if(!d1.equals(d2)){
this.setExtendedState(JFrame.NORMAL);
}else{
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
}
最小化:
this.setExtendedState(JFrame.ICONIFIED);文件选择:
public void setSelectFile(JTextField jf) {
int result = 0;
String path = null;
JFileChooser fileChooser = new JFileChooser();
FileSystemView fsv = FileSystemView.getFileSystemView();
fileChooser.setCurrentDirectory(fsv.getHomeDirectory());
fileChooser.setDialogTitle("请选择保存路径.");
fileChooser.setApproveButtonText("确定");
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
result = fileChooser.showOpenDialog(fileChooser);
if (JFileChooser.APPROVE_OPTION == result) {
path = fileChooser.getSelectedFile().getPath();
}
jf.setText(path);
}