1

私はJavaSwingに非常に慣れており、Javaも同様です。私はnetbeansのウィンドウビルダーを使用してGUIを設計します。orderListRowPanelというJPanelを含むorderListPanelというJPanelがあります。ここで、JPanelのリストを取得しました。これらのJPanelをorderListPanelに挿入します。

http://postimage.org/image/ex00whf69/
orderListPanelは中央にあり、orderListRowPanelはorderListPanelと同じ場所です。

http://postimage.org/image/dbrtn33sj/
今、たくさんのJPanelをorderListPanelに挿入して、リストのように見せたいと思います。赤い四角はJPanel側のコンポーネントです。

BorderLayoutを使用しようとしましたが、foreachループorderListPanel.add(List pList)を使用すると、orderListPanel内に結果が表示されません。誰かがそれを解決する方法を知っていますか?

4

1 に答える 1

2

Netbeans自動GUIビルダーは使用しないでください。ドラッグアンドドロップのGUI構築手法は、Javaコミュニティでは受け入れられません。コードを提供していないため、コードを編集できません。あなたの画像は利用できません。

しかし、これはあなたがそれを行う方法です。これは純粋なハンドコードです

import java.awt.*;
import java.util.ArrayList;
import javax.swing.*;
import java.util.List;

public class GUIBuilder extends JFrame
{
    private JPanel orderList;
    private JPanel orderListRow;
    private JPanel additionalPanel;

    private List panels = new ArrayList(); //Your List

    private JLabel label1, label2, label3;

    public GUIBuilder()
    {
        label1 = new JLabel("Label 1"); //Create the JLabels
        label2 = new JLabel("Label 2");//Create the JLabels
        label3 = new JLabel("Label 3");//Create the JLabels


        orderList = new JPanel(); //Creating the orderList JPanel
       orderList.setLayout(new BoxLayout(orderList, BoxLayout.Y_AXIS)); //Setting Box layout, and set the direction to Y axis.


        orderListRow = new JPanel(); //Creating the orderListRow JPanel        
        orderListRow.add(label1);

        additionalPanel = new JPanel(); //Creating the additionalPanel JPanel      
        additionalPanel.add(label2);

        orderList.add(orderListRow); //Adding orderListRow into orderList
        orderList.add(additionalPanel); //Adding additionalPanel into orderList

        this.setLayout(new GridLayout(1,1));
        this.add(orderList); //Setting orderList into JFrame

        this.pack(); //Setting JFrame size. This will only take required space
        this.setVisible(true); //Making JFrame Visible
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //When you hit the 'X' button, the program will exit
    }

    public static void main(String[]args)
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); //Setting the UI into your native platform UI
            new GUIBuilder(); //Calling your program
        }
        catch(Exception e)
        {
            e.printStackTrace(); //If any error occured in setting up UI, print the stack trace
        }
    }
}

パネルをリスト内に配置した場合は、これを使用します

import java.awt.*;
import java.util.ArrayList;
import javax.swing.*;
import java.util.List;

public class GUIBuilder extends JFrame
{
    private JPanel orderList;
    private JPanel orderListRow;
    private JPanel additionalPanel;

    private List<JPanel> panels = new ArrayList<JPanel>(); //Your List

    private JLabel label1, label2, label3;

    public GUIBuilder()
    {
        label1 = new JLabel("Label 1"); //Create the JLabels
        label2 = new JLabel("Label 2");//Create the JLabels
        label3 = new JLabel("Label 3");//Create the JLabels


        orderList = new JPanel(); //Creating the orderList JPanel
        orderList.setLayout(new BoxLayout(orderList, BoxLayout.Y_AXIS)); //Setting Box layout, and set the direction to Y axis.


        orderListRow = new JPanel(); //Creating the orderListRow JPanel        
        orderListRow.add(label1);
        panels.add(orderListRow); // Add the panel to the List

        additionalPanel = new JPanel(); //Creating the additionalPanel JPanel      
        additionalPanel.add(label2);
        panels.add(additionalPanel); // Add the panel to the List


        for(int i=0;i<panels.size();i++)
        {
            orderList.add(panels.get(i));
        }



        this.setLayout(new GridLayout(1,1));
        this.add(orderList); //Setting orderList into JFrame

        this.pack(); //Setting JFrame size. This will only take required space
        this.setVisible(true); //Making JFrame Visible
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //When you hit the 'X' button, the program will exit
    }

    public static void main(String[]args)
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); //Setting the UI into your native platform UI
            new GUIBuilder(); //Calling your program
        }
        catch(Exception e)
        {
            e.printStackTrace(); //If any error occured in setting up UI, print the stack trace
        }
    }
}
于 2013-02-28T05:18:30.573 に答える