教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 法律文档 >

Java程序设计教程 冶金工业出版社第5章(4)

来源:网络收集 时间:2026-08-27
导读: JMenuItem fileItem1 = new JMenuItem(\新建三个菜单项 JMenuItem fileItem2 = new JMenuItem(\ JMenuItem fileItem3 = new JMenuItem(\ TabbedPaneOrientation tabbedPane; public SwingDemo() { } public void in

JMenuItem fileItem1 = new JMenuItem(\新建三个菜单项 JMenuItem fileItem2 = new JMenuItem(\

JMenuItem fileItem3 = new JMenuItem(\ TabbedPaneOrientation tabbedPane; public SwingDemo() { }

public void init() { }

public void actionPerformed(ActionEvent e) {

//菜单响应事件

//初始函数,为框架添加各种组件 content = getContentPane(); //为菜单添加菜单项 fileMenu.add(fileItem1); fileMenu.add(fileItem2); fileItem1.addActionListener(this); fileItem2.addActionListener(this); fileItem3.addActionListener(this); //将菜单添加到菜单栏 bar.add(fileMenu); setJMenuBar(bar); //添加标签面板

tabbedPane = new TabbedPaneOrientation(); tabbedPane.init(); content.add(tabbedPane);

//构造函数

addWindowListener(new WindowAdapter() { });

public void windowClosing(WindowEvent e) { }

dispose(); System.exit(0);

JMenuItem checkItem = (JMenuItem) e.getSource(); if (checkItem == fileItem1)

fileMenu.insert(fileItem3, 1); System.exit(0); if (checkItem == fileItem2)

}

if (checkItem == fileItem3)

fileMenu.remove(fileItem3);

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

//主函数

System.out.println(\SwingDemo mainFrame = new SwingDemo(); mainFrame.setSize(400, 350); mainFrame.setTitle(\mainFrame.init();

mainFrame.setVisible(true);

//建立一个类扩展自JPanel,其中使用标签面板,并添加组件以及组件的事件处理 class TabbedPaneOrientation extends JPanel implements ActionListener { JTabbedPane tabbedPane = new JTabbedPane(SwingConstants.BOTTOM); JButton button1, button2, button3, button4; public void init() {

//组件初始化

JPanel buttonPanel = new JPanel();

InformationPanel panel1 = new InformationPanel(); SplitPanel panel2 = new SplitPanel(); panel1.init(); panel2.init(); //设置标签面板

tabbedPane.setTabPlacement(JTabbedPane.TOP);

tabbedPane.addTab(\null, panel1, \tabbedPane.addTab(\null, panel2, \button1 = new JButton(\button2 = new JButton(\button3 = new JButton(\button4 = new JButton(\//设置按钮标签 buttonPanel.add(button1); buttonPanel.add(button2); buttonPanel.add(button3); buttonPanel.add(button4); button1.addActionListener(this); button2.addActionListener(this); button3.addActionListener(this);

}

button4.addActionListener(this); button1.setEnabled(false); setLayout(new BorderLayout()); add(tabbedPane, BorderLayout.CENTER); add(buttonPanel, BorderLayout.SOUTH);

public void actionPerformed(ActionEvent e) { } }

button1.setEnabled(true); button2.setEnabled(true); button3.setEnabled(true); button4.setEnabled(true); if (e.getSource() == button1) { }

if (e.getSource() == button2) { }

if (e.getSource() == button3) { }

if (e.getSource() == button4) { }

//按钮响应事件

tabbedPane.setTabPlacement(JTabbedPane.TOP); button1.setEnabled(false);

tabbedPane.setTabPlacement(JTabbedPane.LEFT); button2.setEnabled(false);

tabbedPane.setTabPlacement(JTabbedPane.RIGHT); button3.setEnabled(false);

tabbedPane.setTabPlacement(JTabbedPane.BOTTOM); button4.setEnabled(false);

//建立一个类扩展自JSplitPane类,其中分隔显示两个文本字段及一些控制组件 class SplitPanel extends JSplitPane implements ActionListener { JButton buttonA, buttonB;

JTextField text1 = new JTextField(\ JTextField text2 = new JTextField(\ //新建分隔面板

JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT, text1, text2); private boolean vertical = true; public void init() {

setLayout(new BorderLayout());

//设置边界布局

}

JPanel buttonPanel = new JPanel(); //新建按钮面板

buttonA = new JButton(\buttonA.addActionListener(this); buttonPanel.add(buttonA);

buttonB = new JButton(\buttonB.addActionListener(this); buttonPanel.add(buttonB);

add(split, BorderLayout.CENTER);

//添加分隔面板 //添加按钮面板

add(buttonPanel, BorderLayout.SOUTH);

public void actionPerformed(ActionEvent e) { } }

if (e.getSource() == buttonA) { }

if (e.getSource() == buttonB) { }

if (vertical) { }

if (split.isOneTouchExpandable()) { }

//按钮响应事件

split.setOneTouchExpandable(false);

buttonA.setText(\split.setOneTouchExpandable(true); buttonA.setText(\

} else {

split.setOrientation(JSplitPane.HORIZONTAL_SPLIT); buttonB.setText(\vertical = false;

split.setOrientation(JSplitPane.VERTICAL_SPLIT); buttonB.setText(\vertical = true;

} else {

//建立一个信息选择面板类扩展自JPanel,其中添加多种控制组件 class InformationPanel extends JPanel implements ActionListener { JLabel label;

JTextField name = new JTextField(8);

JPasswordField password = new JPasswordField(8); ButtonGroup bg = new ButtonGroup();

String[] elements = {\

\

JCheckBox gym, music, computer; JList skill;

JButton summit, cancel; JComboBox diploma; JRadioButton male, female; public void init() {

//组件初始化

GridBagLayout gridbag = new GridBagLayout(); //新建网格包布局管理器 GridBagConstraints constraints = new GridBagConstraints();//新建网格包限制 setLayout(gridbag);

//新建“Name”标签并设置属性 label = new JLabel(\

constraints.fill = GridBagConstraints.BOTH; constraints.gridy = 0; constraints.weightx = 0.5;

gridbag.setConstraints(label, constraints); add(label);

constraints.gridx = 1;

gridbag.setConstraints(name, constraints); add(name);

//新建“Password”标签并设置属性 label = new JLabel(\constraints.gridx = 2 …… 此处隐藏:3157字,全部文档内容请下载后查看。喜欢就下载吧 ……

Java程序设计教程 冶金工业出版社第5章(4).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/435504.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)