Java 如何防止JPanel调整大小以填充JScrollPane
使用Java,我创建了一个带有GridBagLayout的表单。GUI位于JPanel中,JPanel位于添加到JFrame的滚动窗格中 我希望带有GUI的JPanel具有固定的大小,以防止表单被拉伸,但是,我希望JScrollPanel能够根据用户的偏好调整大小。(类似于MS Paint,画布是我的JPanel)。我想要防止表单被拉伸的原因是因为我希望它打印,并且从我现在的方式来看,打印的表单几乎按照我想要的方式打印,但是如果在打印之前调整了框架的大小,则会发生不利的变化 也许我的问题最好用我的打印方式来解决,但问题是我以前从未处理过这种打印级别,无法找到类似的问题/教程来帮助我解决问题。我要走的方向可能是“穷人的方式”,但这是目前唯一对我有意义的方式,我是一个相当新手的Java程序员 FrontierMain.javaJava 如何防止JPanel调整大小以填充JScrollPane,java,swing,jpanel,jscrollpane,Java,Swing,Jpanel,Jscrollpane,使用Java,我创建了一个带有GridBagLayout的表单。GUI位于JPanel中,JPanel位于添加到JFrame的滚动窗格中 我希望带有GUI的JPanel具有固定的大小,以防止表单被拉伸,但是,我希望JScrollPanel能够根据用户的偏好调整大小。(类似于MS Paint,画布是我的JPanel)。我想要防止表单被拉伸的原因是因为我希望它打印,并且从我现在的方式来看,打印的表单几乎按照我想要的方式打印,但是如果在打印之前调整了框架的大小,则会发生不利的变化 也许我的问题最好用我
public class FrontierMain {
public FrontierMain(){
JFrame frame = new JFrame();
frame.setTitle("Frontier Insulation Labor Record Tool");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
LaborRecordPanel recordPanel = new LaborRecordPanel();
frame.add(recordPanel.scrollPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run(){
FrontierMain fm = new FrontierMain();
}
});
System.out.println("Hello, World");
}
}
LaborRecordPanel.java
公共类LaborRecordPanel实现
可打印,ActionListener{
私有颜色着色=新颜色(201201);//着色单元格的颜色
私有JLabel数据空间[]=新JLabel[7];
私有JLabel grandTotalSpace=新JLabel();
私有JLabel locAndDescSpace=新JLabel();
私有JLabel personnelSpace[]=新JLabel[10];//空单元格
私有JLabel calendarGridLines[]=新JLabel[300];//网格线的空标签
私有ImageIcon logoIcon=新的ImageIcon(“Images/fici_logo1.jpg”);
专用JLabel logoLabel=新JLabel(logoIcon);
私人JLabel authorizedBy=新JLabel(“授权人:”);
private JLabel toCertify=新JLabel(“兹证明上述劳动已完成”);
私人JLabel laborRecordNO=新JLabel(“编号”);
private JLabel nameOfJob=新JLabel(“作业名称:”);
private JLabel customerPO=新JLabel(“客户PO:”;
私人JLabel合同编号=新JLabel(“合同编号”);
私人JLabel周末=新JLabel(“周末”);
私人JLabel personellist=新JLabel(“以下列出监督和
手工劳动:”);
专用JLabel locAndDescriptionLabel=新JLabel(“位置和说明:”);
私人JLabel personnelTitle=新JLabel(“头衔”);
私人JLabel supt=新JLabel(“supt”);
私人JLabel工头=新JLabel(“工头”);
私有JLabel[]mechanical=新JLabel[8];
私人JLabel calendarTitle=新JLabel(“现场工作小时数”);
私人JLabel dayHeading=新JLabel(“日”);
私有JLabel dateHeading=新JLabel(“日期”);
私有JLabel[]ot2=新JLabel[10];
私有JLabel[]ot1=新JLabel[10];
私有JLabel[]st=新JLabel[10];
私有JLabel mon=新JLabel(“mon”);
私人JLabel周二=新JLabel(“周二”);
私有JLabel wed=新JLabel(“wed”);
私人JLabel-thur=新JLabel(“thur”);
私人JLabel fri=新JLabel(“fri”);
专用JLabel sat=新JLabel(“sat”);
私人JLabel sun=新JLabel(“sun”);
专用JLabel总小时数=新JLabel(“总小时数”);
私人JLabel RATE PER HOUR=新JLabel(“RATE
PER
HOUR”);
私人JLabel totalAmount=新JLabel(“总额
金额”);
私人JLabel grandTotal=新JLabel(“总计”);
JPanel rp=新的JPanel();
JScrollPane scrollPane=新的JScrollPane(rp);
实验室记录面板(){
rp.setPreferredSize(新尺寸(12951830));
rp.setMinimumSize(新尺寸(12951830));
rp.setMaximumSize(新尺寸(12951830));
scrollPane.setPreferredSize(新维度(900700));
scrollPane.getVerticalScrollBar().setUnitIncrement(16);//增加滚动速度
对于JPanel上的(int i=0;i),您可以同时使用:
setSize( w, h)
setPreferredSize()
或者可以添加一个组件侦听器来手动处理调整大小事件时的“重新绘制/无效”
JPanel component = new JPanel();
component.addComponentListener(new ComponentListener()
{
public void componentResized(ComponentEvent evt) {
Component c = (Component)evt.getSource();
//........
}
});
我将我的主GUI面板放在另一个JPanel中,它位于JScrollPane中。这可以防止我的主GUI调整到框架大小,而不是“外部”JPanel是需要调整大小的工具。我读到,使用setSize似乎通常不受欢迎,所以我使用serPreferredSize。此外,根据这个答案:,JPanel不可调整大小,因为它是用户
JPanel component = new JPanel();
component.addComponentListener(new ComponentListener()
{
public void componentResized(ComponentEvent evt) {
Component c = (Component)evt.getSource();
//........
}
});