使用Java pack()方法

使用Java pack()方法,java,swing,layout-manager,pack,Java,Swing,Layout Manager,Pack,我无法使pack()方法工作。我试过几种方法。目前我的代码如下所示: 第1类: public static void main( String[] args ) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { JavaGui mygui = new JavaGui(); // mygui.setSize(1154, 75

我无法使pack()方法工作。我试过几种方法。目前我的代码如下所示:

第1类:

public static void main( String[] args )
    {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run()
        {
         JavaGui mygui = new JavaGui();
   //       mygui.setSize(1154, 753);
            mygui.setVisible(true);
            mygui.pack();
第2类:

public class JavaGui extends javax.swing.JFrame 
    {
    public JavaGui()
        {
        getContentPane().setLayout(null);
        ..
        getContentPane().add(panelLeft);
        ...
        getContentPane().add(panelRight);
我试着将pack方法放在所有地方,但这种添加gui元素的方式不起作用。有什么建议吗?我还尝试将所有内容添加到JFrame而不是getContentPane(),但我也无法做到这一点

  • 不要将空布局与
    pack()
    一起使用。pack方法告诉布局管理器和组件以最佳方式调整自己的大小,如果改用空布局,那么gui可能会缩小到最小大小,因为没有布局将其固定在一起
  • 在大多数情况下,不要使用空布局。使用这些可能会导致您创建几乎不可能扩展、改进和调试的僵化GUI
  • 不要使用
    setSize(…)
    pack()
    。布局主要考虑组件的首选尺寸,而不是它们的尺寸
  • 相反:

  • 使用嵌套JPanel的令人愉快和合理的组合,每个都使用自己的布局管理器
  • 让组件和布局管理器自行调整大小
  • 那么背包应该有帮助
  • 我所做的一般顺序是将所有组件添加到GUI,然后调用
    pack()
    ,然后调用
    setLocationByPlatform(true)
    (我想),然后调用
    setVisible(true)
  • 要获得更好的帮助,请查看

    以下是本网站上使用各种布局管理器的其他问题的几个示例:

  • 不要将空布局与
    pack()
    一起使用。pack方法告诉布局管理器和组件以最佳方式调整自己的大小,如果改用空布局,那么gui可能会缩小到最小大小,因为没有布局将其固定在一起
  • 在大多数情况下,不要使用空布局。使用这些可能会导致您创建几乎不可能扩展、改进和调试的僵化GUI
  • 不要使用
    setSize(…)
    pack()
    。布局主要考虑组件的首选尺寸,而不是它们的尺寸
  • 相反:

  • 使用嵌套JPanel的令人愉快和合理的组合,每个都使用自己的布局管理器
  • 让组件和布局管理器自行调整大小
  • 那么背包应该有帮助
  • 我所做的一般顺序是将所有组件添加到GUI,然后调用
    pack()
    ,然后调用
    setLocationByPlatform(true)
    (我想),然后调用
    setVisible(true)
  • 要获得更好的帮助,请查看

    以下是本网站上使用各种布局管理器的其他问题的几个示例:


    如果希望您的
    JFrame
    使用空布局,请重新排列代码,使其看起来像这样:

    public class JavaGui extends javax.swing.JFrame 
    {
    public JavaGui()
    {
        setMinimumSize(1154, 753); // Make sure you do setMinimumSize() instead of setSize() when using pack() so that the JFrame does not shrink to 0 size
        setLayout(null);
        add(panelLeft);
        add(panelRight);
        pack();
    }
    // Next is main method
    
    主要内容:

    之前,您在将
    JFrame
    设置为可见后修改它,因此除了pack()之外,这通常不起作用。如果您使用的是匿名内部类,则
    JFrame
    的所有组件和设置不应位于main方法中

    您还可以使用其他布局。空布局用于获取精确位置的像素,用于高级GUI设计,如创建自定义GUI,但您似乎正在使用JPanels创建通用GUI。为此,我建议使用a,如果调整了框架的大小并且易于使用,它将使所有内容都居中。要使用
    GridBagLayout
    ,必须替换
    setLayout(null)带有
    setLayout(新的GridBagLayout())并设置
    GridBagConstraints
    。下面是一些使用组件和
    GridBagLayout
    制作面板的示例代码:

    JPanel pane = new JPanel(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    if (shouldFill) {
                //natural height, maximum width
                c.fill = GridBagConstraints.HORIZONTAL;
    }
    //For each component to be added to this container:
    //...Create the component...
    //...Set instance variables in the GridBagConstraints instance...
    pane.add(theComponent, c);
    // Source: Oracle Docs
    

    如果希望
    JFrame
    使用空布局,请重新排列代码,使其看起来像这样:

    public class JavaGui extends javax.swing.JFrame 
    {
    public JavaGui()
    {
        setMinimumSize(1154, 753); // Make sure you do setMinimumSize() instead of setSize() when using pack() so that the JFrame does not shrink to 0 size
        setLayout(null);
        add(panelLeft);
        add(panelRight);
        pack();
    }
    // Next is main method
    
    主要内容:

    之前,您在将
    JFrame
    设置为可见后修改它,因此除了pack()之外,这通常不起作用。如果您使用的是匿名内部类,则
    JFrame
    的所有组件和设置不应位于main方法中

    您还可以使用其他布局。空布局用于获取精确位置的像素,用于高级GUI设计,如创建自定义GUI,但您似乎正在使用JPanels创建通用GUI。为此,我建议使用a,如果调整了框架的大小并且易于使用,它将使所有内容都居中。要使用
    GridBagLayout
    ,必须替换
    setLayout(null)带有
    setLayout(新的GridBagLayout())并设置
    GridBagConstraints
    。下面是一些使用组件和
    GridBagLayout
    制作面板的示例代码:

    JPanel pane = new JPanel(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    if (shouldFill) {
                //natural height, maximum width
                c.fill = GridBagConstraints.HORIZONTAL;
    }
    //For each component to be added to this container:
    //...Create the component...
    //...Set instance variables in the GridBagConstraints instance...
    pane.add(theComponent, c);
    // Source: Oracle Docs
    

    我建议构建swing gui的初学者使用一个好的ide和内置的gui设计器,比如eclipse和windowbuilder,或者matisse的netbeans。它将帮助您构建所需gui的原型,并让您了解如何在源代码中完成布局。 尝试不同的布局,以及当某些值发生变化时会发生什么

    在不了解布局如何工作的情况下,不可能简单地构建一个性能良好的gui,因此完成推荐的教程并查看装满鳗鱼的气垫船已经发布的示例是绝对必要的

    就你的情况而言,我猜你在干什么。因为你提到的是左右面板,所以我建议使用JSplitPane,它可以将屏幕分为两个区域,两个区域的大小和方向都可以自定义

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JSplitPane;
    
    public class JavaGui extends JFrame {
    
        //SerialVersionId http://stackoverflow.com/questions/285793/what-is-a-serialversionuid-and-why-should-i-use-it
        private static final long   serialVersionUID    = 1L;
    
        public static void main(String[] args) {
            //Calls to Gui Code must happen on the event dispatch thread that the gui does not get stuck
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new JavaGui().setVisible(true);
                }
            });
        }       
    
        public JavaGui() {
            // Set the desired size of the frame to determine the maximum size of its components
            setPreferredSize(new Dimension(1024, 768));
    
            // Set the default close operation, if press x on frame, destroy the frame and exit the application - others are just destroy the frame or just hide the
            // frame
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            // BorderLayout because we just need a centric gui with one component, here JSplitPane in full size
            getContentPane().setLayout(new BorderLayout(0, 0));
    
            // JsplitPane is a bit special as it depends on the divider location between both panels, for the sake of a small example we take the default -1,
            JSplitPane splitPane = new JSplitPane();
            // 0.5 divides extra space equally to left and right component when resizing the frame - so specifiying a size for the left and right component is not
            // necessary
            // use the divider location default -1 to let the width of the left component decide where the right component begins, in that case because of the
            // resize weight half and half
            splitPane.setDividerLocation(-1);
            splitPane.setResizeWeight(0.5);
            getContentPane().add(splitPane, BorderLayout.CENTER);
    
            // For the panels the same layout as default as the intention is not stated in your question
            JPanel leftPanel = new JPanel();
            splitPane.setLeftComponent(leftPanel);
            leftPanel.setLayout(new BorderLayout(0, 0));
    
            JPanel rightPanel = new JPanel();
            splitPane.setRightComponent(rightPanel);
            rightPanel.setLayout(new BorderLayout(0, 0));
    
            // Add a button Panel to the south for doing something - flow layout for letting the components flow to the right side
            JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
            getContentPane().add(buttonPanel, BorderLayout.SOUTH);
    
            // Close Button for closing the frame
            JButton btnExit = new JButton("Destroy this frame, but let application run");
            btnExit.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    dispose();
                }
            });
            buttonPanel.add(btnExit);
    
            // Set every component to its preferred size
            pack();
    
            // Make it visible
            setVisible(true);
        }
    }
    

    我建议构建swing gui的初学者使用一个好的ide和内置的gui设计器,比如eclipse和windowbuilder,或者matisse的netbeans。它将帮助您构建所需gui的原型,并让您了解如何在源代码中完成布局。 费用