每当我改变这行代码时:
scrollPane.setSize(new Dimension(500, 500));
到
scrollPane.setSize(new Dimension(500, 600));
我的JPanel中的对象(JPanel)发生了变化吗?
查看结构的方法是:
Objects: JScrollPanel( JPanel ( JPanel( ) ) ); names: scrollPanel ( tableCollection ( table1( ) ) )
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Toolkit;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class testing extends javax.swing.JFrame {
public testing () {
initComponents();
setMainFrameSize();
addScrollPane();
addTables();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addComponentListener(new java.awt.event.ComponentAdapter() {
public void componentResized(java.awt.event.ComponentEvent evt) {
formComponentResized(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void formComponentResized(java.awt.event.ComponentEvent evt) {
setScrollPaneSize();
setTableSize();
}
public static void main(String args[]) {
//<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(testing.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(testing.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(testing.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(testing.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new testing().setVisible(true);
}
});
}
JPanel tableCollection = new JPanel();
JScrollPane scrollPane = new JScrollPane(tableCollection, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
// Variables declaration - do not modify
// End of variables declaration
void setMainFrameSize() {
this.setMinimumSize(new Dimension(800, 600));
Dimension scnDimension = Toolkit.getDefaultToolkit().getScreenSize();
Insets scnInsets = Toolkit.getDefaultToolkit().getScreenInsets(getGraphicsConfiguration());
this.setSize(scnDimension.width, scnDimension.height-(scnInsets.bottom+scnInsets.top));
}
void addScrollPane() {
scrollPane.setBackground(Color.red);
setScrollPaneSize();
this.add(scrollPane);
addTableCollection();
}
void setScrollPaneSize() {
scrollPane.setSize(new Dimension(500, 500));
scrollPane.setLocation(0, barHeight);
}
void addTableCollection() {
tableCollection.setBackground(new Color(189, 195, 199));
setTableCollectionSize();
}
void setTableCollectionSize() {
tableCollection.setPreferredSize(new Dimension(550, 550));
}
void addTables() {
table1.setBackground(new Color(52, 152, 219));
tableCollection.add(table1);
}
void setTableSize() {
int tableHeight = tableCollection.getHeight()-tableHeightOffset*2;
table1.setSize(350, tableHeight);
table1.setLocation(0, 0);
}
int barHeight = 50;
int tableHeightOffset = 50;
int tableWidthOffset = 50;
int nTables = 5;
JPanel table1 = new JPanel();
}
答案 0 :(得分:-1)
只需添加tableCollection.setLayout(null);
即可在JPanel中处理自定义大小的JPanel
感谢Jonas JPanel inside another