Java 为什么这段代码不在底部有三个按钮的滚动窗格中显示内容
我已经在视图类的末尾将JFrame设置为可见,并且不确定为什么我的自定义JPanel仍然不可见。我试图简化代码并避免使用大型视图类,同时实现良好的面向对象编程风格。我的主面板中JFrame底部的JButton是可见的。我尝试将自定义面板添加到框架中,但它们仍然不可见 我尝试将所有内容设置为可见,并且只将自定义JPanel添加到JFrameJava 为什么这段代码不在底部有三个按钮的滚动窗格中显示内容,java,user-interface,jframe,jpanel,Java,User Interface,Jframe,Jpanel,我已经在视图类的末尾将JFrame设置为可见,并且不确定为什么我的自定义JPanel仍然不可见。我试图简化代码并避免使用大型视图类,同时实现良好的面向对象编程风格。我的主面板中JFrame底部的JButton是可见的。我尝试将自定义面板添加到框架中,但它们仍然不可见 我尝试将所有内容设置为可见,并且只将自定义JPanel添加到JFrame public View(Main pMain) { setMain(pMain); panelClientInfo = new JClie
public View(Main pMain)
{
setMain(pMain);
panelClientInfo = new JClientPanel();
panelPaymentInfo = new JPaymentPanel();
panelJobDescription = new JJobPanel();
panelAgreement = new JAgreementPanel();
clearButton = new JButton("Clear");
exitButton = new JButton("Exit");
submitButton = new JButton("Submit");
panelSecondary = new JPanel();
panelMain = new JPanel();
scrollPane = new JScrollPane(panelMain);
panelSecondary.setLayout(new BoxLayout(panelSecondary,
BoxLayout.Y_AXIS));
panelSecondary.add(panelClientInfo);
panelSecondary.add(panelJobDescription);
panelSecondary.add(panelPaymentInfo);
panelSecondary.add(panelAgreement);
panelMain.add(panelSecondary, BorderLayout.CENTER);
panelMain.add(clearButton, BorderLayout.SOUTH);
panelMain.add(submitButton, BorderLayout.SOUTH);
panelMain.add(exitButton, BorderLayout.SOUTH);
scrollPane.add(panelMain);
scrollPane.setVisible(true);
setTitle("G.C. Septic Services Contract Drafter");
setSize(1000, 1000);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(scrollPane);
setVisible(true);
}
/**Here is a custom JPanel that I am trying to use*/
package contractDrafter;
import javax.swing.JPanel;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class JAgreementPanel extends JPanel
{
JPanel panelMain;
JLabel submitterLabel;
JTextField submitterText;
public JAgreementPanel()
{
panelMain = new JPanel();
panelMain.setLayout(new BoxLayout(panelMain, BoxLayout.Y_AXIS));
submitterLabel = new JLabel("Submitter Name: ");
submitterText = new JTextField("e.g: Calvin M. Cox", 30);
panelMain.add(Box.createVerticalGlue());
panelMain.add(submitterLabel);
panelMain.add(Box.createVerticalGlue());
panelMain.add(submitterText);
panelMain.add(Box.createVerticalGlue());
}
}
我想让这个程序显示不同的JPanel,这样我岳母所要做的就是在完成的程序中输入一些值,它会为她编写文件,以减少她患关节炎的手的工作量。我希望看到JPanel以一种半整洁到整洁的方式出现在框架上,这样她就可以在框架上上下滚动并输入必要的信息。您的代码包含许多缺少的类!!当你发布你的代码时,至少要和超类一起工作,这样我们才能理解它的作用 无论如何 我尝试将自定义面板添加到框架中,但它们仍然不可见 这确实与您的代码伙伴冲突!! 在代码中
panelMain.add(panelSecondary, BorderLayout.CENTER);
panelMain.add(clearButton, BorderLayout.SOUTH);
panelMain.add(submitButton, BorderLayout.SOUTH);
panelMain.add(exitButton, BorderLayout.SOUTH);
您传递的约束属于BorderLayout
,另一方面,您没有将布局设置为BorderLayout
,因此默认情况下它是FlowLayout
同样,即使是BorderLayout
添加相同的“border”,也会覆盖该边框中的最后一个组件
您还没有上传图像,但我可以想象按钮在中心水平对齐,这是因为JPanel
的默认FlowLayout
布局
我希望看到JPanel以一种半整洁到整洁的方式出现在画面上,这样她就可以在画面上上下滚动并输入必要的信息
好吧,您正在使滚动窗格包含面板和按钮,这是完全错误的(至少在您的设计案例中)
你应该做的是
JFrame f = new JFrame();
JPanel slidingPanel = new JPanel ();
slidingPanel.setLayout(new BoxLayout(slidingPanel,BoxLayout.Y_AXSIS));
JScrollPane scrollPane = new JScrollPanel (slidingPanel);
f.getContentPane().add(scrollpane,BorderLayout.CENTER);
//then add all of your panels in the slidingpanel
JPanel buttonPanel = new JPanel();
//i can't give you a hint on this , it's almost just designer choice for how you want your buttons to layout
//but add them to the south !!
f.getContentPane().add(buttonPanel,BorderLayout.SOUTH);
如果您的家庭还需要一些额外的项目人员,我很乐意帮助您,但stack overflow不适合您,请将您的项目上传到私人github回购中,或者如果您已经有一个邀请我,我的帐户与我在这里的帐户具有相同的详细信息(mate;) 您的代码包含大量缺少的类!!当你发布你的代码时,至少要和超类一起工作,这样我们才能理解它的作用 无论如何 我尝试将自定义面板添加到框架中,但它们仍然不可见 这确实与您的代码伙伴冲突!! 在代码中
panelMain.add(panelSecondary, BorderLayout.CENTER);
panelMain.add(clearButton, BorderLayout.SOUTH);
panelMain.add(submitButton, BorderLayout.SOUTH);
panelMain.add(exitButton, BorderLayout.SOUTH);
您传递的约束属于BorderLayout
,另一方面,您没有将布局设置为BorderLayout
,因此默认情况下它是FlowLayout
同样,即使是BorderLayout
添加相同的“border”,也会覆盖该边框中的最后一个组件
您还没有上传图像,但我可以想象按钮在中心水平对齐,这是因为JPanel
的默认FlowLayout
布局
我希望看到JPanel以一种半整洁到整洁的方式出现在画面上,这样她就可以在画面上上下滚动并输入必要的信息
好吧,您正在使滚动窗格包含面板和按钮,这是完全错误的(至少在您的设计案例中)
你应该做的是
JFrame f = new JFrame();
JPanel slidingPanel = new JPanel ();
slidingPanel.setLayout(new BoxLayout(slidingPanel,BoxLayout.Y_AXSIS));
JScrollPane scrollPane = new JScrollPanel (slidingPanel);
f.getContentPane().add(scrollpane,BorderLayout.CENTER);
//then add all of your panels in the slidingpanel
JPanel buttonPanel = new JPanel();
//i can't give you a hint on this , it's almost just designer choice for how you want your buttons to layout
//but add them to the south !!
f.getContentPane().add(buttonPanel,BorderLayout.SOUTH);
如果您的家庭还需要一些额外的项目人员,我很乐意帮助您,但stack overflow不适合您,请将您的项目上传到私人github回购中,或者如果您已经有一个邀请我,我的帐户与我在这里的帐户具有相同的详细信息(mate;)
所以我终于弄明白了这是什么。我要感谢@OverLoadedBurden的快速回复和帮助。我将只提供一个定制的JPanel类,因为其余的类非常相似,因此没有必要。每当我创建自定义JPanel时,面板都不会显示,因为我正在将内容添加到自定义JPanel中包含的面板中。例如,在我编写的旧代码中:
public JAgreementPanel()
{
panelMain = new JPanel();
panelMain.setLayout(new BoxLayout(panelMain, BoxLayout.Y_AXIS));
submitterLabel = new JLabel("Submitter Name: ");
submitterText = new JTextField("e.g: Calvin M. Cox", 30);
panelMain.add(Box.createVerticalGlue());
//This is where the error exists
panelMain.add(submitterLabel);
panelMain.add(Box.createVerticalGlue());
//This is where the error exists
panelMain.add(submitterText);
panelMain.add(Box.createVerticalGlue());
}
Whereas I should have been adding the desired content of the panel to the custom
panel
itself. This is the new correctly functioning code:
public JAgreementPanel()
{
panelMain = new JPanel();
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
submitterLabel = new JLabel("Submitter Name: ");
submitterText = new JTextField("e.g: Calvin M. Cox", 30);
//this could be also written as "this.add(submitterLabel)"
add(submitterLabel);
add(Box.createHorizontalGlue());
add(submitterText);
add(Box.createHorizontalGlue());
setVisible(true);
}
This could also be accomplished with the code comments as well. I will also include
the updated view constructor that is called to create the gui:
public View(Main pMain)
{
setMain(pMain);
clearButton = new JButton("Clear");
exitButton = new JButton("Exit");
submitButton = new JButton("Submit");
panelSecondary = new JPanel();
panelMain = new JPanel();
panelSecondary.setLayout(new BoxLayout(panelSecondary, BoxLayout.Y_AXIS));
panelSecondary.add(new JAgreementPanel());
panelSecondary.add(new JClientPanel());
panelSecondary.add(new JJobPanel());
panelSecondary.add(new JPaymentPanel());
panelMain.setLayout(new GridLayout(2,1));
panelMain.add(panelSecondary);
JPanel panelButtons = new JPanel();
panelButtons.add(exitButton);
panelButtons.add(clearButton);
panelButtons.add(submitButton);
panelMain.add(panelButtons);
scrollPane = new JScrollPane(panelMain);
scrollPane.setVisible(true);
add(panelMain);
setTitle("G.C. Septic Services Contract Drafter");
setSize(500, 500);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
所以我终于弄明白了这是什么。我要感谢@OverLoadedBurden的快速回复和帮助。我将只提供一个定制的JPanel类,因为其余的类非常相似,因此没有必要。每当我创建自定义JPanel时,面板都不会显示,因为我正在将内容添加到自定义JPanel中包含的面板中。例如,在我编写的旧代码中:
public JAgreementPanel()
{
panelMain = new JPanel();
panelMain.setLayout(new BoxLayout(panelMain, BoxLayout.Y_AXIS));
submitterLabel = new JLabel("Submitter Name: ");
submitterText = new JTextField("e.g: Calvin M. Cox", 30);
panelMain.add(Box.createVerticalGlue());
//This is where the error exists
panelMain.add(submitterLabel);
panelMain.add(Box.createVerticalGlue());
//This is where the error exists
panelMain.add(submitterText);
panelMain.add(Box.createVerticalGlue());
}
Whereas I should have been adding the desired content of the panel to the custom
panel
itself. This is the new correctly functioning code:
public JAgreementPanel()
{
panelMain = new JPanel();
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
submitterLabel = new JLabel("Submitter Name: ");
submitterText = new JTextField("e.g: Calvin M. Cox", 30);
//this could be also written as "this.add(submitterLabel)"
add(submitterLabel);
add(Box.createHorizontalGlue());
add(submitterText);
add(Box.createHorizontalGlue());
setVisible(true);
}
This could also be accomplished with the code comments as well. I will also include
the updated view constructor that is called to create the gui:
public View(Main pMain)
{
setMain(pMain);
clearButton = new JButton("Clear");
exitButton = new JButton("Exit");
submitButton = new JButton("Submit");
panelSecondary = new JPanel();
panelMain = new JPanel();
panelSecondary.setLayout(new BoxLayout(panelSecondary, BoxLayout.Y_AXIS));
panelSecondary.add(new JAgreementPanel());
panelSecondary.add(new JClientPanel());
panelSecondary.add(new JJobPanel());
panelSecondary.add(new JPaymentPanel());
panelMain.setLayout(new GridLayout(2,1));
panelMain.add(panelSecondary);
JPanel panelButtons = new JPanel();
panelButtons.add(exitButton);
panelButtons.add(clearButton);
panelButtons.add(submitButton);
panelMain.add(panelButtons);
scrollPane = new JScrollPane(panelMain);
scrollPane.setVisible(true);
add(panelMain);
setTitle("G.C. Septic Services Contract Drafter");
setSize(500, 500);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
我很乐意在其他地方与您联系!我已经尝试了你建议的代码,但仍然没有显示任何内容。我的github用户名:是tylerdixon117right我现在不是有效的编码环境,但我查找了您,该项目不在您的任何回购上!上传这个项目,这样我就可以@TylerjDixon查看它。我想我可能已经发现了这个问题,我会尽快给你回复,只要我能将它插入eclipse。我现在正在工作。再次感谢你的帮助!我很乐意在其他地方与您联系!我已经尝试了你建议的代码,但仍然没有显示任何内容。我的github用户名:是tylerdixon117right我现在不是有效的编码环境,但我查找了您,该项目不在您的任何回购上!上传这个项目,这样我就可以@TylerjDixon查看它。我想我可能已经发现了这个问题,我会尽快给你回复,只要我能将它插入eclipse。我现在正在工作。再次感谢你的帮助!