import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.tree.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.table.*;
/**
* Swing 组件测试程序
* 测试Swing所有组件及其相应的事件
* @author 天翼.李 2003.4.17 晚23:14
* @link http://www.robochina.org
* @link robococde@etang.com
*/
public class SwingTest extends JFrame
{
/**
* 主模块,初始化所有子模块,并设置主框架的相关属性
*/
public SwingTest()
{
// 初始化所有模块
MenuTest menuTest = new MenuTest();
LeftPanel leftPanel = new LeftPanel();
RightPanel rightPanel = new RightPanel();
BottomPanel bottomPanel = new BottomPanel();
CenterPanel centerPanel = new CenterPanel();
// 设置主框架的布局
Container c = this.getContentPane();
// c.setLayout(new BorderLayout())
this.setJMenuBar(menuTest);
c.add(leftPanel,BorderLayout.WEST);
c.add(rightPanel,BorderLayout.EAST);
c.add(centerPanel,BorderLayout.CENTER);
c.add(bottomPanel,BorderLayout.SOUTH);
// 利用无名内隐类,增加窗口事件
this.addWindowListener(new WindowAdapter()
{
public void WindowClosing(WindowEvent e)
{
// 释放资源,退出程序
dispose();
System.exit(0);
}
});
setSize(700,500);
setTitle("Swing 组件大全简体版");
// 隐藏frame的标题栏,此功暂时关闭,以方便使用window事件
// setUndecorated(true);
setLocation(200,150);
show();
}
////////////////////////////////////////////////////////////////////////////
/**
* 菜单栏处理模块
* JMenuBar --+
* --JMenu--+
* --JMenuItem --ActionListener
*
*/
class MenuTest extends JMenuBar
{
private JDialog aboutDialog;
/**
* 菜单初始化操作
*/
public MenuTest()
{
JMenu fileMenu = new JMenu("文件");
JMenuItem exitMenuItem = new JMenuItem("退出",KeyEvent.VK_E);
JMenuItem aboutMenuItem = new JMenuItem("关于...",KeyEvent.VK_A);
fileMenu.add(exitMenuItem);
fileMenu.add(aboutMenuItem);
this.add(fileMenu);
aboutDialog = new JDialog();
initAboutDialog();
// 菜单事件
exitMenuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
dispose();
System.exit(0);
}
});
aboutMenuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// "关于"对话框的处理
aboutDialog.show();
}
});
}
/**
* 返回关于对话框
*/
public JDialog getAboutDialog()
{
return aboutDialog;
}
/**
* 设置"关于"对话框的外观及响应事件,操作和JFrame一样都是在内容
* 框架上进行的
*/
public void initAboutDialog()
{
aboutDialog.setTitle("关于");
Container con =aboutDialog.getContentPane();
// Swing 中使用html语句
Icon icon = new ImageIcon("smile.gif");
JLabel aboutLabel = new JLabel("<html><b><font size=5>"+
"<center>Swing 组件大全简体版!"+"<br>天翼.李",icon,JLabel.CENTER);
//JLabel aboutLabel = new JLabel("Swing 组件大全简体版!",icon,JLabel.CENTER);
con.add(aboutLabel,BorderLayout.CENTER);
aboutDialog.setSize(450,225);
aboutDialog.setLocation(300,300);
aboutDialog.addWindowListener(new WindowAdapter()
{
public void WindowClosing(WindowEvent e)
{
dispose();
}
});
}
}
////////////////////////////////////////////////////////////////////////////
/**
* 最左边模块,继承JPanel,初始化内容为JTree
* JPanel--+
* --JTree
*/
class LeftPanel extends JPanel
{
private int i = 0;
public LeftPanel()
{
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
DefaultMutableTreeNode child = new DefaultMutableTreeNode("Child");
DefaultMutableTreeNode select = new DefaultMutableTreeNode("select");
DefaultMutableTreeNode child1 = new DefaultMutableTreeNode(""+i);
root.add(child);
root.add(select);
child.add(child1);
JTree tree = new JTree(root);
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
// 每个节点的行高
tree.setRowHeight(20);
tree.addTreeSelectionListener(new TreeSelectionListener ()
{
public void valueChanged(TreeSelectionEvent e)
{
// 内隐类不能直接引用外部类tree,1.外部变量可申明为final 2.新建外部类的对象
JTree tree =(JTree)e.getSource();
DefaultMutableTreeNode selectNode = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
i++;
selectNode.add(new DefaultMutableTreeNode(""+i));
}
});
tree.setPreferredSize(new Dimension(100,300));
// tree.setEnabled(true);
JScrollPane scrollPane = new JScrollPane(tree);
//scrollPane.setSize(100,350);
this.add(scrollPane);
}
}
////////////////////////////////////////////////////////////////////////////
/**
* 最下面层模块,继承JPanel,初始化内容为进度条,并由定时器控制
* JPanel--+
* --JProcessBar --Timer
*/
class BottomPanel extends JPanel
{
private JProgressBar pb;
public BottomPanel()
{
pb = new JProgressBar();
pb.setPreferredSize(new Dimension(680,20));
// 设置定时器,用来控制进度条的处理
Timer time = new Timer(1,new ActionListener()
{
int counter = 0;
public void actionPerformed(ActionEvent e)
{
counter++;
pb.setValue(counter);
Timer t = (Timer)e.getSource();
// 如果进度条达到最大值重新开发计数
if (counter == pb.getMaximum())
{
t.stop();
counter =0;
t.start();
}
}
});
time.start();
pb.setStringPainted(true);
pb.setMinimum(0);
pb.setMaximum(100);
pb.setBackground(Color.white);
pb.setForeground(Color.red);
this.add(pb);
}
/**
* 设置进度条的数据模型
*/
public void setProcessBar(BoundedRangeModel rangeModel)
{
pb.setModel(rangeModel);
}
}
////////////////////////////////////////////////////////////////////////////
/**
* 最右边模块,继承JPanel,初始化各种按钮
* JPanel--+
* --JButton --JToggleButton -- JList -- JCombox --JCheckBox ....
*/
class RightPanel extends JPanel
{
public RightPanel()
{
this.setLayout(new GridLayout(8,1));
// 初始化各种按钮
JCheckBox checkBox = new JCheckBox("复选按钮");
JButton button = new JButton("打开文件");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JFileChooser file = new JFileChooser();
int result = file.showOpenDialog(new JPanel());
if (result ==file.APPROVE_OPTION)
{
String fileName = file.getSelectedFile().getName();
String dir = file.getCurrentDirectory().toString();
JOptionPane.showConfirmDialog(null,dir+"\\"+fileName,"选择的文件",JOptionPane.YES_OPTION);
}
}
});
JToggleButton toggleButton = new JToggleButton("双态按钮");
ButtonGroup buttonGroup = new ButtonGroup();
JRadioButton radioButton1 = new JRadioButton("单选按钮1",false);
JRadioButton radioButton2 = new JRadioButton("单选按钮2",false);
// 组合框的处理
JComboBox comboBox = new JComboBox();
comboBox.setToolTipText("点击下拉列表增加选项");
comboBox.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JComboBox comboBox =(JComboBox)e.getSource();
comboBox.addItem("程序员");
comboBox.addItem("分析员");
}
});
// 列表框的处理
DefaultListModel litem = new DefaultListModel();
litem.addElement("香蕉");
litem.addElement("水果");
JList list = new JList(litem);
list.addListSelectionListe
没有合适的资源?快使用搜索试试~ 我知道了~
swing 组件测试程序.rar_java Swing程序
共19个文件
class:16个
txt:1个
gif:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 43 浏览量
2022-09-19
12:54:34
上传
评论
收藏 18KB RAR 举报
温馨提示
一个简单的Java Swing组件应用程序,基本上包含了java Swing的常用的组件。
资源推荐
资源详情
资源评论
收起资源包目录
swing 组件测试程序.rar (19个子文件)
swing 组件测试程序
SwingTest.java 11KB
SwingTest$RightPanel$3.class 951B
smile.gif 1KB
SwingTest$MenuTest$1.class 675B
SwingTest$BottomPanel.class 1KB
SwingTest$MenuTest$3.class 595B
SwingTest$MenuTest$2.class 740B
SwingTest$LeftPanel.class 2KB
SwingTest$RightPanel$1.class 1KB
SwingTest$1.class 520B
SwingTest$CenterPanel$1.class 802B
SwingTest$RightPanel$2.class 835B
SwingTest.class 1KB
SwingTest$CenterPanel.class 2KB
SwingTest$RightPanel.class 2KB
SwingTest$BottomPanel$1.class 1KB
SwingTest$LeftPanel$1.class 1KB
SwingTest$MenuTest.class 2KB
www.pudn.com.txt 218B
共 19 条
- 1
资源评论
寒泊
- 粉丝: 75
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功