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

Java 如何在一个框架中并排添加JPanel/JSplitPane组件?,java,swing,jpanel,layout-manager,jsplitpane,Java,Swing,Jpanel,Layout Manager,Jsplitpane,我是Java GUI新手,需要一些帮助。我正在开发一个应用程序,它从HDD加载一个图像,并在下面输出(所需输出)中显示的第一个窗口中显示它。我不太确定这个场景中要使用的布局。尝试了GridBagLayout,但似乎没有得到确切的输出。我需要一个2x3布局,其中第一行有3列,由3个标签与名称图像和第二行的第一个单元格组成的图像。我的问题是: 以下哪种布局适合此场景?我如何创建它 我用哪一个?JPanel还是JSplitPane 如何使图像仅显示在第二列的第一个单元格中 我在“选项1”菜单中包含了

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

  • 以下哪种布局适合此场景?我如何创建它
  • 我用哪一个?JPanel还是JSplitPane
  • 如何使图像仅显示在第二列的第一个单元格中

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

  • 我使用File类处理图像加载和保存选项。可以吗?我读到AWT包中有一个“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();
     }
}
这是输出


这都是关于布局的实验。

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


这都是关于布局的实验。

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


这都是关于布局的实验。

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



这都是关于布局的实验。

看看GridLayout和BorderLayout。这些布局可以满足您的需要。GridLayout使用固定大小的组件。将尝试BorderLayout…从您的图片判断,您可以为顶部部件和底部部件创建网格布局。每个面板有3列。然后为frame.gRao创建一个BorderLayout,如@jtothebee,您需要同时使用两种或更多布局。这里的关键是您需要嵌套JPanel,每个都使用自己的布局管理器。其他可能考虑的布局包括MIGPLAY,第三方布局管理器。JtoHeBee——考虑从你的好评论中创建一个答案。看看GridLayout和BordDayStudio。这些布局可以满足您的需要。GridLayout使用固定大小的组件。将尝试BorderLayout…从您的图片判断,您可以为顶部部件和底部部件创建网格布局。每个面板有3列。然后为frame.gRao创建一个BorderLayout,如@jtothebee,您需要同时使用两种或更多布局。这里的关键是您需要嵌套JPanel,每个都使用自己的布局管理器。其他可能考虑的布局包括MIGPLAY,第三方布局管理器。JtoHeBee——考虑从你的好评论中创建一个答案。看看GridLayout和BordDayStudio。这些布局可以满足您的需要。GridLayout使用固定大小的组件。将尝试BorderLayout…从您的图片判断,您可以为顶部部件和底部部件创建网格布局。每个面板有3列。然后为frame.gRao创建一个BorderLayout,如@jtothebee,您需要同时使用两种或更多布局。这里的关键是您需要嵌套JPanel,每个都使用自己的布局管理器。其他可能考虑的布局包括MIGPLAY,第三方布局管理器。JtoHeBee——考虑从你的好评论中创建一个答案。看看GridLayout和BordDayStudio。这些布局可以满足您的需要。GridLayout使用固定大小的组件。将尝试BorderLayout…从您的图片判断,您可以为顶部部件和底部部件创建网格布局。每个面板有3列。然后为frame.gRao创建一个BorderLayout,如@jtothebee,您需要同时使用两种或更多布局。这里的关键是您需要嵌套JPanel,每个都使用自己的布局管理器。其他可能考虑的布局包括MIGPLAY,第三方布局管理器。JtoHeBee——考虑从你的好评论中创建答案。谢谢你的指导。。。你能教我如何处理图像吗?这里有很多关于图像的话题,不管是图像图标还是缓冲区图像。随便看看。@jtothebee如何保存显示在JLabel
public ImageIcon jImage(字符串路径){ImageIcon myImage=new ImageIcon(路径);Image img=myImage.getImage();Image newimg=img.GetScaleInstance(l4.getWidth(),l4.getHeight(),Image.SCALE\u SMOOTH);ImageIcon=new ImageIcon(newimg);return image;}
else if(e.getSource()==Open){choose=new JFileChooser();choose.setDialogTitle(“选择要打开的图像文件”);choose.setCurrentDirectory(新文件(System.getProperty(“user.home”);choose.setFileSelectionMode(仅JFileChooser.FILES_)
FileNameExtensionFilter=newfilenameextensionfilter(“所有图像文件”、“jpg”、“jpeg”、“gif”、“png”);choose.addchoosablefilter(过滤器);int result=choose.showOpenDialog(null);if(result==JFileChooser.APPROVE_选项){fileToOpen=choose.getSelectedFile();String path=fileToOpen.getAbsolutePath();l4.setText(“”;l4.setIcon(jImage(path));}
D