Swing代码(5)

2019-05-17 19:53

showImg.setIcon(imgs[counter]); counter++; showImg.repaint(); } } }

例6-16 TimerAndJProgressBar.java

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

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

public class TimerAndJProgressBar extends JFrame implements ActionListener, ChangeListener { JPanel p = new JPanel(); JLabel msg = new JLabel(\ JProgressBar pgbar = new JProgressBar(); Timer timer = new Timer(100, this); JButton start = new JButton(\开始\ JButton stop = new JButton(\停止\

public TimerAndJProgressBar() { Container c = this.getContentPane(); Box hbox = Box.createHorizontalBox(); p.add(hbox); hbox.add(start); hbox.add(hbox.createHorizontalStrut(20)); hbox.add(stop); c.add(p, BorderLayout.NORTH); c.add(pgbar, BorderLayout.CENTER); c.add(msg, BorderLayout.SOUTH); pgbar.setStringPainted(true); pgbar.setString(\ pgbar.setPreferredSize(new Dimension(200, 30)); pgbar.setBorderPainted(false); pgbar.setBackground(Color.white); pgbar.setForeground(Color.black); pgbar.addChangeListener(this); start.addActionListener(this); stop.addActionListener(this); this.setTitle(\ this.setBounds(200, 200, 300, 140); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true);

}

public static void main(String[] args) { new TimerAndJProgressBar(); }

public void actionPerformed(ActionEvent e) { if (e.getSource() == start) { timer.start(); } else if (e.getSource() == stop) { timer.stop(); } else if (e.getSource() == timer) { int value = pgbar.getValue(); if (value < 100) { value++; pgbar.setValue(value); } else { timer.stop(); pgbar.setValue(0); } } }

public void stateChanged(ChangeEvent e) { int value = pgbar.getValue();

if (e.getSource() == pgbar) { msg.setText(\目前已完成:\

}

} }

例6-17 JFileChooserDemo.java

import java.awt.*;

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

public class JFileChooserDemo extends JFrame implements ActionListener { JLabel msg = new JLabel(\ JTextArea ta = new JTextArea(); JFileChooser fc = new JFileChooser(); JButton openf = new JButton(\ JButton savef = new JButton(\ JPanel p = new JPanel(); JScrollPane sp = new JScrollPane(ta); public JFileChooserDemo() {

Container c = this.getContentPane(); sp.setPreferredSize(new Dimension(350, 300)); openf.addActionListener(this); savef.addActionListener(this); p.add(openf); p.add(savef); c.add(msg, \ c.add(sp, \ c.add(p, \ this.setTitle(\ this.setBounds(200, 200, 300, 200); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }

public static void main(String[] args) { new JFileChooserDemo(); }

public void actionPerformed(ActionEvent e) { File file = null; int result; FileInputStream fileInStream = null; FileOutputStream fileOutStream = null; int readbyte; if (e.getSource() == openf) { fc.setApproveButtonText(\确定\ fc.setDialogTitle(\打开文件\

result = fc.showOpenDialog(this); ta.setText(\

if (result == JFileChooser.APPROVE_OPTION) { file = fc.getSelectedFile(); msg.setText(\您选择打开的文件名称为:\} else if (result == JFileChooser.CANCEL_OPTION) { msg.setText(\您没有选择任何文件\}

if (file != null) { try { fileInStream = new FileInputStream(file); } catch (FileNotFoundException fe) { msg.setText(\ return; }

try { while ((readbyte = fileInStream.read()) != -1) {

ta.append(String.valueOf((char) readbyte)); }

} catch (IOException ioe) { msg.setText(\读取文件错误\

} finally {

try { if (fileInStream != null) fileInStream.close(); } catch (IOException ioe2) { } } } }

if (e.getSource() == savef) { result = fc.showSaveDialog(this); file = null; String fileName; if (result == JFileChooser.APPROVE_OPTION) { file = fc.getSelectedFile(); msg.setText(\您选择存储的文件名称为:\

} else if (result == JFileChooser.CANCEL_OPTION) { msg.setText(\您没有选择任何文件\}

if (file != null) { try { fileOutStream = new FileOutputStream(file); } catch (FileNotFoundException fe) { msg.setText(\ return; }

String content = ta.getText();

try { fileOutStream.write(content.getBytes()); } catch (IOException ioe) { msg.setText(\写入文件错误\} finally { try { if (fileOutStream != null) fileOutStream.close(); } catch (IOException ioe2) { }

} } } } }

例6-18 JTreeDemo.java

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

public class JTreeDemo extends JFrame implements TreeSelectionListener { JTextPane tp = new JTextPane(); public JTreeDemo() { Container c = this.getContentPane(); DefaultMutableTreeNode root = new DefaultMutableTreeNode(\例题代码浏览器\

DefaultMutableTreeNode node = new DefaultMutableTreeNode(\ root.add(node); node = new DefaultMutableTreeNode(\ root.add(node); node = new DefaultMutableTreeNode(\ root.add(node); JTree tree = new JTree(root); JScrollPane spTree = new JScrollPane(tree); JScrollPane spShow = new JScrollPane(tp); JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, spTree, spShow); tree.getSelectionModel().setSelectionMode( TreeSelectionModel.SINGLE_TREE_SELECTION); tree.addTreeSelectionListener(this); c.add(splitPane); this.setTitle(\ this.setBounds(200, 100, 700, 400); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }

public static void main(String[] args) { new JTreeDemo(); }

public void valueChanged(TreeSelectionEvent e) {


Swing代码(5).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:甲级单位编制光伏玻璃项目可行性报告(立项可研+贷款+用地+2013

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: