如何在swing中动态增组件

如何在swing中动态增加组件呢??
java 代码
这是CPUPanel的组件的代码
  1. package example;  
  2.   
  3. import java.awt.Color;  
  4. import java.awt.Font;  
  5. import java.awt.event.ActionEvent;  
  6. import java.awt.event.ActionListener;  
  7. import javax.swing.JButton;  
  8. import javax.swing.JLabel;  
  9. import javax.swing.JPanel;  
  10. import com.swtdesigner.SwingResourceManager;  
  11.   
  12. public class CPUPanel extends JPanel {  
  13.   
  14.     /** 
  15.      * Create the panel 
  16.      */  
  17.     public CPUPanel() {  
  18.         super();  
  19.         setLayout(null);  
  20.   
  21.         final JLabel label = new JLabel();  
  22.         label.setIcon(SwingResourceManager.getIcon(CPUPanel.class"/com/esri/arcgis/beans/TOC/TOCControlIconColor16x16.gif"));  
  23.         label.setForeground(new Color(12800));  
  24.         label.setFont(new Font("宋体", Font.ITALIC, 18));  
  25.         label.setBounds(0029452);  
  26.         add(label);  
  27.         label.setText("这是谁的哪个牌子的CPU??");  
  28.   
  29.         final JButton button = new JButton();  
  30.         button.addActionListener(new ActionListener() {  
  31.             public void actionPerformed(ActionEvent e) {  
  32.                 setVisible(false);  
  33.             }  
  34.         });  
  35.         button.setText("退出");  
  36.         button.setBounds(24919923);  
  37.         add(button);  
  38.         //  
  39.     }  
  40.   
  41. }  
下面是MainBorad的代码,
java 代码
  1. package example;  
  2.   
  3. import java.awt.event.ActionEvent;  
  4. import example.CPUPanel;  
  5. import java.awt.event.ActionListener;  
  6. import javax.swing.JButton;  
  7. import javax.swing.JFrame;  
  8.   
  9. public class MainBoard extends JFrame {  
  10.   
  11.     /** 
  12.      * Launch the application 
  13.      * @param args 
  14.      */  
  15.     public static void main(String args[]) {  
  16.         try {  
  17.             MainBoard frame = new MainBoard();  
  18.             frame.setVisible(true);  
  19.         } catch (Exception e) {  
  20.             e.printStackTrace();  
  21.         }  
  22.     }  
  23.   
  24.     /** 
  25.      * Create the frame 
  26.      */  
  27.     public MainBoard() {  
  28.         super();  
  29.         getContentPane().setLayout(null);  
  30.         setBounds(100100500375);  
  31.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  32.   
  33.         final JButton button = new JButton();  
  34.         button.addActionListener(new ActionListener() {  
  35.             public void actionPerformed(ActionEvent e) {  
  36.                 final CPUPanel panel = new CPUPanel();  
  37.                 panel.setBounds(14245230200);  
  38.                 getContentPane().add(panel); 
  39. ///
  40.                 repaint();  
  41.             }  
  42.         });  
  43.         button.setText("CPU的情况");  
  44.         button.setBounds(101012423);  
  45.         getContentPane().add(button);  
  46.   
  47.         final JButton button_1 = new JButton();  
  48.         button_1.setText("内存的情况");  
  49.         button_1.setBounds(179109923);  
  50.         getContentPane().add(button_1);  
  51.   
  52.           
  53.         //  
  54.     }  
  55.   
  56. }  
千成要记住要加repaint()方法
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值