java應用色彩選擇器示例分享。本站提示廣大學習愛好者:(java應用色彩選擇器示例分享)文章只能為提供參考,不一定能成為您想要的結果。以下是java應用色彩選擇器示例分享正文
package com.liuxing.test;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class ColorChooser extends JFrame {
private JLabel sampleText = new JLabel("Label");
private JButton chooseButton = new JButton("Choose Color");
public static void main(String[] args) {
new ColorChooser();
}
public ColorChooser() {
this.setSize(300, 100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel1 = new JPanel();
sampleText.setBackground(null);
panel1.add(sampleText);
chooseButton.addActionListener(new ButtonListener());
panel1.add(chooseButton);
this.add(panel1);
this.setVisible(true);
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Color c = JColorChooser.showDialog(null, "Choose a Color", sampleText.getForeground());
if (c != null)
sampleText.setForeground(c);
}
}
}
Python在讀取一個文件時,會記住其在文件中的地位,假如第二次仍須要從頭讀取,則須要挪用f.seek(0)從新從頭開端讀取。
一些例子:
>>> f = open('hi.txt','w') >>> f.closed False >>> f.mode 'w' >>> f.name 'hi.txt' >>> f.encoding
緊縮息爭緊縮文件(zip/unzip)
1,單個文件緊縮成zip文件
#!/usr/bin/python import zipfile f = zipfile.ZipFile('archive.zip','w',zipfile.ZIP_DEFLATED) f.write('1.py') f.write('/root/install.log') f.close()
細心不雅察緊縮今後的archive.zip,外面有一個1.py和一個root的目次,root目次下有一個install.log
ZIP_DEFLATED是緊縮標記,假如應用它須要編譯了zlib模塊,假如僅僅是打包而不緊縮的話,可以改成zipfile.ZIP_STORED
2,把zip文件解緊縮
#!/usr/bin/python import zipfile zfile = zipfile.ZipFile('archive.zip','r') for filename in zfile.namelist(): data = zfile.read(filename) file = open(filename, 'w+b') file.write(data) file.close()
假如archive.zip裡有目次,則在以後目次下也應當存在對應的目次,不然會報錯。
3,把全部文件夾緊縮
#!/usr/bin/python import zipfile import os f = zipfile.ZipFile('archive.zip','w',zipfile.ZIP_DEFLATED) startdir = "c:\\\\mydirectory" for dirpath, dirnames, filenames in os.walk(startdir): for filename in filenames: f.write(os.path.join(dirpath,filename)) f.close()
假如湧現:
Compression requires the (missing) zlib module
處理辦法:
yum install zlib zlib-devel
,然後從新編譯裝置python