问题描述:

Good morning,I am currently trying to make a graphic user interface like this:

I would want to have 3 blank spaces for images and under each blank space, a button to choose and load an image. I've been reading tutorials and so on, with paintingComponent and so, but I don't know how to make these blank spaces and buttons and open a window to choose from you computer the images when we click on load.

I have written this:

package projet;

import java.awt.BorderLayout;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class FrameIM extends JFrame{

/**

*

*/

private static final long serialVersionUID = -7538888128782793269L;

private static final int width = 700;

private static final int height = 500;

public FrameIM(){

//window

this.setTitle("Mutual information");

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setLocationRelativeTo(null);

this.setSize(width, height);

//boutons

JButton boutonLoad1 = new JButton("Load image A");

JButton boutonLoad2 = new JButton("Load image B");

JButton boutonProcess = new JButton("Processing Mutual information");

//layout des boutons

this.setLayout(new BorderLayout(0, 200));

//ajout des boutons

this.getContentPane().add(boutonLoad1, BorderLayout.WEST);

this.getContentPane().add(boutonLoad2, BorderLayout.CENTER);

this.getContentPane().add(boutonProcess, BorderLayout.EAST);

this.setVisible(true);

}`

If someone can help me, I would appreciate.

Thanks

网友答案:

In order to take an action with your buttons, you need to implement ActionListener interface, like:

//boutons
    JButton boutonLoad1 = new JButton("Load image A");
    boutonLoad1.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("button 1 is pressed");
        }
    });
    ... do same with your other buttons

According to your prototype image, you will need to play around with layouts. I will give you instructions, as I imagined it could be done with Grid Layout, in order to achieve what you need:

Create your primary JPanel with BoxLayout and set it to Y_AXIS, and at the end add it to your ContentPane

JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
mainPanel.setBorder(new EmptyBorder(10, 10, 10, 10));

Now you'll create and add three more panels to your main panel:

You first (north) panel, which will contain your Labels, will be:

int rows = 0;
int cols = 3;
int hgap = 5; 
int vgap = 0;
JPanel first = new JPanel(new GridLayout(rows,cols,hgap,vgap));

Your second (center) panel, which will contain your Images, will be:

int rows = 0;
int cols = 3;
int hgap = 10;
int vgap = 10;
JPanel second = new JPanel(new GridLayout(rows,cols,hgap,vgap));

Third (south) panel which will contain your Buttons, will be:

int rows = 0;
int cols = 3;
int hgap = 5;
int vgap = 0;
JPanel third = new JPanel(new GridLayout(rows,cols,hgap,vgap));
网友答案:

In order to take an action when a button is pressed you need to add an ActionListener. Inside there you can open a FileDialog to let the user choose a file.

相关阅读:
Top