*/ --------------------------------------------------------------------------------------
*/ 出自: 编程中国 http://www.bc-cn.net
*/ 作者: jdk2006
*/ 时间: 2007-7-31 编程论坛首发
*/ 声明: 尊重作者劳动,转载请保留本段文字
*/ --------------------------------------------------------------------------------------
这是我做的GUI,主要功能为显示图片.GUI的图形界面比较简易,功能也很简单
主要有新建、打开、退出和继续浏览按扭。希望大家以后多分享代码。
这些代码是在jdk1.6的版本测试的.
/*
* PictureFrame.java
*
* Created on 2007年7月29日, 下午11:09
*/
/**
*
* @author new
*/
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.io.*;
public class PictureFrame extends javax.swing.JFrame {
/** Creates new form PictureFrame */
public PictureFrame() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" 生成的代码 ">
private void initComponents() {
jPopupMenu1 = new javax.swing.JPopupMenu();
jPanel2 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenuItem2 = new javax.swing.JMenuItem();
jMenuItem4 = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
jMenuItem5 = new javax.swing.JMenuItem();
jMenuItem6 = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("PictureView Manager");
setBackground(new java.awt.Color(255, 255, 255));
setBounds(new java.awt.Rectangle(200, 50, 500, 500));
setMaximizedBounds(new java.awt.Rectangle(100, 200, 800, 800));
setState(1);
jPanel2.setEnabled(false);
jLabel1.setIcon(new javax.swing.ImageIcon("D:\\红影淡雅\\图片\\山水\\115.jpg"));
jLabel1.setBorder(javax.swing.BorderFactory.createMatteBorder(1, 1, 1, 1, new java.awt.Color(255, 0, 0)));
jButton1.setText("\u7ee7\u7eed\u6d4f\u89c8");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
JButton1Listener(evt);
}
});
jButton2.setText("\u53d6\u6d88\u6d4f\u89c8");
jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton2MouseClicked(evt);
}
});
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 417, Short.MAX_VALUE)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(88, 88, 88)
.addComponent(jButton1)
.addGap(60, 60, 60)
.addComponent(jButton2)
.addContainerGap(107, Short.MAX_VALUE))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 559, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addContainerGap())
);
jMenu1.setText("\u6587\u4ef6(F)");
jMenuItem1.setText("\u65b0\u5efa(N)");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
MenuItem1Listener(evt);
}
});
jMenu1.add(jMenuItem1);
jMenuItem2.setText("\u6253\u5f00(O)");
jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
MenuItem2Listener(evt);
}
});
jMenu1.add(jMenuItem2);
jMenuItem4.setText("\u9000\u51fa(E)");
jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
MenuItem4Listener(evt);
}
});
jMenu1.add(jMenuItem4);
jMenuBar1.add(jMenu1);
jMenu2.setText("\u5e2e\u52a9(H)");
jMenuItem5.setText("\u5173\u4e8e(about)");
jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
aboutListener(evt);
}
});
jMenu2.add(jMenuItem5);
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(15, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(42, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void aboutListener(java.awt.event.ActionEvent evt) {
// TODO 将在此处添加您的处理代码:
JOptionPane.showMessageDialog(jMenuItem5,"jdk2006制作,谢谢大家学习使用","PictureView",JOptionPane.YES_NO_OPTION);
}
private void MenuItem4Listener(java.awt.event.ActionEvent evt) {
// TODO 将在此处添加您的处理代码:
System.exit(1);
}
private void MenuItem2Listener(java.awt.event.ActionEvent evt) {
// TODO 将在此处添加您的处理代码:
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(jButton1);
filename=chooser.getSelectedFile().getName();
openPath=chooser.getCurrentDirectory().getPath();
System.out.println(openPath);
ImageIcon imag=new ImageIcon(openPath+"\\"+filename);
jLabel1.setIcon(imag);
}
private void MenuItem1Listener(java.awt.event.ActionEvent evt) {
// TODO 将在此处添加您的处理代码:
ImageIcon imag=new ImageIcon();
jLabel1.setIcon(imag);
}
private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {
// TODO 将在此处添加您的处理代码:
System.exit(1);
}
private void JButton1Listener(java.awt.event.ActionEvent evt) {
// TODO 将在此处添加您的处理代码:
File file=new File(openPath);
//String[] str=new String[openPath.length()];
String[] str=file.list();
str=file.list();
ImageIcon imag=new ImageIcon(openPath+"\\"+str[i]);
jLabel1.setIcon(imag);
i++;
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new PictureFrame().setVisible(true);
}
});
}
// 变量声明 - 不进行修改
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenuItem jMenuItem4;
private javax.swing.JMenuItem jMenuItem5;
private javax.swing.JMenuItem jMenuItem6;
private javax.swing.JPanel jPanel2;
private javax.swing.JPopupMenu jPopupMenu1;
private String filename;
private String openPath;
private static int i=0;
// 变量声明结束
}
[此贴子已经被作者于2007-7-31 23:45:29编辑过]