java - 扩展 JPanel 的类未在 JFrame 中显示
问题描述
我有两个类,Interfaz
extendsJFrame
和Contenido
extends JPanel
。我只是想将一个Contenido
对象添加到Interfaz
JFrame
(我在Interfaz
's 的构造函数中),但是当我运行代码(Interfaz
是我的主类)时,会出现Interfaz
's 的JFrame
窗口,但Contenido
JPanel
不会出现在那里,即使我将它添加到JFrame
. 有人知道为什么会这样吗?theInterfaz
JFrame
是空的,但Contenido
JPanel
有 aJLabel
和 a JTextArea
。我还这样做了,每当我单击JPanel
.
抱歉,代码很长,我对这两个类都使用了 netbeans 的 GUI 构建器,它添加了很多代码。
Interfaz.java
package contenidodirectorio;
import javax.swing.JComponent;
import javax.swing.JPanel;
public class Interfaz extends javax.swing.JFrame {
public Interfaz() {
initComponents();
contenidoPanel = new Contenido();
add(contenidoPanel);
contenidoPanel.setVisible(true);
pack();
}
/**
* 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() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 502, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 420, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Interfaz.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Interfaz.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Interfaz.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Interfaz.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Interfaz().setVisible(true);
}
});
}
// Variables declaration - do not modify
// End of variables declaration
private JPanel contenidoPanel;
}
Contenido.java
package contenidodirectorio;
public class Contenido extends javax.swing.JPanel {
public Contenido() {
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() {
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jLabel1 = new javax.swing.JLabel();
addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
formMouseClicked(evt);
}
});
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
jLabel1.setText("Directorio:");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(48, Short.MAX_VALUE)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 224, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(48, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(101, 101, 101)
.addComponent(jLabel1))
.addGroup(layout.createSequentialGroup()
.addGap(82, 82, 82)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(156, Short.MAX_VALUE))
);
}// </editor-fold>
private void formMouseClicked(java.awt.event.MouseEvent evt) {
System.out.println("Clicked.");
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration
解决方案
推荐阅读
- sql - 如何打印postgres表输出,以便区分空字符串和null
- youtube-api - 我想使用 YouTube Data API 获取播放列表观看次数
- .net - 为什么 log4net 不从 finalize 方法记录?
- multithreading - 线程与进程:可视化是否正确?
- android - 无法通过编程方式设置设备所有者
- javascript - 未捕获的类型错误:无法在 prac.js:3 处读取 null 的属性“样式”
- c - 如何在 C 中读取 CSV 文件
- android - 在 kotlin native 中读/写文件 - IOS 端
- deep-learning - 了解何时在 pytorch 中调用 zero_grad(),当训练有多个损失时
- android - 错误的 Android Studio 预览行为