大家好我现在有一个问题想请教大家!我是用SWing画的两个contentPane!
在第一个contentPane上我画的一个按钮"单击我".在第二个上面我画的一个 jTextField1
我现在想呀实现当我单击"单击我"时可以弹出第二个contentPane,在jTextField1
显示出"I love java"的字样!
问题就是我现在单击"单击我"但是并不弹出第二个contentPane!我想问问是怎么回事!是不是接口的问题咯
程序在下面请大家指教指教!
谢谢
我用的是jbpackage untitled4;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;public class Frame1 extends JFrame {
  JPanel contentPane;
  JPanel contentPane1 = new JPanel();
  XYLayout xYLayout1 = new XYLayout();
  XYLayout xYLayout2 = new XYLayout();
  JButton jButton1 = new JButton();
  JTextField jTextField1 = new JTextField();  //Construct the frame
  public Frame1() {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }  //Component initialization
  private void jbInit() throws Exception  {
    contentPane = (JPanel) this.getContentPane();
    jButton1.setText("单击我");
    jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
    contentPane1.setLayout(xYLayout2);
    contentPane.setLayout(xYLayout1);
    this.setSize(new Dimension(400, 300));
    this.setTitle("Frame Title");
    jTextField1.setText("");
    contentPane.add(jButton1, new XYConstraints(106, 127, 201, 54));
    contentPane1.add(jTextField1, new XYConstraints(88, 110, 218, 38));
  }  //Overridden so we can exit when window is closed
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
    }
  }  void jButton1_actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="单击我")
      jTextField1.setText("I love java"); 
  }
}class Frame1_jButton1_actionAdapter implements java.awt.event.ActionListener {
  Frame1 adaptee;  Frame1_jButton1_actionAdapter(Frame1 adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton1_actionPerformed(e);
  }
}

解决方案 »

  1.   

    if(e.getActionCommand()=="单击我"){
          jTextField1.setText("I love java"); 
          contentPane1.setVisible(true);
    }
      

  2.   

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import com.borland.jbcl.layout.*;public class Frame1
        extends JFrame implements ActionListener {
        JPanel contentPane = null;
        XYLayout xYLayout1 = new XYLayout();
        XYLayout xYLayout2 = new XYLayout();
        JButton jButton1 = new JButton();
        JTextField jTextField1 = new JTextField();    //Construct the frame
        public Frame1() {
            enableEvents(AWTEvent.WINDOW_EVENT_MASK);
            try {
                jbInit();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }    //Component initialization
        private void jbInit() throws Exception {
            contentPane = (JPanel)this.getContentPane();
            jButton1.setText("单击我");
            jButton1.addActionListener(this);
            contentPane.setLayout(xYLayout1);
            this.setSize(new Dimension(400, 300));
            this.setTitle("Frame Title");
            jTextField1.setText("");
            contentPane.add(jButton1, new XYConstraints(106, 127, 201, 54));
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setVisible(true);
        }    public static void main(String[] args) { //为了测试方便,加一个main方法
            new Frame1();
        }    public void actionPerformed(ActionEvent actionEvent) { //点击按钮的动作
            contentPane.removeAll(); //移除组件内所有其它组件
            contentPane.add(jTextField1, new XYConstraints(88, 110, 218, 38)); //加入新的组件
            jTextField1.setText("I Love Java !!!");
            this.repaint(); //重新描绘JFrame窗口
            //重新布局使新的组件显示出来
            this.invalidate();
            this.validate();
        }
    }