简体   繁体   English

如何使用Java swing创建捕捉的组件?

[英]How to create snapped components with java swing?

I have worked hard on writing my GUI in swing however I am trying to improve it further since I feel it still looks a little off. 我一直在努力地编写自己的GUI,但是由于我觉得它看起来还有些不足,因此我正在尝试进一步改进它。

I would ideally like: 我理想地希望:

  • the button to snap to the top right, 扣住右上角的按钮
  • the textfield to be the same height as the button and stretch from the top left to the button edge 文本字段与按钮的高度相同,并从左上方延伸到按钮边缘
  • the scrollpane to stretch from the bottom of the textfield and the button to the edges of the window even when stretched. 滚动窗格以从文本字段和按钮的底部拉伸到窗口的边缘,即使在拉伸时也是如此。

I'm unsure how to "snap" the components to the top right, top left and rest of the area respectively. 我不确定如何分别“捕捉”组件到该区域的右上方,左上方和其余部分。

    @SuppressWarnings("serial")
    class TFrame extends JFrame
    {
      TFrame()
      {
        super("Huffman Compression");//setTitle
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 300);
        setResizable(true);

        jPanel = new JPanel();

        jTextField = new JTextField("Enter string to compress...");

        jButton = new JButton("Compress");
        jButton.setFocusable(false);

        jTextArea = new JTextArea("LOG AREA", 30, 30);
        jTextArea.setWrapStyleWord(true);
        jTextArea.setLineWrap(true);
        jTextArea.setEditable(false);
        jTextArea.setFocusable(false);
        jTextArea.setOpaque(false);

        jScrollPane = new JScrollPane(jTextArea);
        jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

        jPanel.add(jTextField, BorderLayout.WEST);
        jPanel.add(jButton, BorderLayout.EAST);
        jPanel.add(jScrollPane, BorderLayout.SOUTH);

        add(jPanel);

        try
        {
          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException
               | InstantiationException
               | IllegalAccessException
               | UnsupportedLookAndFeelException e)
        {
          e.printStackTrace();
        }

        setVisible(true);
      }

      private JPanel jPanel;
      private JTextField jTextField;
      private JButton jButton;
      private JTextArea jTextArea;
      private JScrollPane jScrollPane;

    }

    public static void main(String[] args)
        {

          TFrame frame = new TFrame();

        frame.pack();
        ...

This is what it currently looks like: http://i.imgur.com/90cmDl1.png 这是当前的样子: http : //i.imgur.com/90cmDl1.png

Regards. 问候。

Basically, you need to take advantage of a series of layout managers (commonly known as "compound layouts"). 基本上,您需要利用一系列的布局管理器(通常称为“复合布局”)。

For example, you can accomplish the requirements for the button and field using a GridBagLayout and the by using a BorderLayout , you can accomplish the rest, for example... 例如,您可以使用GridBagLayout满足按钮和字段的要求,而使用BorderLayout可以满足其余要求,例如...

日志窗口

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class BrowserWindow {

    public static void main(String[] args) {
        new BrowserWindow();
    }

    public BrowserWindow() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JPanel topPane = new JPanel(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridx = 0;
                gbc.gridy = 0;
                gbc.weightx = 1;
                gbc.fill = GridBagConstraints.BOTH;
                topPane.add(new JTextField(10), gbc);
                gbc.weightx = 0;
                gbc.fill = GridBagConstraints.NONE;
                gbc.gridx++;
                topPane.add(new JButton("Compress"), gbc);

                JTextArea ta = new JTextArea("Log...", 30, 30);
                JScrollPane sp = new JScrollPane(ta);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(topPane, BorderLayout.NORTH);
                frame.add(sp);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

Take a look at Laying Out Components Within a Container for more details 看一下在容器内布置组件的更多详细信息

Whether you're learning Swing or just want a quick GUI, I highly recommend using the NetBeans IDE for its WYSIWYG Swing editor. 无论您是在学习Swing还是只需要快速的GUI,我都强烈建议使用NetBeans IDE的WYSIWYG Swing编辑器。 You could even re-create the same GUI you've already built (in seconds) just to compare your own code to the auto-generated version and learn from the differences. 您甚至可以重新创建已经构建的GUI(以秒为单位),以将自己的代码与自动生成的版本进行比较,并从差异中学习。

More to the topic: 关于主题的更多信息:

  • Controls snap into place as you drag them. 拖动控件时,控件会卡入到位。
  • Selecting multiple controls allows syncing of their dimensions. 选择多个控件可以同步其尺寸。
  • Snapping controls to the edges of a frame causes them to stretch by default. 将控件捕捉到框架边缘会导致默认情况下进行拉伸。

There is some learning curve, because everything is configurable; 有一些学习曲线,因为一切都是可配置的。 but if you plan to make more than one Swing application in your life, Netbeans is the (free) way to go. 但是,如果您打算一生中制作多个Swing应用程序,那么Netbeans是(免费)可行的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM