netbeans中的JLayeredPane

netbeans中的JLayeredPane,netbeans,jlayeredpane,Netbeans,Jlayeredpane,我试图将两个组件Jeditorpane和Jtextarea放在Jlayeredpane中。我正在使用Netbeans。我在jlayeredpane中添加了jeditorpane和jtextarea以及两个按钮。当我点击按钮1时,它会显示消息“你好,世界做得很好” 当点击按钮2时,它会显示信息“Hello world not doing good” 但当我点击按钮1时,它会显示消息“Hello world doing good”,但当我点击按钮2时,它不会显示消息“Hello world not

我试图将两个组件Jeditorpane和Jtextarea放在Jlayeredpane中。我正在使用Netbeans。我在jlayeredpane中添加了jeditorpane和jtextarea以及两个按钮。当我点击按钮1时,它会显示消息“你好,世界做得很好”

当点击按钮2时,它会显示信息“Hello world not doing good”

但当我点击按钮1时,它会显示消息“Hello world doing good”,但当我点击按钮2时,它不会显示消息“Hello world not doing good”,因为它会将组件移动到前面。谁能告诉我怎么解决这个问题吗。下面是源代码,部分由netbeans生成,部分由我编写。 多谢各位

public class test extends javax.swing.JFrame {

/** Creates new form test */
public test() {
    initComponents();
}

/** This method is called from within the constructor to
 * initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is
 * always regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    jLayeredPane1 = new javax.swing.JLayeredPane();
    jScrollPane1 = new javax.swing.JScrollPane();
    jEditorPane1 = new javax.swing.JEditorPane();
    jScrollPane2 = new javax.swing.JScrollPane();
    jTextArea1 = new javax.swing.JTextArea();
    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jLayeredPane1.add(jTextArea1);// i added this code using custom code property
    jLayeredPane1.add(jEditorPane1); //i added this code using custom code property 

    jEditorPane1.setText("");//i added this code using custom code property
    jScrollPane1.setViewportView(jEditorPane1);

    jScrollPane1.setBounds(0, 0, 480, 200);
    jLayeredPane1.add(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);

    jTextArea1.setColumns(20);
    jTextArea1.setRows(5);
    jTextArea1.setText("");//i added this code using custom code property of netbeans
    jScrollPane2.setViewportView(jTextArea1);

    jScrollPane2.setBounds(0, 0, 480, 200);
    jLayeredPane1.add(jScrollPane2, javax.swing.JLayeredPane.DEFAULT_LAYER);

    jButton1.setText("jButton1");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });

    jButton2.setText("jButton2");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton2ActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 505, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(jButton1)
                    .addGap(143, 143, 143)
                    .addComponent(jButton2)))
            .addContainerGap(68, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(42, 42, 42)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 228, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(33, 33, 33)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jButton1)
                .addComponent(jButton2))
            .addContainerGap(43, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    jTextArea1.setText("");
    jTextArea1.setOpaque(true);
    jLayeredPane1.moveToFront(jEditorPane1);
    jEditorPane1.setText("Hello world doing nice");
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    jEditorPane1.setText("");
    jEditorPane1.setOpaque(true);
    jLayeredPane1.moveToFront(jTextArea1);
    jTextArea1.setText("Hello world not doing good");
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new test().setVisible(true);
        }
    });
}

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JEditorPane jEditorPane1;
private javax.swing.JLayeredPane jLayeredPane1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration
  }
公共类测试扩展了javax.swing.JFrame{
/**创建新的表单测试*/
公开考试(){
初始化组件();
}
/**此方法从构造函数中调用,以
*初始化表单。
*警告:请勿修改此代码。此方法的内容为
*始终由表单编辑器重新生成。
*/
@抑制警告(“未选中”)
// 
私有组件(){
jLayeredPane1=newjavax.swing.JLayeredPane();
jScrollPane1=newjavax.swing.JScrollPane();
jEditorPane1=newjavax.swing.JEditorPane();
jScrollPane2=newjavax.swing.JScrollPane();
jTextArea1=newjavax.swing.JTextArea();
jButton1=newjavax.swing.JButton();
jButton2=newjavax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLayeredPane1.add(jTextArea1);//我使用自定义代码属性添加了此代码
jLayeredPane1.add(jEditorPane1);//我使用自定义代码属性添加了此代码
jEditorPane1.setText(“”;//我使用自定义代码属性添加了此代码
jScrollPane1.setViewportView(jEditorPane1);
jScrollPane1.setBounds(0,0480200);
添加(jScrollPane1,javax.swing.JLayeredPane.DEFAULT_层);
jTextArea1.设置列(20);
jTextArea1.设置行(5);
jTextArea1.setText(“”;//我使用netbeans的自定义代码属性添加了此代码
jScrollPane2.setViewportView(jTextArea1);
jScrollPane2.setBounds(0,0480200);
添加(jScrollPane2,javax.swing.JLayeredPane.DEFAULT_层);
setText(“jButton1”);
jButton1.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
jButton1ActionPerformed(evt);
}
});
setText(“jButton2”);
jButton2.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout=newjavax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(布局);
layout.setHorizontalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLayeredPane1,javax.swing.GroupLayout.PREFERRED\u SIZE,505,javax.swing.GroupLayout.PREFERRED\u SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1)
.addGap(143143143)
.addComponent(jButton2)))
.addContainerGap(68,简称最大值))
);
layout.setVerticalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(42,42,42)
.addComponent(jLayeredPane1,javax.swing.GroupLayout.PREFERRED\u SIZE,228,javax.swing.GroupLayout.PREFERRED\u SIZE)
.addGap(33,33,33)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addContainerGap(43,简称最大值))
);
包装();
}// 
私有void jButton1ActionPerformed(java.awt.event.ActionEvent evt){
//TODO在此处添加您的处理代码:
jTextArea1.setText(“”);
jTextArea1.setOpaque(true);
jLayeredPane1.移动到前面(绝地武士pane1);
jEditorPane1.setText(“你好,世界做得很好”);
}
私有void jButton2ActionPerformed(java.awt.event.ActionEvent evt){
//TODO在此处添加您的处理代码:
jEditorPane1.setText(“”);
jEditorPane1.setOpaque(真);
jLayeredPane1.向前移动(jTextArea1);
jTextArea1.setText(“你好,世界不好”);
}
/**
*@param指定命令行参数
*/
公共静态void main(字符串参数[]){
invokeLater(new Runnable()){
公开募捐{
新测试().setVisible(真);
}
});
}
//变量声明-不修改
私有javax.swing.JButton jButton1;
私有javax.swing.JButton jButton2;
私有javax.swing.JEditorPane jEditorPane1;
私有javax.swing.JLayeredPane jLayeredPane1;
私有javax.swing.JScrollPane-jScrollPane1;
私有javax.swing.JScrollPane-jScrollPane2;
私有javax.swing.JTextArea jTextArea1;
//变量结束声明
}

您可能应该改为使用。

这里是我问题的简化版本。我想重叠两个swing组件,jeditorpane和jtextarea。对于特定事件,只有jeditorpane的内容应该可见,而对于其他事件,jtextarea的内容应该可见。它就像在某个特定的时间,我们只有一个组件是可见的,而不是其他。
 private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    jEditorPane1.setText("");
    jEditorPane1.setOpaque(true);
    jLayeredPane1.moveToFront(jTextArea1);
    jTextArea1.setText("Hello world not doing good");
}
public class test extends javax.swing.JFrame {

/** Creates new form test */
public test() {
    initComponents();
}

/** This method is called from within the constructor to
 * initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is
 * always regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    jLayeredPane1 = new javax.swing.JLayeredPane();
    jScrollPane1 = new javax.swing.JScrollPane();
    jEditorPane1 = new javax.swing.JEditorPane();
    jScrollPane2 = new javax.swing.JScrollPane();
    jTextArea1 = new javax.swing.JTextArea();
    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jLayeredPane1.add(jTextArea1);// i added this code using custom code property
    jLayeredPane1.add(jEditorPane1); //i added this code using custom code property 

    jEditorPane1.setText("");//i added this code using custom code property
    jScrollPane1.setViewportView(jEditorPane1);

    jScrollPane1.setBounds(0, 0, 480, 200);
    jLayeredPane1.add(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);

    jTextArea1.setColumns(20);
    jTextArea1.setRows(5);
    jTextArea1.setText("");//i added this code using custom code property of netbeans
    jScrollPane2.setViewportView(jTextArea1);

    jScrollPane2.setBounds(0, 0, 480, 200);
    jLayeredPane1.add(jScrollPane2, javax.swing.JLayeredPane.DEFAULT_LAYER);

    jButton1.setText("jButton1");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });

    jButton2.setText("jButton2");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton2ActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 505, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(jButton1)
                    .addGap(143, 143, 143)
                    .addComponent(jButton2)))
            .addContainerGap(68, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(42, 42, 42)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 228, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(33, 33, 33)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jButton1)
                .addComponent(jButton2))
            .addContainerGap(43, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    jTextArea1.setText("");
    jTextArea1.setOpaque(true);
    jLayeredPane1.moveToFront(jEditorPane1);
    jEditorPane1.setText("Hello world doing nice");
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    jEditorPane1.setText("");
    jEditorPane1.setOpaque(true);
    jLayeredPane1.moveToFront(jTextArea1);
    jTextArea1.setText("Hello world not doing good");
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new test().setVisible(true);
        }
    });
}

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JEditorPane jEditorPane1;
private javax.swing.JLayeredPane jLayeredPane1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration
  }