创建选项卡对象 需要关联到小窗体对象(JPanel)
但对运用 Jpanel 对象是不可见的 ,需要用到边框并设置颜色才可见
JPnael对象与选项卡对象关联后,就不需要对小窗体对象进行设置了(小窗体对象及其关联的组件(这是一个整体) 自动与选项卡相匹配
在关联小窗体的组件的类中 需要继承 JPanel 类(小窗体)
其中的思想与 组件化思想差不多,但是根本的思想有所差别 ,面向的对象不同,
package cn.lovo.day03_30; import java.awt.Image; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; public class MoneyJPanel extends JPanel{ public MoneyJPanel() { this.setLayout(null);
JButton jb = new JButton("姓名"); jb.setBounds(60, 150, 80, 25); this.add(jb); Image img = new ImageIcon("ming/dsws.jpg").getImage(); img = img.getScaledInstance(120, 100, 1); JLabel jla = new JLabel(new ImageIcon(img)); jla.setBounds(35, 30, 120, 100); this.add(jla); } }
package cn.lovo.day03_30; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class NewJpanel extends JPanel{ public NewJpanel() { this.setLayout(null); JTextField t = new JTextField(); t.setBounds(50, 40, 120, 30); this.add(t); JLabel jla = new JLabel("确定"); jla.setBounds(80, 100, 80, 25); this.add(jla); } }
1 import javax.swing.JFrame; 2 import javax.swing.JTabbedPane; 3 4 public class TabedFrame extends JFrame{ 5 public TabedFrame() 6 { 7 8 this.setLayout(null); 9 10 //创建选项卡对象 11 JTabbedPane jtb = new JTabbedPane(JTabbedPane.LEFT ); 12 jtb.setBounds(80, 30, 250, 250); 13 this.add(jtb); 14 15 16 //设置选项卡标签 和容器 17 jtb.add("新闻", new NewJpanel()); 18 jtb.add("财经",new MoneyJPanel()); 19 20 21 this.setVisible(true); 22 this.setSize(500,400); 23 this.setVisible(true); 24 this.setDefaultCloseOperation(3); 25 this.setLocationRelativeTo(null); 26 27 28 } 29 public static void main(String[] args) { 30 31 TabedFrame ta = new TabedFrame(); 32 33 } 34 35 }