【问题标题】:Issues in locating elements and re sizing定位元素和调整大小的问题
【发布时间】:2012-11-09 22:01:05
【问题描述】:

请看下面的代码

向导面板

package wizardGUI;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class WizardPanel extends JDialog
{
    private JPanel cardPanel, buttonPanel;
    private JButton next,previous;
    private CardLayout c1;

    private FileSelector fileSelector;
    private DelemeterSelector delemeterSelector;

    private int count = 1;

    public WizardPanel()
    {
        //Intializing instance variables
        fileSelector = FileSelector.getInstance();
        delemeterSelector = DelemeterSelector.getInstance();

        cardPanel = new JPanel();
        c1 = new CardLayout();
        cardPanel.setLayout(c1);

        cardPanel.add(fileSelector,"1");
        cardPanel.add(delemeterSelector,"2");

        c1.show(cardPanel, "1");;


        buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

        next = new JButton("Next");
        next.addActionListener(new NextButtonAction());
        previous = new JButton("Previous");

        buttonPanel.add(next);
        buttonPanel.add(previous);

        //Creating the GUI
        this.setLayout(new BorderLayout());
        this.add(cardPanel,"Center");
        this.add(buttonPanel,"South");

        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setResizable(true);
        this.pack();
        this.setVisible(true);

    }

    private class NextButtonAction implements ActionListener
    {
        public void actionPerformed(ActionEvent ae)
        {

                c1.show(cardPanel, "2");

        }
    }
}

文件选择器

package wizardGUI;

/*This is the first panel is wazard GUI. Using this window user can select the correct file
  which contains the data required to create the table
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class FileSelector extends JPanel
{
    private JLabel fileName, description;
    private JTextField fileTxt;
    private JButton browse;

    private GridBagLayout gbl;
    private GridBagConstraints gbc;

    private static FileSelector instance = null;

    private FileSelector()
    {
        //Intializing instance variables
        fileName = new JLabel("File Name: ");
        description = new JLabel("Specify the source of the data");

        fileTxt = new JTextField(10);

        browse = new JButton("Browse");

        gbl = new GridBagLayout();
        gbc = new GridBagConstraints();

        //Creating GUI
        this.setLayout(gbl);

        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.weightx = 0.0;
        gbc.weighty = 0.0;
        gbc.fill = GridBagConstraints.BOTH;
        this.add(description,gbc);

        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.weightx = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(0,10,0,0);
        this.add(locationPanel(),gbc);

        this.setBorder(BorderFactory.createEmptyBorder());
    }

    private JPanel locationPanel()
    {
        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout());

        panel.add(fileName);
        panel.add(fileTxt);
        panel.add(browse);

        return panel;
    }

    public static FileSelector getInstance()
    {
        if(instance==null)
        {
            instance = new FileSelector();
        }

        return instance;
    }
}

删除选择器

/*This is the second windows in wizard
This class is designed to let the user to select the delemeter to break information */

package wizardGUI;

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class DelemeterSelector extends JPanel
{
    private JLabel description;
    private JRadioButton tabBtn, semicolanBtn, commaBtn, spaceBtn;
    private JTextArea txtArea;
    private JScrollPane scroll;
    private ButtonGroup btnGroup;

    private GridBagLayout gbl;
    private GridBagConstraints gbc;

    private static DelemeterSelector instance = null;

    private DelemeterSelector()
    {
        //Initializing instance variables
        description = new JLabel("What delemeter separates your fields? Select the appropreiate delemeter");

        tabBtn = new JRadioButton("Tab");
        semicolanBtn = new JRadioButton("Semicolan");
        commaBtn = new JRadioButton("Comma");
        spaceBtn = new JRadioButton("Space");

        btnGroup = new ButtonGroup();
        btnGroup.add(tabBtn);
        btnGroup.add(semicolanBtn);
        btnGroup.add(commaBtn);
        btnGroup.add(spaceBtn);

        txtArea = new JTextArea(20,70);

        scroll = new JScrollPane(txtArea);

        gbl = new GridBagLayout();
        gbc = new GridBagConstraints();

        this.setLayout(gbl);

        //Creating the GUI
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(20,0,0,0);
        this.add(description,gbc);

        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(20,0,0,0);
        this.add(radioPanel(),gbc);

        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.insets = new Insets(10,0,0,0);
        gbc.fill = GridBagConstraints.BOTH;
        this.add(scroll,gbc);
    }

    private JPanel radioPanel()
    {
        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout());

        panel.add(tabBtn);
        panel.add(semicolanBtn);
        panel.add(commaBtn);
        panel.add(spaceBtn);

        panel.setBorder(BorderFactory.createTitledBorder("Choose the Delimeter that seperates your fields"));

        return panel;
    }

    public static DelemeterSelector getInstance()
    {
        if(instance == null)
        {
            instance = new DelemeterSelector();
        }

        return instance;
    }
}

当我运行代码时,“FileSelector”看起来很丑。我希望所有内容都出现在窗格的顶部,但相反,所有内容都出现在中间!我什至尝试了GridBagLayout 选项来使其可调整大小,但它也失败了。它的外观在附图中

如何使它看起来美观且可扩展?请帮忙

【问题讨论】:

  • 无论如何你只需要查看“FileSelector”
  • 那就只贴相关代码而不是添加小评论说:“嘿,我转储的代码90%是不需要的。请在您作为志愿者尝试解决我的问题时过滤它” . A little reading on posting a question
  • 它变成这样是因为“DelemeterSelector”比“FileSelector”使用了更多的空间。所以这两个类都是需要的。这两个类都只包含 GUI 元素。删除一个元素会搞砸一切
  • 不,这两个类都不需要。您可以通过手动调整 JFrame 的大小来获得此行为(如果调整大小的行为不正确)。否则,带有一个包含非常大组件的虚拟面板的 CardLayout 就足够了

标签: java swing user-interface layout cardlayout


【解决方案1】:

在这种情况下,fill 约束不是必需的。另外,gridxgridy 从 0 开始。此示例代码

    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.insets = new Insets(10,0,0,0);
    this.add(description,gbc);

    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.weighty = 1;
    gbc.insets = new Insets(0,10,0,0);
    this.add(locationPanel(),gbc);

产生这个

不确定这是否是您想要的。

【讨论】:

  • hmm..这似乎是我正在寻找的......无论如何我会稍后尝试,因为现在已经接近凌晨 1 点了 :) 非常感谢您的回复
  • 但是元素的大小呢?如您所见,它们非常小,甚至与窗口的宽度不匹配。调整大小时,它变得最糟糕!如何解决这个问题?
【解决方案2】:

只是一个显示相同行为和修复的快速示例,无需所有外部代码(SSCCE

import javax.swing.*;
import java.awt.*;

public class CardLayoutDemo {

  public static void main( String[] args ) {
    EventQueue.invokeLater( new Runnable() {
      @Override
      public void run() {
        JFrame frame = new JFrame( "TestFrame" );

        CardLayout cardLayout = new CardLayout();
        JPanel contentPane = new JPanel( cardLayout );

        JPanel firstPanel = new JPanel(  );
        firstPanel.setLayout( new BorderLayout(  ) );
        firstPanel.add( new JLabel( "Contents" ) );

        //wrap the smallest panel instead of directly adding it
        contentPane.add( firstPanel, "first" );
//        JPanel wrappedPanel = new JPanel( new BorderLayout(  ) );
//        wrappedPanel.add( firstPanel, BorderLayout.NORTH );
//        contentPane.add( wrappedPanel, "first" );

        JPanel secondPanel = new JPanel( new BorderLayout(  ) );
        secondPanel.add( new JComponent() {
          @Override
          public Dimension getPreferredSize() {
            return new Dimension( 500, 500 );
          }
        }, BorderLayout.CENTER );
        contentPane.add( secondPanel, "second" );

        cardLayout.show( contentPane, "first" );

        frame.setContentPane( contentPane );
        frame.pack();
        frame.setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE );
        frame.setVisible( true );
      }
    } );
  }
}

当您运行代码而不做任何更改时,它会显示一个面板,其中内容居中(firstPanel)。无需对firstPanel 进行任何更改,只需将其包装起来即可将其保留在顶部。只需将contentPane.add( firstPanel, "first" ) 替换为下面的 3 行(在注释中)。

很多时候nesting layouts 可以更轻松地获得所需的结果,而这就是其中之一。

【讨论】:

  • 好的,谢谢!非常感谢您的帮助:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-12
  • 1970-01-01
相关资源
最近更新 更多