如何在框架中并排添加 JPanel/JSplitPane 个组件?

How do I add JPanel/JSplitPane components side by side in a Frame?

我是 Java GUI 的新手,需要一些帮助。我正在开发一个应用程序,它从 HDD 加载图像并将其显示在下面输出(所需输出)中显示的第一个 window 中。我不太确定在这种情况下要使用的布局。尝试了 GridBagLayout,但我似乎没有得到准确的输出。我需要一个 2x3 布局,其中第一行 3 列由 3 个名称为 IMAGE 的标签组成,第二行的第一个单元格由图像组成。我的问题是:

我在下面包含了我的代码...请帮助!

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

class WorkingImage extends JFrame implements ActionListener

{

    JMenuItem Open, Close, Save1, Save2, Save3, Transform1, Transform2;
    JFileChooser choose;
    JPanel panel;
    Label l1, l2, l3;


    /*public void myLayout()
    {
        panel.setLayout(new GridBagLayout());

        GridBagConstraints gc = new GridBagConstraints();
    }*/



    WorkingImage(String title)
    {
        super(title);

        //myLayout();

        JMenuBar mbar = new JMenuBar();
        setJMenuBar(mbar);

        //File Menu, Open, Save, Close Menu Items

        JMenu file = new JMenu("File");
        mbar.add(file);

        Open = new JMenuItem("Open");
        Open.setMnemonic('O');
        Open.addActionListener(this);

        Close = new JMenuItem("Close");
        Close.setMnemonic('E');
        Close.addActionListener(this);

        Save1 = new JMenuItem("Save");
        Save1.setMnemonic('A');
        Save1.addActionListener(this);

        file.add(Open);
        file.add(Save1);
        file.add(Close);

        //Creation of Option 1 and Option 2 Menus

        JMenu opt1 = new JMenu("Option1");
        mbar.add(opt1);
        Transform1 = new JMenuItem("Transform");
        Transform1.addActionListener(this);
        Save2 = new JMenuItem("Save");
        Save2.addActionListener(this);
        opt1.add(Transform1);
        opt1.add(Save2);

        JMenu opt2 = new JMenu("Option2");
        mbar.add(opt2);
        Transform2 = new JMenuItem("Transform");
        Transform2.addActionListener(this);
        Save3 = new JMenuItem("Save");
        Save3.addActionListener(this);
        opt2.add(Transform2);
        opt2.add(Save3);


        //Set Frame Size

        setSize(800, 600);
        setVisible(true);

        // Get the size of the screen

        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

        // Determine the new location of the window

        int w = getSize().width;
        int h = getSize().height;
        int x = (dim.width-w)/2;
        int y = (dim.height-h)/2;

        // Move the window

        setLocation(x, y);


        addWindowListener(new WindowAdapter()
        {
            @Override
            public void windowClosing(WindowEvent we)
            {
                System.exit(0);
            }
        });
    }


    @Override
    public void actionPerformed(java.awt.event.ActionEvent e)
    {
        if(e.getSource() == Close)
        {
            System.out.println("\nApplication Terminated...");
            System.exit(0);
        }

        else if(e.getSource() == Open)
        {
            choose = new JFileChooser();
            choose.setDialogTitle("Specify a file to Open");

            //Set File Extension filter
            choose.setFileSelectionMode(JFileChooser.FILES_ONLY);
            FileNameExtensionFilter filter = new FileNameExtensionFilter("jpeg, jpg, png files", "jpeg", "jpg", "png");
            choose.setFileFilter(filter);

            int userSelection = choose.showOpenDialog(this);

            if(userSelection == JFileChooser.APPROVE_OPTION)
            {
                File fileToOpen = choose.getSelectedFile();
            }
        }

        else if(e.getSource() == Save1)
        {
            choose = new JFileChooser();
            choose.setDialogTitle("Specify a file to save");

            //Set file extension filter

            choose.setFileSelectionMode(JFileChooser.FILES_ONLY);
            FileNameExtensionFilter filter = new FileNameExtensionFilter(".jpeg, .jpg and .png files", "jpeg", "jpg", "png");
            choose.setFileFilter(filter);

            int userSelection = choose.showSaveDialog(this);

            if (userSelection == JFileChooser.APPROVE_OPTION)
            {
                File fileToSave = choose.getSelectedFile();
                System.out.println("Save as file: " + fileToSave.getAbsolutePath());
            }

        }

        else
        {

        }
    }
}

class JavaImage
{
    public static void main(String args[])
    {
        new WorkingImage("Image Display");
    }
}

看看这个实现。它将使用 2 个布局管理器。我使用了不同的组件。

public class SOAnswer extends JFrame{

     private void initComponents(){
          JPanel topPanel = new JPanel(new GridLayout(1, 3, 10, 10));// 1 row, 3 columns

          topPanel.add(new JButton("IMAGE"));
          topPanel.add(new JButton("IMAGE"));
          topPanel.add(new JButton("IMAGE"));

          JPanel bottomPanel = new JPanel(new GridLayout(1, 3, 10, 10));
          bottomPanel.add(new JButton("CLICK ME"));
          bottomPanel.add(new JButton("CLICK ME"));
          bottomPanel.add(new JButton("CLICK ME"));

          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
          setLayout(new BorderLayout(10, 10));
          add(topPanel, BorderLayout.NORTH);
          add(bottomPanel, BorderLayout.CENTER);
          setVisible(true);

     }

     public static void main(String[]args){
          SOAnswer go = new SOAnswer();
          go.initComponents();
     }
}

这是输出

一切都是关于布局的实验。