Netbeans 中的 JLayeredPane
我试图将两个组件 Jeditorpane 和 Jtextarea 放入 Jlayeredpane 中。我正在使用 Netbeans。我在 jlayeredpane 中添加了 jeditorpane 和 jtextarea 以及两个按钮。当我点击button1时,它应该显示消息“Hello world做得很好”。
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");
}
当单击按钮2时,它应该显示消息“Hello world not getting good”。
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");
}
但是当我单击button1时,它会显示消息“Hello world做得很好”,但是当我单击button2时,它不会显示消息“Hello world做得不好”,因为它应该将组件移到前面。有人可以告诉我如何解决这个问题吗?这是源代码,部分由 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
}
I am trying to put two components Jeditorpane and Jtextarea in Jlayeredpane. I am using Netbeans. I added jeditorpane and jtextarea in jlayeredpane and two buttons. When i click on button1 then it should show message "Hello world doing nice".
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");
}
and when click on button2 then it should show message "Hello world not doing good".
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");
}
But when i click on button1 then it shows message "Hello world doing nice" but when i click on button2 then it does not shows message "Hello world not doing good" as it should move the component to front. Could some one please tell me how to solve this. Here is source code which is partially generated by netbeans and partially written by me.
Thank you.
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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能应该使用 CardLayout 代替。
You should probably be using CardLayout instead.