}
例 6-6 JPanelDemo.java
import java.awt.*; import javax.swing.*;
public class JPanelDemo extends JFrame { JPanel p1 = new JPanel(); JPanel p2 = new JPanel(); JPanel p3 = new JPanel(new GridLayout(2, 2)); JLabel msg1 = new JLabel(\这是面板P1,只包含本JLabel\ JLabel msg2 = new JLabel(\这是面板P2,其中包含了面板P3,P3中含4个按钮\ }
JButton[] btn = new JButton[4]; public JPanelDemo() { Container c = this.getContentPane(); for (int i = 0; i < 4; i++) { btn[i] = new JButton(\ } p1.add(msg1); for (int i = 0; i < 4; i++) { p3.add(btn[i]); } p2.add(msg2); p2.add(p3); c.setLayout(null); p1.setBounds(0, 0, 320, 30); p2.setBounds(0, 30, 320, 120); msg2.setBounds(0, 30, 320, 30); p3.setBounds(0, 60, 320, 90); c.add(p1); c.add(p2); this.setTitle(\ this.setSize(320, 150); this.setLocation(300, 300); this.validate(); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
public static void main(String[] args) { new JPanelDemo(); }
例6-7 JScrollPaneDemo.java
import java.awt.*;
import java.awt.event.*; import javax.swing.*;
public class JScrollPaneDemo extends JFrame implements ActionListener { JLabel img = new JLabel(new ImageIcon(\ JButton b1 = new JButton(\显示垂直滚动条\ JButton b2 = new JButton(\不显示垂直滚动条\ JButton b3 = new JButton(\必要时显示垂直滚动条\
JScrollPane sp = new JScrollPane(img);
JPanel p = new JPanel(new GridLayout(1, 3)); public JScrollPaneDemo() { Container c = this.getContentPane(); c.add(sp, \ c.add(p, \ p.add(b1); p.add(b2); p.add(b3); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); this.setTitle(\ this.setSize(500, 300); this.setLocation(300, 300); this.validate(); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
public static void main(String[] args) { new JScrollPaneDemo(); }
public void actionPerformed(ActionEvent e) { String s = e.getActionCommand(); if (s.equals(\显示垂直滚动条\
sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); } else if (s.equals(\不显示垂直滚动条\
sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER); } else if (s.equals(\必要时显示垂直滚动条\
sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); } }
}
例6-8 JSplitPaneDemo.java
import java.awt.*; import javax.swing.*;
public class JSplitPaneDemo extends JFrame { JLabel left = new JLabel(\这是左侧显示区!\ JLabel right = new JLabel(\这是右侧显示区!\
JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, false, left,right); public JSplitPaneDemo() { Container c = this.getContentPane(); c.add(sp); this.setTitle(\ this.setSize(300, 200); this.setLocation(300, 300); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { new JSplitPaneDemo(); } }
例6-9 JInternalFrameDemo.java
import java.awt.*;
import java.awt.event.*; import javax.swing.*;
public class JInternalFrameDemo extends JFrame implements ActionListener { JDesktopPane dPane = new JDesktopPane(); JButton btn = new JButton(\点击创建一个内部窗口\
public JInternalFrameDemo() { Container c = this.getContentPane(); c.add(dPane, \ c.add(btn, \ btn.addActionListener(this); this.setTitle(\ this.setSize(300, 300); this.setLocation(300, 300); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) { new JInternalFrameDemo(); }
public void actionPerformed(ActionEvent e) { JInternalFrame inf = new JInternalFrame(\内部窗口\
inf.setLocation(20, 20); inf.setSize(260, 200); inf.setVisible(true);
JLabel msg = new JLabel(\这是内部面板中的JLabel\
Container c = inf.getContentPane(); c.add(msg); dPane.add(inf); } }
例6-10 JLabelWithIcon.java
import java.awt.*; import javax.swing.*;
public class JLabelWithIcon extends JFrame { Icon icon = new ImageIcon(\ JLabel lb1 = new JLabel(\文本标签\ }
JLabel lb2 = new JLabel(icon);
JLabel lb3 = new JLabel(\文本内容\public JLabelWithIcon() { Container c = this.getContentPane(); c.setLayout(new GridLayout(1, 3)); c.add(lb1); c.add(lb2); c.add(lb3); lb3.setVerticalTextPosition(JLabel.BOTTOM); lb3.setHorizontalTextPosition(JLabel.CENTER); this.setTitle(\ this.setSize(400, 200); this.setLocation(300, 300); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
public static void main(String[] args) { new JLabelWithIcon(); }
例6-11 CommonComponent.java
import java.awt.*;
import java.awt.event.*; import javax.swing.*;
import javax.swing.event.*;
public class CommonComponent extends JFrame { JLabel msg = new JLabel(\ JLabel lName = new JLabel(\ JLabel lSex = new JLabel(\ JLabel lWeight = new JLabel(\ JLabel lInterest = new JLabel(\ JLabel lCity = new JLabel(\ JButton bSubmit = new JButton(\ JButton bReset = new JButton(\ JTextField tName = new JTextField(100); JRadioButton male = new JRadioButton(\ JRadioButton female = new JRadioButton(\ ButtonGroup bgSex = new ButtonGroup(); JPasswordField tWeight = new JPasswordField(100); JCheckBox read = new JCheckBox(\ JCheckBox tour = new JCheckBox(\ JCheckBox sport = new JCheckBox(\ JCheckBox game = new JCheckBox(\ String[] sCity = { \ JComboBox goCity = new JComboBox(sCity); BtnListener blsn = new BtnListener(this); TxtListener tlsn = new TxtListener(this); String name, sex, weight, city; String[] interest = new String[4]; public CommonComponent() { Container c = this.getContentPane(); c.setLayout(null); JPanel p = new JPanel(new GridLayout(1, 1)); JPanel p1 = new JPanel(new GridLayout(5, 2, 10, 10)); JPanel p2 = new JPanel(new GridLayout(1, 2)); JPanel p3 = new JPanel(new GridLayout(2, 2)); JPanel p4 = new JPanel(); p.setBounds(0, 0, 300, 40); p1.setBounds(0, 40, 300, 180); p4.setBounds(0, 220, 300, 50); c.add(p); c.add(p1);