【问题标题】:How to create a user interface using Swing in Java? [closed]如何在 Java 中使用 Swing 创建用户界面? [关闭]
【发布时间】:2013-04-29 08:44:02
【问题描述】:

我想用 Java 创建一个用户界面(下图)。我用的是网格布局,但是怎么平分呢?

【问题讨论】:

  • 请提供一些代码。我们不是代码编写服务。
  • 尝试使用 GridBagLayout.. 阅读有关 gridwidth 和 gridheight 的信息。基本上我的意思是你可以将整个 UI 划分为 9 行和 12 列.. 只是为了让你理解,制作第一个网格,即 x = 0, y = 0(我看到时间 - 08:58: 10) 网格宽度为 4.. 同样
  • @Mady:给我参考链接
  • 给我几分钟..我正在编写一个参考代码来帮助您更好地理解..同时您为什么不尝试阅读有关 GridBagConstraints、gridwith 和 gridheight 的内容:docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html跨度>
  • 这是一个奇怪而古怪的 GUI。我认为它可以更好地布局,你不应该试图模仿它。注意:不是投票者之一,但他们可能会因为您的努力很少,再加上“请给出一些代码”而投票。我们可能会提供代码,也可能不会,但大多数反对被要求提供代码。

标签: java swing user-interface layout-manager


【解决方案1】:

这是你的代码

public class SwingSolution extends JFrame
{
    private JPanel componentPanel = null;   
    private JButton buttonWithWidth2 = null;    
    private JButton button2 = null; 
    private JButton buttonWithHeight2 = null;   
    private JButton button4 = null; 
    private JButton button5 = null;

    public JPanel getComponentPanel()
    {
       if(null == componentPanel)
       {
           componentPanel = new JPanel();
           GridBagLayout gridBagLayout = new GridBagLayout();
           componentPanel.setLayout(gridBagLayout);

           GridBagConstraints constraint = new GridBagConstraints();

           constraint.gridx = 0;
           constraint.gridy = 0;
           // Set gridwidth to 2 grids
           constraint.gridwidth = 2;
           buttonWithWidth2 = new JButton("Button Width 2");
           componentPanel.add(buttonWithWidth2, constraint);

           constraint.gridx = 2;
           constraint.gridy = 0;
           // set the gridwidth back to normal i.e. 1 grid
           constraint.gridwidth = 1;
           button2 = new JButton("Button 2");
           componentPanel.add(button2, constraint);

           constraint.gridx = 0;
           constraint.gridy = 1;
           // set the gridheight to 2
           constraint.gridheight = 2;
           buttonWithHeight2 = new JButton("Button Height 2");
           componentPanel.add(buttonWithHeight2, constraint);

           constraint.gridx = 1;
           constraint.gridy = 1;
           // set the gridheight back to normal i.e. 1 grid
           constraint.gridheight = 1;
           button4 = new JButton("Button 4");
           componentPanel.add(button4, constraint);

           constraint.gridx = 1;
           constraint.gridy = 2;
           button5 = new JButton("Button 5");
           componentPanel.add(button5, constraint);
       }

       return componentPanel;
    }

    public static void main(String[] args)
    {
        JFrame frame = new JFrame();
        SwingSolution main = new SwingSolution();

        frame.setTitle("Simple example");
        frame.setSize(300, 200);
        frame.setLocationRelativeTo(null);

        frame.setContentPane(main.getComponentPanel());

        frame.setVisible(true);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
}

它应该给你输出:

【讨论】:

  • 提前致谢。现在我了解了 GridBagLayout。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-15
  • 2010-10-24
  • 1970-01-01
  • 2010-11-14
相关资源
最近更新 更多