简体   繁体   English

需要帮助以正确放置旋转组件

[英]Needing help to place swing components correctly

In order to pass a college course, i need to code a software with JAVA and the swing API. 为了通过大学课程,我需要使用JAVA和swing API编写软件。 However I have some issues to place my components like I want : 但是我在放置组件时遇到一些问题:

I want to place some components in the moodPanel: Like this 我要放置在moodPanel一些组件: 像这

Which layout should I use for the moodPanel and which is the best Way to get the components like in the second draw ? 我应该为moodPanel使用哪种布局,以及哪种组件是第二次绘制中获得组件的最佳方式?

Edit : I'm currently trying to apply the solution below, but I have some problems : Here 编辑:我当前正在尝试应用下面的解决方案,但我有一些问题: 这里

public MoodPanel(){
    super();

    this.setBackground(new Color(255, 255,0));

    this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));

    this.choicePanel = new JPanel();
    this.choicePanel.setLayout(new GridBagLayout());
    this.choicePanel.setBackground(new Color(255, 0, 0));
    GridBagConstraints c = new GridBagConstraints();
    this.statPanel = new JPanel();
    this.statPanel.setLayout(new BorderLayout());



    this.lIdTweet= new JLabel();
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridwidth=3;
    c.gridx=0;
    c.gridy=0;
    c.weightx=0.0;
    c.anchor =GridBagConstraints.FIRST_LINE_START;
    this.choicePanel.add(this.lIdTweet,c);

    this.moodGroup = new ButtonGroup();

    this.JRBad = new JRadioButton("Mauvais");
    this.JRBad.setActionCommand("0");
    this.JRBad.setVisible(false);
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx=0;
    c.gridy=1;
    c.weightx=0.0;
    this.choicePanel.add(this.JRBad,c);



    this.JRNeutral = new JRadioButton("Neutre");
    this.JRNeutral.setActionCommand("2");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx=1.0;
    c.insets = new Insets(0,70, 0, 0);
    this.choicePanel.add(this.JRNeutral,c);
    this.JRNeutral.setVisible(false);




    this.JRGood = new JRadioButton("Bon");
    this.JRGood.setVisible(false);
    this.JRGood.setActionCommand("4");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx=0.0;
    c.insets = new Insets(0,0, 0, 0);
    this.choicePanel.add(this.JRGood,c);





    this.moodGroup.add(this.JRBad);
    this.moodGroup.add(this.JRNeutral);
    this.moodGroup.add(this.JRGood);



    this.buttonPanel = new JPanel();
    this.buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
    this.intializingListener();
    this.btnAddToList = new JButton("Ajouter");
    this.btnAddToList.addActionListener(OKAction);
    this.MPClose= new JButton("Fermer");
    this.MPClose.addActionListener(CancelAction);
    this.buttonPanel.add(btnAddToList);
    this.buttonPanel.add(MPClose);
    this.buttonPanel.setVisible(false);
    this.buttonPanel.setSize(new Dimension(this.buttonPanel.getPreferredSize().width,this.btnAddToList.getPreferredSize().height));
    this.buttonPanel.setBackground(new Color(60, 90, 60));





    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx=0;
    c.gridy=1;
    c.weightx=0.0;
    c.gridwidth=3;
    this.choicePanel.add(buttonPanel,c);
    this.add(choicePanel,BorderLayout.NORTH);
    this.add(statPanel,BorderLayout.SOUTH);

When it comes to designing layouts, you need to break down each section in needs and responsibilities, while you could actually do the whole layout using GridBagLayout 's, it's probably way over kill. 在设计布局时,您需要按需求和职责细分每个部分,而实际上可以使用GridBagLayout完成整个布局,这可能是GridBagLayout的。

Instead, you can use a series of compounding layouts, each adding to what the previous layouts where doing, for example... 取而代之的是,您可以使用一系列混合布局,每个布局都会添加到以前的布局中,例如...

If we look at the main layout, I see two main areas, more or less... 如果我们看一下主要布局,我会看到两个主要区域,或多或少...

在此处输入图片说明

this just screams BorderLayout (again GridBagLayout could work) 这只是尖叫BorderLayout (再次GridBagLayout可以工作)

Now, we start focusing into the NORTH section 现在,我们开始关注NORTH

在此处输入图片说明

The question that needs to be answered here is, are all those three sections equal in height? 这里需要回答的问题是,这三个部分的高度都相等吗? If they are, then GridLayout could work, if not, then I'd probably be leaning towards GridBagLayout 如果是这样,那么GridLayout可以工作,否则,我可能会倾向于GridBagLayout

The last two sections are almost the same... 最后两个部分几乎相同...

在此处输入图片说明 在此处输入图片说明

You need to answer the question, are all the elements going to be given the same about of horizontal space or not? 您需要回答这个问题,所有元素的水平空间是否都相同? If they size equally, then you can use a GridLayout , if not, you could use either a FlowLayout or GridBagLayout depending on your needs and desires. 如果它们的大小相等,则可以使用GridLayout ,否则,可以根据需要和需要使用FlowLayoutGridBagLayout

Try never to look at the "whole" UI and try and solve it in one single step, most times, you want to break it down into areas of functionality, which will also allow you to separate the areas of responsibility in your code and generate self contained units of work from which you can base you classes on. 绝不要试图看待整个“ UI”,并且一步一步地尝试解决它,多数情况下,您希望将其分解为功能区域,这也将使您能够在代码中分离责任区域并生成自包含的工作单元,您可以以此为基础进行学习。

See Laying Out Components Within a Container for more details. 有关更多详细信息,请参见在容器中布置组件

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

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