首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Java pack()方法

使用Java pack()方法
EN

Stack Overflow用户
提问于 2014-09-10 06:19:45
回答 3查看 17.8K关注 0票数 1

我不能让pack()方法工作。我试过几种方法。我的代码目前是这样的:

第一类:

代码语言:javascript
复制
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();

第二类:

代码语言:javascript
复制
public class JavaGui extends javax.swing.JFrame 
    {
    public JavaGui()
        {
        getContentPane().setLayout(null);
        ..
        getContentPane().add(panelLeft);
        ...
        getContentPane().add(panelRight);

我试着把pack方法放在任何地方,但是它不能用这种方式添加gui元素。有什么建议吗?我还尝试将所有内容添加到JFrame中,而不是getContentPane()中,但我也无法实现这一点。

EN

回答 3

Stack Overflow用户

发布于 2014-09-10 06:21:41

  1. 不要将空布局与pack()一起使用。pack方法告诉布局管理器和组件优化自己的大小,如果你使用null布局,那么gui就有可能缩小到最小的尺寸,因为在大多数情况下根本没有together.
  2. Don't使用null布局的布局来保存它。使用这些工具可能会创建难以扩展、改进和调试的僵化图形用户界面。
  3. 请勿使用setSize(...)pack()。布局主要考虑组件的首选大小,而不是它们的大小。

而是:

  1. 使用一种令人愉快且合理的嵌套JPanels组合,每个组合都使用自己的布局管理器。
  2. 让组件和布局管理器调整themselves.
  3. Then包的大小应该会有所帮助。
  4. 我所做的一般顺序是将所有组件添加到图形用户界面,然后调用pack(),然后调用setLocationByPlatform(true) (我想),然后是setVisible(true).

要获得更好的帮助,请查看Swing Layout Manager Tutorials

下面是这个网站上使用各种布局管理器的其他问题的一些示例:

票数 15
EN

Stack Overflow用户

发布于 2014-09-10 08:01:42

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

如果不了解布局是如何工作的,就不能简单地构建一个行为良好的gui,所以做推荐的教程并查看Hovercraft Full Of Eels已经发布的示例是绝对必要的。

对于你的情况,我只是猜测你在做什么。因为你提到了左边和右边的面板,所以我推荐一个JSplitPane,它可以让你把屏幕分成两个区域,这两个区域的大小和方向都可以自定义。

代码语言:javascript
复制
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);
    }
}
票数 2
EN

Stack Overflow用户

发布于 2014-09-10 06:36:01

如果希望JFrame使用空布局,请重新排列代码,使其看起来如下所示:

代码语言:javascript
复制
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

Main:

代码语言:javascript
复制
public static void main(String[] args)
{
java.awt.EventQueue.invokeLater(new Runnable() {
    public void run()
    {
        new JavaGui().setVisible(true);
        // Do not do any formatting for your JFrame here
    }
});

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

您也可以使用其他布局。Null布局用于获取精确位置的像素,用于高级图形用户界面设计,例如创建自定义图形用户界面,但看起来您是在使用JPanels制作通用图形用户界面。为此,我建议使用GridBagLayout,如果调整了框架的大小,它会保持所有内容居中,并且易于使用。要使用GridBagLayout,您必须将setLayout(null);替换为setLayout(new GridBagLayout());并设置GridBagConstraints。以下是使用组件和GridBagLayout制作面板的一些示例代码

代码语言:javascript
复制
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
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25754454

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档