我用setCellRenderer方法: class myPanel  extends JPanel {
    myPanel(String[] str) {
      setLayout(new GridLayout(1, 1));      JLabel labelText = new JLabel("12号");
      add(labelText);
    }给每一个格装上了一个panel对像,请问如何访问此对像
即如果我想通过代码,改第二行,第三格的panel的label的text,怎写?

解决方案 »

  1.   

    class myPanel  extends JPanel {
        myPanel(String[] str,String text) {
          setLayout(new GridLayout(1, 1));      JLabel labelText = new JLabel(text);
          add(labelText);
        }
      

  2.   

    不过还是没怎么明白你的意思
    觉得应该这么写比较好吧
    class myPanel  extends JPanel {   
       JLabel  labelText=new JLabel(); 
        myPanel(String[] str) {
          setLayout(new GridLayout(1, 1));
          labelText = new JLabel("12号");
          add(labelText);
        }
       public setLabelText(String text){
          this.label.setText(text);
       }
      

  3.   

    在mypanel提供一个设置labeltext的方法的接口
      

  4.   

    package test;import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    import javax.swing.*;
    import javax.swing.table.*;
    public class Applet2 extends JApplet {class RadioButtonPanel  extends JPanel {
           RadioButtonPanel(String[] str) {
           setLayout(new GridLayout(1, 1));
           JLabel labelText = new JLabel("12号");
           add(labelText);
         }
       }
    class RadioButtonRenderer extends RadioButtonPanel implements TableCellRenderer{
         RadioButtonRenderer(String[] strs) {
           super(strs);
         }
       public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
           Component component = this;
           if (hasFocus) {
             component.setBackground(Color.ORANGE);
           }
           else {
             component.setBackground(Color.green);
           }
           return component;
         }   public Component getTableCellEditorComponent(JTable table, Object value,
                                                      boolean isSelected, int row,
                                                      int column) {       return this;
         }     public Object getCellEditorValue() {       return this;
         }   }
    Component nowComponent=null;
       String[] answer = {
            "A", "B", "C"};
     RadioButtonRenderer render;  private boolean isStandalone = false;
      JPanel jPanel1 = new JPanel();
      JButton jButton1 = new JButton();
      //Get a parameter value
      public String getParameter(String key, String def) {
        return isStandalone ? System.getProperty(key, def) :
            (getParameter(key) != null ? getParameter(key) : def);
      }  //Construct the applet
      public Applet2() {  }  //Initialize the applet
      public void init() {
        try {
          jbInit();
        }
        catch (Exception e) {
          e.printStackTrace();
        }
      }  //Component initialization
      private void jbInit() throws Exception {    DefaultTableModel dm = new DefaultTableModel() {
         public boolean isCellEditable(int row, int column) {
           return false;
          }
        };
        dm.setDataVector(
            new Object[][] {
              {"1", "1"}
            , {"2", "2"}
            , {"3", "3"}
            , {"4", "4"}
            , {"5", "5"}
        }
            ,
            new Object[] {"1", "2"});    JTable table = new JTable(dm);
        table.setVerifyInputWhenFocusTarget(false);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        table.setCellSelectionEnabled(true);
        table.setColumnSelectionAllowed(false);
        table.setGridColor(UIManager.getColor("window"));
        table.setRowHeight(60);
        table.setRowSelectionAllowed(false);
        table.setSelectionBackground(Color.red);
        table.addKeyListener(new Applet2_table_keyAdapter(this));render=new RadioButtonRenderer(answer);
     table.getColumn(table.getColumnName(1)).setCellRenderer(
            render);    table.getColumn(table.getColumnName(0)).setCellRenderer(render
            );    JScrollPane jScrollPane1 = new JScrollPane();    this.setSize(new Dimension(400, 300));
        this.getContentPane().add(jScrollPane1, BorderLayout.CENTER);
        jScrollPane1.getViewport().add(table, null);  }
      //Get Applet information
      public String getAppletInfo() {
        return "Applet Information";
      }  //Get parameter info
      public String[][] getParameterInfo() {
        return null;
      }  //static initializer for setting look & feel
      static {
        try {
          //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
          //UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
        }
        catch (Exception e) {
        }
      }  void table_keyPressed(KeyEvent e) {
        if(e.getKeyCode()==KeyEvent.VK_SPACE){// ((JLabel)nowComponent).setIcon(icon);
     JTable tab=(JTable)e.getComponent();int row=tab.getSelectedRow();
    int col=tab.getSelectedColumn();Component cell2 =render.getTableCellRendererComponent(tab, tab.getValueAt(row,col), false, false, 0, 1) ;
      jll.setText("sadfkljdslkf");
      cell2.repaint();
        }  }
    }class Applet2_table_keyAdapter
        extends java.awt.event.KeyAdapter {
      Applet2 adaptee;  Applet2_table_keyAdapter(Applet2 adaptee) {
        this.adaptee = adaptee;
      }  public void keyPressed(KeyEvent e) {
        adaptee.table_keyPressed(e);
      }
    }
      

  5.   

    漏了两句,以下才是
    package test;import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    import javax.swing.*;
    import javax.swing.table.*;
    public class Applet2 extends JApplet {class RadioButtonPanel  extends JPanel {
           RadioButtonPanel(String[] str) {
           setLayout(new GridLayout(1, 1));
           JLabel labelText = new JLabel("12号");
           add(labelText);
         }
       }
    class RadioButtonRenderer extends RadioButtonPanel implements TableCellRenderer{
         RadioButtonRenderer(String[] strs) {
           super(strs);
         }
       public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
           Component component = this;
           if (hasFocus) {
             component.setBackground(Color.ORANGE);
           }
           else {
             component.setBackground(Color.green);
           }
           return component;
         }   public Component getTableCellEditorComponent(JTable table, Object value,
                                                      boolean isSelected, int row,
                                                      int column) {       return this;
         }     public Object getCellEditorValue() {       return this;
         }   }
    Component nowComponent=null;
       String[] answer = {
            "A", "B", "C"};
     RadioButtonRenderer render;  private boolean isStandalone = false;
      JPanel jPanel1 = new JPanel();
      JButton jButton1 = new JButton();
      //Get a parameter value
      public String getParameter(String key, String def) {
        return isStandalone ? System.getProperty(key, def) :
            (getParameter(key) != null ? getParameter(key) : def);
      }  //Construct the applet
      public Applet2() {  }  //Initialize the applet
      public void init() {
        try {
          jbInit();
        }
        catch (Exception e) {
          e.printStackTrace();
        }
      }  //Component initialization
      private void jbInit() throws Exception {    DefaultTableModel dm = new DefaultTableModel() {
         public boolean isCellEditable(int row, int column) {
           return false;
          }
        };
        dm.setDataVector(
            new Object[][] {
              {"1", "1"}
            , {"2", "2"}
            , {"3", "3"}
            , {"4", "4"}
            , {"5", "5"}
        }
            ,
            new Object[] {"1", "2"});    JTable table = new JTable(dm);
        table.setVerifyInputWhenFocusTarget(false);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        table.setCellSelectionEnabled(true);
        table.setColumnSelectionAllowed(false);
        table.setGridColor(UIManager.getColor("window"));
        table.setRowHeight(60);
        table.setRowSelectionAllowed(false);
        table.setSelectionBackground(Color.red);
        table.addKeyListener(new Applet2_table_keyAdapter(this));render=new RadioButtonRenderer(answer);
     table.getColumn(table.getColumnName(1)).setCellRenderer(
            render);    table.getColumn(table.getColumnName(0)).setCellRenderer(render
            );    JScrollPane jScrollPane1 = new JScrollPane();    this.setSize(new Dimension(400, 300));
        this.getContentPane().add(jScrollPane1, BorderLayout.CENTER);
        jScrollPane1.getViewport().add(table, null);  }
      //Get Applet information
      public String getAppletInfo() {
        return "Applet Information";
      }  //Get parameter info
      public String[][] getParameterInfo() {
        return null;
      }  //static initializer for setting look & feel
      static {
        try {
          //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
          //UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
        }
        catch (Exception e) {
        }
      }  void table_keyPressed(KeyEvent e) {
        if(e.getKeyCode()==KeyEvent.VK_SPACE){// ((JLabel)nowComponent).setIcon(icon);
     JTable tab=(JTable)e.getComponent();int row=tab.getSelectedRow();
    int col=tab.getSelectedColumn();Component cell2 =render.getTableCellRendererComponent(tab, tab.getValueAt(row,col), false, false, 0, 1) ;
      JPanel jpl=(JPanel)cell2;
      JLabel jll=(JLabel)jpl.getComponent(0);  jll.setText("sadfkljdslkf");
      cell2.repaint();
        }  }
    }class Applet2_table_keyAdapter
        extends java.awt.event.KeyAdapter {
      Applet2 adaptee;  Applet2_table_keyAdapter(Applet2 adaptee) {
        this.adaptee = adaptee;
      }  public void keyPressed(KeyEvent e) {
        adaptee.table_keyPressed(e);
      }
    }
      

  6.   

    Component cell2 =render.getTableCellRendererComponent(tab, tab.getValueAt(row,col), false, false, 0, 1) ;是可以取对像,但经过:
      
      JPanel jpl=(JPanel)cell2;
      JLabel jll=(JLabel)jpl.getComponent(0);
      jll.setText("sadfkljdslkf");
      cell2.repaint();
    后,全部格里的jlabel都"setText为sadfkljdslkf" 了
      

  7.   

    估计全部格里都是同一个对像,所以取出一个cell2后,一改,全部都改了,请问怎办