100个Java经典例⼦(21-30)初学者的利器⾼⼿的宝典
JavaSE
package test21;
import javax.swing.JDesktopPane;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JFrame;
import javax.swing.KeyStroke;
import java.awt.event.*;
import java.awt.*;
/
**
* Title: 内部窗体演⽰
* Description: 这是演⽰⼀个内部窗体。可以选择“新⽂档”菜单不停的⽣成内部窗体。
* Filename:
*/
public class InternalFrameDemo extends JFrame
implements ActionListener {
private static final long serialVersionUID = 1L;
JDesktopPane desktop;
/**
*⽅法说明:构造器,添加窗体成员
*输⼊参数:
*返回类型:
*/
public InternalFrameDemo() {
super("InternalFrameDemo");
//这⾥设置了⼀个⽐较⼤的窗体,给四周只留了50px的边界
int inset = 50;
Dimension screenSize = DefaultToolkit().getScreenSize();
setBounds(inset, inset,
screenSize.width - inset*2,
screenSize.height - inset*2);
/
/定义界⾯
desktop = new JDesktopPane(); //桌⾯⾯板
createFrame(); //创建第⼀个内部窗体
setContentPane(desktop);//将桌⾯添加到窗体
setJMenuBar(createMenuBar());
//设置托拽模式
desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
}
/**
*⽅法说明:组建菜单
*输⼊参数:
*返回类型:
*/
protected JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
//定义⼀个菜单
JMenu menu = new JMenu("⽂件");
menu.setMnemonic(KeyEvent.VK_D);
menuBar.add(menu);
//定义“新⽂档”菜单
JMenuItem menuItem = new JMenuItem("新⽂档");
JMenuItem menuItem = new JMenuItem("新⽂档");
menuItem.setMnemonic(KeyEvent.VK_N);
menuItem.KeyStroke(
KeyEvent.VK_N, ActionEvent.ALT_MASK));
menuItem.setActionCommand("new");
menuItem.addActionListener(this);
menu.add(menuItem);
//定义“退出”菜单
menuItem = new JMenuItem("退出");
menuItem.setMnemonic(KeyEvent.VK_Q);
menuItem.KeyStroke(
KeyEvent.VK_Q, ActionEvent.ALT_MASK));
menuItem.setActionCommand("quit");
menuItem.addActionListener(this);
menu.add(menuItem);
return menuBar;
}
/**
*⽅法说明:事件监听,对选择的菜单做出反映
*输⼊参数:ActionEvent e 事件
*返回类型:
*/
public void actionPerformed(ActionEvent e) {
if ("new".ActionCommand())) { //新⽂档
createFrame();
} else { //退出
quit();
}
}
/**
*⽅法说明:调⽤MyInternalFrame类创建新的内部窗体,
*输⼊参数:
*返回类型:
*/
protected void createFrame() {
MyInternalFrame frame = new MyInternalFrame();
frame.setVisible(true);
desktop.add(frame);
try {
frame.setSelected(true);
} catch (java.beans.PropertyVetoException e) {}
}
/**
*⽅法说明:退出程序
*输⼊参数:
*返回类型:
*/
protected void quit() {
}
/**
*⽅法说明:主⽅法
*输⼊参数:
*返回类型:
*/
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
//建⽴⼀个内部窗体
InternalFrameDemo frame = new InternalFrameDemo();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //显⽰窗体
//显⽰窗体
frame.setVisible(true);
}
}
package test21;
import javax.swing.JInternalFrame;
import javax.swing.JTextArea;
/
**
* Title: 内部窗体
* Description: ⽣成⼀个内部窗体,提供InternalFrameDemo类使⽤
* Filename: MyInternalFrame.java
*/
public class MyInternalFrame extends JInternalFrame {
private static final long serialVersionUID = 1L;
static int openFrameCount = 0;
static final int xOffset = 30, yOffset = 30;
public MyInternalFrame() {
super("⽂档 #" + (++openFrameCount),
true, //可变尺⼨
true, //有关闭按钮
true, //有最⼤化按钮
true);//最⼩化按钮
//给内部窗体添加⼀个⽂本域
JTextArea j = new JTextArea(5,20);
getContentPane().add(j);
//设置内部窗体的⼤⼩
setSize(300,300);
//设置内部窗体的显⽰位置
setLocation(xOffset*openFrameCount, yOffset*openFrameCount); }
}
package test22;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
/**
* Title: 分割⾯板
* Description: 演⽰将⾯板分割成左右两部分
* Fiename:SplitPaneDemo.java
*/
public class SplitPaneDemo implements ListSelectionListener {
private String[] imageNames={"Bird.gif","Cat.gif","Dog.gif","Pig.gif"}; private JLabel picture;
private JList list;
private JSplitPane splitPane;
/**
*⽅法说明:构造器,定义了所有要使⽤的构件
*输⼊参数:
*返回类型:
*/
public SplitPaneDemo() {
//创建⼀个图像名称的列表,设置为单选⽅式
list = new JList(imageNames);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setSelectedIndex(0);
list.addListSelectionListener(this);
JScrollPane listScrollPane = new JScrollPane(list);
htmlborder//获取默认的图⽚
ImageIcon firstImage = createImageIcon("images/" +
(String)imageNames[0]);
if (firstImage != null) {
picture = new JLabel(firstImage);
picture.setPreferredSize(new IconWidth(), IconHeight()));
} else {
picture = new JLabel((String)imageNames[0]);
}
JScrollPane pictureScrollPane = new JScrollPane(picture);
//创建⼀个⽔平分割的⾯板,定义了两个⾯板的名字。
splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,
listScrollPane, pictureScrollPane);
splitPane.setOneTouchExpandable(true);
splitPane.setDividerLocation(150);
//定义⾯板的最⼩尺⼨
Dimension minimumSize = new Dimension(100, 50);
listScrollPane.setMinimumSize(minimumSize);
pictureScrollPane.setMinimumSize(minimumSize);
//定义初始尺⼨
splitPane.setPreferredSize(new Dimension(400, 200));
}
/**
*⽅法说明:获得这个分割的⾯板
*输⼊参数:
*返回类型:JSplitPane 对象
*/
public JSplitPane getSplitPane() {
return splitPane;
}
/**
*⽅法说明:列表监听事件处理
*输⼊参数:ListSelectionEvent e 列表选择事件
*返回类型:
*/
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting())
return;
JList theList = (Source();
if (theList.isSelectionEmpty()) {
picture.setIcon(null);
picture.setText(null);
} else {
int index = SelectedIndex();
ImageIcon newImage = createImageIcon("images/" +
(String)imageNames[index]);
picture.setIcon(newImage);
if (newImage != null) {
picture.setText(null);
picture.setPreferredSize(new IconWidth(), IconHeight() ));
} else {
picture.setText("Image not found: "
picture.setText("Image not found: "
+ (String)imageNames[index]);
}
}
}
/**
*⽅法说明:根据路径获取图形对象
*输⼊参数:String path 图⽚路径
*返回类型:ImageIcon 图⽚对象
*/
protected static ImageIcon createImageIcon(String path) {
java.URL imgURL = Resource(path); if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
return null;
}
}
/**
*⽅法说明:主⽅法
*输⼊参数:
*返回类型:
*/
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
//定义窗体
JFrame frame = new JFrame("SplitPaneDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SplitPaneDemo splitPaneDemo = new SplitPaneDemo();
//显⽰窗体
frame.pack();
frame.setVisible(true);
}
}
package test23;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
/**
* Title: 滑动杆演⽰
* Description: 使⽤滑动杆控制定时器,来控制图⽚的播放速度
* Filename: SliderDemo.java
*/
public class SliderDemo extends JPanel
implements ActionListener,
WindowListener,
ChangeListener {
private static final long serialVersionUID = 1L;
//设置图⽚的参数
static final int FPS_MIN = 0; //设置最⼩值
static final int FPS_MAX = 30; //设置最⼤值
static final int FPS_INIT = 15; //初始数值
int frameNumber = 0;
int NUM_FRAMES = 14;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论