【发布时间】:2015-04-07 06:35:25
【问题描述】:
每当我更改这行代码时:
scrollPane.setSize(new Dimension(500, 500));
到
scrollPane.setSize(new Dimension(500, 600));
我的 JPanel 中的我的对象 (JPanel) 发生了变化?
查看结构的一种方法是:
对象:JScrollPanel(JPanel(JPanel())); 名称:滚动面板(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();
}
【问题讨论】:
-
停止尝试使用
setLocation、setMinimum/Preferred/MaximumSize,您的容器应该在适当的布局管理器的控制之下。我也会避免GroupLayout,因为手动修改不是特别容易,但我就是这样 -
“为什么在 JScrollPanel 上滚动 JPanel 时会更改我在 JPanel 中的对象?” 因为您正在与布局管理器作斗争
-
好的,我需要完全控制布局中的对象,我无法避免...我将修改代码,使其更具可读性并重新表述问题...谢谢 4你的快速反应
-
现代 UI 中的像素完美布局是一种错觉。您无法控制在不同系统或不同字体之间更改组件大小的渲染过程
-
“我的 JPanel 中的我的对象 (JPanel) 发生了变化?” 它做了什么?这与您的预期有何不同?
标签: java swing jpanel jscrollpane