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

时间:2015-10-24 13:18:06

标签: java swing jpanel layout-manager jsplitpane

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

  • 哪种布局适合此场景,如何创建?
  • 我使用哪一个? JPanel或JSplitPane?
  • 如何让图像单独显示在第二列的第一个单元格中

  • 我添加了一个名为' Transform'的菜单项。在'选项1'菜单。如何编写代码,以便在'转换'单击MenuItem,第一个单元格(2x1)中的图像被清除并显示在第二个单元格(2x2)中。

  • 我使用File类来处理Image加载和保存选项。好吗?我读过那里有一个' Image' AWT包中的类来处理图像数据,但我不太确定它的实现。

我在下方提供了我的代码......请帮忙!

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");
    }
}

OUTPUT DESIRED

1 个答案:

答案 0 :(得分:3)

看看这个实现。它将使用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();
     }
}

这是输出

enter image description here

这完全是关于布局的实验。