简体   繁体   English

应该使用哪种布局?

[英]Which Layout should be used?

I'm kind of new to the whole "how to arrange your components" thing in JAVA and I couldn't figure out how to realise the following JFrame (I can't post images so I just put the link) 我对JAVA中的整个“如何安排组件”一事有点陌生,我不知道如何实现以下JFrame(我无法发布图片,因此只放置了链接) http://oi58.tinypic.com/10qjkpg.jpg

I tried to be as precise as possible about what I already did. 对于已完成的工作,我尝试尽可能地精确。 I would like your advice about how to arrange the green part. 我想就如何安排绿色部分提出建议。 Thanks! 谢谢!

EDIT: as some people rightfully said, I didn't put the code of what I did. 编辑:正如一些人正确地说的那样,我没有写我所做的代码。 Here it is: 这里是:

public Frame(){
        this.setTitle("Small application");
        this.setSize(445, 500);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setVisible(true);
        this.setResizable(false);

        JPanel container = new JPanel();
        container.setLayout(new BorderLayout());

        //Title
        JLabel title = new JLabel("Welcome to this application");
        title.setHorizontalAlignment(JLabel.CENTER);
        title.setPreferredSize(new Dimension(200,50));
        title.setFont(new Font("Courrier",Font.BOLD,20));
        container.add(title, BorderLayout.NORTH);

        //Center part
        JPanel centerPart = new JPanel();
        JLabel cell1 = new JLabel("Enter all measurements:");
        cell1.setPreferredSize(new Dimension(150,20));
        JLabel cell2 = new JLabel("Please, select the files...");
        cell2.setPreferredSize(new Dimension(150,20));
        cell2.setBackground(Color.white);
        cell2.setBorder(BorderFactory.createLineBorder(Color.black));
        cell2.setOpaque(true);
        JButton cell3 = new JButton("Browse");
        cell3.setPreferredSize(new Dimension(100,20));
        centerPart.add(cell1);
        centerPart.add(cell2);
        centerPart.add(cell3);
        container.add(centerPart, BorderLayout.CENTER);

        /*
         * I need your help here :)
         * I can't figure out how to put the image and the text next to it
         */ 

        //Bottom part
        JPanel bottom = new JPanel();
        JButton graph = new JButton("Graph");
        JButton exit = new JButton("Exit");
        bottom.add(graph);
        bottom.add(exit);
        container.add(bottom, BorderLayout.SOUTH);

        this.setContentPane(container);
    }

For most practical cases, you use multiple, nested containers , with a LayoutManager suited to the layout within each container. 在大多数实际情况下,您可以使用多个嵌套容器 ,并使用一个适合每个容器布局的LayoutManager。

Each LayoutManager does one specific job, in practice you often want differnt regions of a UI layouted in different ways. 每个LayoutManager都执行一项特定的工作,实际上,您通常希望UI的不同区域以不同的方式进行布局。 So for each region use a separate Container (eg JPanel) and set a LayoutManager that suits your layout requirements. 因此,对于每个区域,请使用单独的容器(例如JPanel),并设置适合您的布局要求的LayoutManager。

The big hurdle for beginners seems to be to get the point that LayoutManagers can (and often must) be used with nested containers. 对于初学者来说,最大的障碍似乎是要知道LayoutManagers可以(并且经常必须)与嵌套容器一起使用。

Try using a JPanel 尝试使用JPanel

  • Create the JPanel 创建JPanel
  • Place your JPanel into your JFrame 将您的JPanel放入您的JFrame
  • Position the labels, button, textfield onto the newly created JPanel. 将标签,按钮,文本字段放置到新创建的JPanel上。

It should do the trick. 它应该可以解决问题。 and it is pretty basic. 这是非常基本的。 You should be able to do the code on your own! 您应该能够自己编写代码!

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

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