问题:我想知道Netbeans在设计时使用的大小(preferredSize)?
jtpWindows = new javax.swing.JTabbedPane(); // Contains jPHoriz
jPHoriz = new javax.swing.JPanel(); // Contains jPHorizGraph
jPHorizGraph = new javax.swing.JPanel(); // Contains jPHorizGraph01, jPHorizGap and jPHorizGraph02
jPHorizGraph01 = new javax.swing.JPanel();
jPHorizGap = new javax.swing.JPanel();
jPHorizGraph02 = new javax.swing.JPanel();
//现在如何包含
jPHorizGraph01
//jPHorizGraph01 Upper
jPHorizGraph01.setPreferredSize(new java.awt.Dimension(1679, 323));
javax.swing.GroupLayout jPHorizGraph01Layout = new javax.swing.GroupLayout(jPHorizGraph01);
jPHorizGraph01.setLayout(jPHorizGraph01Layout);
jPHorizGraph01Layout.setHorizontalGroup(
jPHorizGraph01Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
jPHorizGraph01Layout.setVerticalGroup(
jPHorizGraph01Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 323, Short.MAX_VALUE)
);
jPHorizGap
//jPHorizGap is a vertical separator between jPHorizGraph01 and jPHorizGraph02
jPHorizGap.setBorder(javax.swing.BorderFactory.createEtchedBorder());
jPHorizGap.setPreferredSize(new java.awt.Dimension(1679, 12));
javax.swing.GroupLayout jPHorizGapLayout = new javax.swing.GroupLayout(jPHorizGap);
jPHorizGap.setLayout(jPHorizGapLayout);
jPHorizGapLayout.setHorizontalGroup(
jPHorizGapLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
jPHorizGapLayout.setVerticalGroup(
jPHorizGapLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 8, Short.MAX_VALUE)
);
jPHorizGraph02
//jPHorizGraph02 Lower
jPHorizGraph02.setPreferredSize(new java.awt.Dimension(1679, 323));
javax.swing.GroupLayout jPHorizGraph02Layout = new javax.swing.GroupLayout(jPHorizGraph02);
jPHorizGraph02.setLayout(jPHorizGraph02Layout);
jPHorizGraph02Layout.setHorizontalGroup(
jPHorizGraph02Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
jPHorizGraph02Layout.setVerticalGroup(
jPHorizGraph02Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 323, Short.MAX_VALUE)
);
插入 jPHorizGraph01
, jPHorizGap
和 jPHorizGraph02
jPHorizGraph
javax.swing.GroupLayout jPHorizGraphLayout = new javax.swing.GroupLayout(jPHorizGraph);
jPHorizGraph.setLayout(jPHorizGraphLayout);
jPHorizGraphLayout.setHorizontalGroup(
jPHorizGraphLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPHorizGraph01, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPHorizGraph02, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPHorizGap, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
jPHorizGraphLayout.setVerticalGroup(
jPHorizGraphLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPHorizGraphLayout.createSequentialGroup()
.addComponent(jPHorizGraph01, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(0, 0, 0)
.addComponent(jPHorizGap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, 0)
.addComponent(jPHorizGraph02, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
在 jPHorizGraph
jPHoriz
javax.swing.GroupLayout jPHorizLayout = new javax.swing.GroupLayout(jPHoriz);
jPHoriz.setLayout(jPHorizLayout);
jPHorizLayout.setHorizontalGroup(
jPHorizLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPHorizGraph, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
jPHorizLayout.setVerticalGroup(
jPHorizLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPHorizLayout.createSequentialGroup()
.addComponent(jPHorizGraph, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
在 jPHoriz
jtpWindows
jtpWindows.addTab("Time", jPHoriz);
在 jtpWindows
中插入 jframe
与其他对象
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jpMain, javax.swing.GroupLayout.DEFAULT_SIZE, 1689, Short.MAX_VALUE)
.addComponent(jpReadFile, javax.swing.GroupLayout.DEFAULT_SIZE, 1689, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(2, 2, 2)
.addComponent(jtpWindows)
.addGap(3, 3, 3)))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jpMain, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(2, 2, 2)
.addComponent(jpReadFile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(752, Short.MAX_VALUE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(56, 56, 56)
.addComponent(jtpWindows)
.addGap(0, 0, 0)))
);
这不是代码......只是解释!
[Width, Height]
JFrame.preferredSize = [1689, 806]
javax.swing.JTabbedPane jtpWindows.preferredSize = [1684, 750]
javax.swing.JPanel jPHoriz.preferredSize = [1679, 722]
javax.swing.JPanel jPHorizGraph.preferredSize = [1679, 669]
javax.swing.JPanel jPHorizGraph01.preferredSize = [1679, 323]
javax.swing.JPanel jPHorizGap.preferredSize = [1679, 12]
javax.swing.JPanel jPHorizGraph02.preferredSize = [1679, 323]
会发生什么?答案:当我在面板中绘图时,我需要面板的尺寸,因为我的绘图是剪切图像......
问题:我需要在启动时获取JFrame的值并更改值,我该怎么做?
当用户更改Jframe的尺寸时,我需要调整 jPHorizGraph01 和 jPHorizGraph02 的宽度和高度
问题我需要使用哪些事件来检测JFrame大小的变化,以及我需要在JPanel中更改哪些值?
EDITION 1
问题2 :jPHorizGraph01,jPHorizGap和jPHorizGraph02垂直排列。
jPHorizGap的高度永久相等,不应该改变!!! jPHorizGraph01 的高度必须等于 jPHorizGraph02 !
如果 jPHorizGraph 的高度发生变化,那么 jPHorizGraph01 和 jPHorizGraph02 的高度必须在这两个对象之间按比例改变!!! / p>
答案 0 :(得分:0)
The Java Tutorials: How To Write a Component Listener说(部分),
对处理组件事件感兴趣的类要么实现此接口及其包含的所有方法,要么扩展抽象
ComponentAdapter
类,仅覆盖感兴趣的方法。然后,使用组件的addComponentListener
方法向组件注册从该类创建的侦听器对象。当组件的大小,位置或可见性发生更改时,将调用侦听器对象中的相关方法,并将ComponentEvent
传递给它。隐藏组件,使组件可见,移动或调整大小后,
Component
对象会触发一个或多个组件事件。
您可能(例如)显示类似
的消息public void componentResized(ComponentEvent e) {
Component c = e.getComponent();
displayMessage(String.format("%s - %d, %d", c.getClass().getName(),
c.getWidth(), c.getHeight()));
}