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;
gridbag.setConstraints(label, constraints); add(label);
constraints.gridx = 3;
gridbag.setConstraints(password, constraints); add(password);
//新建“ComboBox”并设置属性
diploma = new JComboBox(new Object[]{\
\
constraints.gridx = 0; constraints.gridy = 1; constraints.gridwidth = 2;
gridbag.setConstraints(diploma, constraints);
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说教育文库Java程序设计教程 冶金工业出版社第5章(4)在线全文阅读。
相关推荐: