【问题标题】:How to create snapped components with java swing?如何使用 java swing 创建快照组件?
【发布时间】:2013-11-07 10:03:08
【问题描述】:

我一直在努力编写我的摇摆图形用户界面,但我正在尝试进一步改进它,因为我觉得它看起来仍然有点不对劲。

我希望:

  • 用于对齐右上角的按钮,
  • 文本字段与按钮高度相同,并从左上角延伸到按钮边缘
  • 滚动窗格从文本字段底部和按钮拉伸到窗口边缘,即使在拉伸时也是如此。

我不确定如何将组件分别“对齐”到该区域的右上角、左上角和其余部分。

    @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();
        ...

这是它目前的样子: http://i.imgur.com/90cmDl1.png

问候。

【问题讨论】:

    标签: java swing user-interface


    【解决方案1】:

    基本上,您需要利用一系列布局管理器(通常称为“复合布局”)。

    例如,您可以使用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);
                }
            });
        }
    
    }
    

    查看Laying Out Components Within a Container了解更多详情

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-30
      • 2018-04-24
      • 2020-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-18
      相关资源
      最近更新 更多