2

サンプル GUIこんにちは、私は問題があります。誰かがそれを助けることができれば、それは素晴らしいことです。私はボーダーとグリッドレイアウトを使用しており、GUI を分割しようとしていますが、ボタンを全体のごく一部にしたいので、それは起こっていません。私もボタンを配置しようとしていますが、それが良い習慣であるかどうかはわかりません.2つのクラスがあります。1つはフレームのメインメソッドであるRunFurnitureで、もう1つはGUIのPanelFurnitureです。プログラムがコンパイルされ、実行されています。私は良い説明をしたことを願っています。これがコードです。

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class PanelFurniture extends JPanel implements ActionListener
{

    JButton center, east;
    JButton[] commandButtons = {
                                 new JButton(" Add Chair"),
                                 new JButton(" Add Table"),
                                 new JButton(" Add Desk "),
                                 new JButton(" Clear All   "),
                                 new JButton("Total Price"),
                                 new JButton("   Save       "),
                                 new JButton("   Load       "),
                                 new JButton("Summary ")
                                                        };

    JPanel centerPanel, westPanel, eastPanel;

    PanelFurniture()
    {

        this.setLayout(new BorderLayout());


        westPanel = new JPanel();
        westPanel.setLayout(new FlowLayout());

        for(int i=0; i<commandButtons.length; i++)      
        {
            westPanel.add(commandButtons[i]);
            commandButtons[i].addActionListener(this);

        }
//      westPanel.setSize(westDimension);   
        this.add(westPanel, BorderLayout.WEST);

        // start the middle panel       
        centerPanel = new JPanel(new GridLayout(1,2));

        center = new JButton("center");
        centerPanel.add(center);
        east = new JButton("east");
        centerPanel.add(east);  


        this.add(centerPanel, BorderLayout.CENTER);


    }

    public void actionPerformed(ActionEvent ae)
    {




    }
}

RunRurniture

import java.awt.*;
import javax.swing.*;
public class RunRurniture 
{

    /**
     * @param args
     */
    public static void main(String[] args) 
    {


        JFrame application = new JFrame();
        PanelFurniture panel = new PanelFurniture();
        application.add(panel);
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        application.setSize(300,150);
        application.setLocationByPlatform(true);
        application.setVisible(true);

    }

}
4

3 に答える 3

4

いくつかの追加の提案:

  • レイアウトにスペースを使用しないでください。配置を使用します。

  • コンポーネントの推奨サイズを使用して、レイアウトに作業を任せます。

  • 可能な場合は構成を使用してくださいfor-each

  • EDTで開始します。

家具テスト

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

/** @see http://stackoverflow.com/q/9793194/230513 */
public class FurnitureTest {

    private static final class FurniturePanel
        extends JPanel implements ActionListener {

        private static final int N = 3;
        private static final Icon icon =
            UIManager.getIcon("OptionPane.informationIcon");
        private JPanel westPanel = new JPanel();
        private JPanel centerPanel = new JPanel();
        private JButton[] commandButtons = {
            new JButton("Add Chair"),
            new JButton("Add Table"),
            new JButton("Add Desk"),
            new JButton("Clear All"),
            new JButton("Total Price"),
            new JButton("Save"),
            new JButton("Load"),
            new JButton("Summary")
        };

        FurniturePanel() {
            this.setLayout(new GridLayout());
            westPanel.setLayout(new BoxLayout(westPanel, BoxLayout.Y_AXIS));

            for (JButton b : commandButtons) {
                b.setAlignmentX(JButton.CENTER_ALIGNMENT);
                westPanel.add(b);
                b.addActionListener(this);
            }
            this.add(westPanel, BorderLayout.WEST);

            centerPanel.setLayout(new GridLayout(N, N, N, N));
            for (int i = 0; i < N * N; i++) {
                centerPanel.add(new JLabel(icon));
            }
            this.add(centerPanel, BorderLayout.CENTER);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println(e);
        }
    }

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame application = new JFrame();
                FurniturePanel panel = new FurniturePanel();
                application.add(panel);
                application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                application.pack();
                application.setLocationByPlatform(true);
                application.setVisible(true);
            }
        });

    }
}
于 2012-03-20T19:59:29.197 に答える
4

最新の編集:

JPanel上のLINE_STARTまたはWESTは と一緒に使用する必要がGridLayoutあり、CENTERJPanel はこれらの画像に対応するために使用できますGridLayout:-) . あなたがこの回答を受け入れたとき、そして@AndrewThompsonが物に関するコメントを追加したとき、私はそれに取り組んでいましたがGridLayout、JPanelをGridLayoutに配置すると、同じサイズのJButtonが許可されるようです。GridLayoutコード内での私の解釈は次のとおりです。

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

import java.awt.*;
import javax.swing.*;
public class RunFurniture 
{

    /**
     * @param args
     */
    public static void main(String[] args) 
    {


        JFrame application = new JFrame();
        PanelFurniture panel = new PanelFurniture();
        application.add(panel);
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        application.pack();
        application.setLocationByPlatform(true);
        application.setVisible(true);

    }

}

class PanelFurniture extends JPanel implements ActionListener
{

    JButton center, east;
    JButton[] commandButtons = {
                                 new JButton(" Add Chair"),
                                 new JButton(" Add Table"),
                                 new JButton(" Add Desk "),
                                 new JButton(" Clear All   "),
                                 new JButton("Total Price"),
                                 new JButton("   Save       "),
                                 new JButton("   Load       "),
                                 new JButton("Summary ")
                                                        };

    JPanel centerPanel, westPanel, eastPanel;

    PanelFurniture()
    {

        this.setLayout(new BorderLayout());


        westPanel = new JPanel();
        westPanel.setLayout(new GridLayout(0, 1));
        //westPanel.setLayout(new BoxLayout(westPanel, BoxLayout.PAGE_AXIS));
        westPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        for(int i=0; i<commandButtons.length; i++)      
        {
            westPanel.add(commandButtons[i]);
            commandButtons[i].addActionListener(this);

        }
//      westPanel.setSize(westDimension);   
        this.add(westPanel, BorderLayout.LINE_START);

        // start the middle panel       
        centerPanel = new JPanel(new GridLayout(1,2));

        center = new JButton("center");
        centerPanel.add(center);
        east = new JButton("east");
        centerPanel.add(east);  


        this.add(centerPanel, BorderLayout.CENTER);


    }

    public void actionPerformed(ActionEvent ae)
    {




    }
}

出力:

家具グリッドレイアウト

BoxLayoutコード内での私の解釈は次のとおりです。

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

import java.awt.*;
import javax.swing.*;
public class RunFurniture 
{

    /**
     * @param args
     */
    public static void main(String[] args) 
    {


        JFrame application = new JFrame();
        PanelFurniture panel = new PanelFurniture();
        application.add(panel);
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        application.pack();
        application.setLocationByPlatform(true);
        application.setVisible(true);

    }

}

class PanelFurniture extends JPanel implements ActionListener
{

    JButton center, east;
    JButton[] commandButtons = {
                                 new JButton(" Add Chair"),
                                 new JButton(" Add Table"),
                                 new JButton(" Add Desk "),
                                 new JButton(" Clear All   "),
                                 new JButton("Total Price"),
                                 new JButton("   Save       "),
                                 new JButton("   Load       "),
                                 new JButton("Summary ")
                                                        };

    JPanel centerPanel, westPanel, eastPanel;

    PanelFurniture()
    {

        this.setLayout(new BorderLayout());


        westPanel = new JPanel();
        westPanel.setLayout(new GridLayout(0, 1));
        //westPanel.setLayout(new BoxLayout(westPanel, BoxLayout.PAGE_AXIS));
        westPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        for(int i=0; i<commandButtons.length; i++)      
        {
            westPanel.add(commandButtons[i]);
            commandButtons[i].addActionListener(this);

        }
//      westPanel.setSize(westDimension);   
        this.add(westPanel, BorderLayout.LINE_START);

        // start the middle panel       
        centerPanel = new JPanel(new GridLayout(1,2));

        center = new JButton("center");
        centerPanel.add(center);
        east = new JButton("east");
        centerPanel.add(east);  


        this.add(centerPanel, BorderLayout.CENTER);


    }

    public void actionPerformed(ActionEvent ae)
    {




    }
}

出力は次のとおりです。

家具

于 2012-03-20T19:42:02.630 に答える
4

これは、私がコメントで言及したことの例です (他にいくつかの調整を加えたものです)。

パネル家具イメージ

package test;

import java.awt.*;
import javax.swing.*;

public class PanelFurniture extends JPanel
{

    private static final long serialVersionUID = 4231608548183463223L;

    JButton center, east;
    // leading/trailing spaces in these labels will be ignored.
    JButton[] commandButtons = {
            new JButton("Add Chair"),
            new JButton("Add Table"),
            new JButton("Add Desk "),
            new JButton("Clear All   "),
            new JButton("Total Price"),
            new JButton("Summary "),
            new JButton("Save"),
            new JButton("Load")
    };

    JPanel centerPanel, eastPanel;

    PanelFurniture()
    {
        this.setLayout(new BorderLayout());

        JToolBar toolBar = new JToolBar(); 

        for(int i=0; i<commandButtons.length; i++)      
        {
            if (i==3 || i==6) {
                toolBar.addSeparator();
            }
            toolBar.add(commandButtons[i]);
        }
        this.add(toolBar, BorderLayout.NORTH);

        // start the middle panel       
        centerPanel = new JPanel(new GridLayout(1,2));

        center = new JButton("center");
        centerPanel.add(center);
        east = new JButton("east");
        centerPanel.add(east);  

        this.add(centerPanel, BorderLayout.CENTER);
    }

    public static void main(String[] args) 
    {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame application = new JFrame();
                PanelFurniture panel = new PanelFurniture();
                application.add(panel);
                application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                application.pack();
                application.setLocationByPlatform(true);
                application.setVisible(true);
            }
        });
    }
}

アップデート

この GUI はイメージに基づいています。

パネル家具 2

package test;

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;

public class PanelFurniture extends JPanel
{

    private static final long serialVersionUID = 4231608548183463223L;

    JButton center, east;
    // leading/trailing spaces in these labels will be ignored.
    JButton[] commandButtons = {
            new JButton("Add Chair"),
            new JButton("Add Table"),
            new JButton("Add Desk "),
            new JButton("Clear All   "),
            new JButton("Total Price"),
            new JButton("Summary "),
            new JButton("Save"),
            new JButton("Load")
    };

    JPanel centerPanel, westPanel, westPanelConstrain, eastPanel;

    PanelFurniture()
    {
        super(new BorderLayout(2,2));
        this.setBorder(new EmptyBorder(4,4,4,4));

        westPanel = new JPanel(new GridLayout(0,1,4,4));

        for(int i=0; i<commandButtons.length; i++)      
        {
            westPanel.add(commandButtons[i]);
        }
        westPanelConstrain = new JPanel(new BorderLayout());
        westPanelConstrain.add(westPanel, BorderLayout.NORTH);
        this.add(westPanelConstrain, BorderLayout.WEST);

        // start the middle panel       
        centerPanel = new JPanel(new GridLayout(1,2));

        center = new JButton("center");
        centerPanel.add(center);
        east = new JButton("east");
        centerPanel.add(east);

        this.add(centerPanel, BorderLayout.CENTER);
    }

    public static void main(String[] args) 
    {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame application = new JFrame();
                PanelFurniture panel = new PanelFurniture();
                application.add(panel);
                application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                application.pack();
                application.setLocationByPlatform(true);
                application.setVisible(true);
            }
        });
    }
}
于 2012-03-20T19:43:49.327 に答える