本篇主要练习JPanel,JScrollPane,JLabel控件的使用。
JPanel面板的使用
import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; //面板也是一个swing容器,可以容纳其它控件,但必须添加到其它容器中才能发挥作用,如添加到窗体控件容器中 public class MyJPanel extends JFrame{ private static final long serialVersionUID = 3932481269635344760L; private JPanel content; public static void main(String[] args) { MyJPanel myjp = new MyJPanel(); myjp.setVisible(true); } public MyJPanel() { // TODO Auto-generated constructor stub setTitle("面板使用"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 250, 200); content = new JPanel(); content.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(content);// 应用内容面板 JPanel jp1 = new JPanel(); content.add(jp1); jp1.add(new JButton("A")); jp1.add(new JButton("B")); jp1.add(new JButton("C")); JPanel jp2 = new JPanel(); content.add(jp2); jp2.add(new JButton("D")); jp2.add(new JButton("E")); jp2.add(new JButton("F")); JPanel jp3 = new JPanel(); content.add(jp3); jp3.add(new JButton("D")); jp3.add(new JButton("E")); jp3.add(new JButton("F")); } }
Swing Container: JFrame, JPanel, JScrollPane , JSplitPane, JTabeledPane ,JDesktopPane, JInternalFrame ,JDialog, JWindow, JToolBar, JApplet
Swing Component: JButton, JCheckBox, JRadioButton, JToggleButton ,JLabel, JTextField, JPasswordField, JTextArea ,JSlider, JScrollBar, JList, JComboBox, JTree
JTableOnJScrollPane, JTable, TableColumn , JOptionPage, JTextPane, JEditorPane
Swing Menw: JMenuBar, JMenu, JPopupItem, JCheckBoxMenuItem, JRadioButtonMenuItem
AWT controls:Frame, Applet, Dialog, Window, Panel, ScrollPane, Button, Label, ChekcBox , Choice, List, ScrollBar, TextField, TextArea
滚动面板JScrollPane的使用
import java.awt.BorderLayout; import javax.swing.JFrame; //JPanel面板本身作为一个容器,可以聚集一些控件进行布局, //但也必须添加到其它容器中才能发挥作用,如窗体控件JFrame import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.border.EmptyBorder; public class MyJScrollPane extends JFrame { private JPanel content; private static final long serialVersionUID = 6788676512883612630L; public static void main(String[] args) { MyJScrollPane myJsp = new MyJScrollPane(); myJsp.setVisible(true); } MyJScrollPane(){ setTitle("滚动面板使用"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100,100,250,200); content = new JPanel();// 创建全局内容面板 content.setBorder(new EmptyBorder(5, 5, 5, 5)); content.setLayout(new BorderLayout(0, 0)); // 设置布局模式 setContentPane(content);// 应用布局管理 JScrollPane jsp = new JScrollPane(); // 将滚动面板添加到全局内容面板中,并放置到正中间件 content.add(jsp,BorderLayout.CENTER); JTextArea textArea = new JTextArea();// 创建文本域控件 jsp.setViewportView(textArea); // 设置滚动面板中显示文本域 } }
import java.awt.BorderLayout; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; public class MyLabel extends JFrame{ private static final long serialVersionUID = -6835211007654215677L; public static void main(String[] args) { MyLabel myJl = new MyLabel(); myJl.setVisible(true); } private JPanel contentPane; MyLabel(){ setTitle("标签栏的使用"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5,5,5,5)); contentPane.setBounds(100, 100, 250, 100); contentPane.setLayout(new BorderLayout(0, 0)); setContentPane(contentPane);// 应用内容面板 JLabel jl = new JLabel("广东省深圳市Company-X"); // 工程bin/imgage目录下的icon.ipg图片 jl.setIcon(new ImageIcon(MyLabel.class.getResource("/image/icon.jpg"))); contentPane.add(jl,BorderLayout.CENTER); } }