当前位置:编程学习 > JAVA >>

哪位审美好得大神帮改改布局



package a;


import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

public class ScanPicture implements ActionListener, Runnable {
JFrame f;
JPanel bts;
JLabel pl;
JTextArea jta;
JTextField tField;
JScrollPane jsp;
JButton cf, start, next, prev, stop,sure,biaoshi,shuru;
JFileChooser fc;
File[] sf;
int index;
Thread auto;
boolean autoFlag;
int delay = 3 * 1000;
int o;
// 合法的文件后缀
String[] suf = { "1.JPG", "2.JPG", "3.JPG","4.JPG" };
// 这里就是GUI布局
ScanPicture() {
pl = new JLabel();
pl.setHorizontalAlignment(JLabel.CENTER);
jsp = new JScrollPane(pl);
next = new JButton("》");
prev = new JButton("《");
sure = new JButton("确定");
biaoshi = new JButton("单价五元,不设找零");
shuru = new JButton("请输入钱数!");
    tField= new JTextField();
biaoshi.setLocation(10,10);
bts = new JPanel(new FlowLayout(FlowLayout.CENTER));
// bts.setLayout(null);
bts.setSize(500,800);
bts.setLocation(5,30);
bts.setBackground(Color.red);
bts.add(prev);
bts.add(next);
bts.add(sure);
bts.add(biaoshi);
bts.add(shuru);
bts.add(tField);
cf = new JButton("请选择要查看图片的位置!");
fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
f = new JFrame();
jta = new JTextArea();
f.setDefaultCloseOperation(3);
f.getContentPane().add(cf, "North");//标题
f.getContentPane().add(jsp, "Center");//图片
f.getContentPane().add(bts, "East");
f.getContentPane().add(jta, "South");
f.setSize(400, 300);
f.setLocationRelativeTo(null);
f.setVisible(true);
// 给按钮加入事件侦听器
next.addActionListener(this);
prev.addActionListener(this);
sure.addActionListener(this);
biaoshi.addActionListener(this);
cf.addActionListener(this);
auto = new Thread(this);
auto.start();
}

public static void main(String[] args) {
try {
UIManager
.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception e) {
try {
UIManager.setLookAndFeel(UIManager
.getSystemLookAndFeelClassName());
} catch (Exception e2) {
}
}
new ScanPicture();
}

// 处理各按键事件
public void actionPerformed(ActionEvent e) {
Object src = e.getSource();
if (src == cf) {
int o = fc.showOpenDialog(f);
if (o == JFileChooser.APPROVE_OPTION) {
sf = fc.getSelectedFile().listFiles(new FilenameFilter() {


public boolean accept(File dir, String name) {
name = name.toUpperCase();
for (int i = 0; i < suf.length; i++)
if (name.endsWith(suf[i]))
return true;
return false;
}
});
if (sf.length > 0) {
index = 0;
showPic();
}
}
}
if (sf == null || sf.length == 0)
return;
else if (src == next)
next();
else if (src == prev)
prev();
else if (src == sure)
sure();
}

void prev() {
index = --index < 0 ? sf.length - 1 : index;
showPic();
}

void next() {
index = ++index > sf.length - 1 ? 0 : index;
showPic();
}
void sure(){
if (sf == null || sf.length == 0){
return;
}else{

// for (int i = 0; i < suf.length; i++){
// System.out.println(sf[index].getName());
     jta.setText("选择了图片:"+sf[index].getName());
    System.out.println (bts.getLocation());
    System.out.print(biaoshi.getLocation()+"+"+sure.getLocation()+"+"+prev.getLocation());
//}
}
}

public void run() {
while (true) {
if (sf != null && sf.length > 0 && autoFlag) {
try {
Thread.sleep(delay);
} catch (Exception e) {
}
next();
}
try {
Thread.sleep(100);
} catch (Exception e) {
}
}
}


// 显示图片
private void showPic() {
if (sf == null || sf.length == 0)
return;
pl.setIcon(new ImageIcon(sf[index].getAbsolutePath()));

}
}
--------------------编程问答-------------------- 好久没用swing
补充:Java ,  Eclipse
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,