簡體   English   中英

如何使用 JavaSwing 顯示用戶在 JFrame 上選擇的圖像

[英]How to display an image that a user has selected on the JFrame using JavaSwing

我正在嘗試將“個人資料圖片”添加到用戶的個人資料頁面。 基本上,我有它到他們可以從他們的計算機 select 一個文件並將其上傳到應用程序的地方,它會顯示他們的個人資料圖片。 但是它不起作用,我認為它當前無法顯示它,但生成它不正確。

這是我的代碼

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.ObjectInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JFileChooser;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

class createProfilePage extends JFrame implements ActionListener {
    Container container = getContentPane();

    JLabel name = new JLabel("Name: ");
    JTextField nameField = new JTextField();

    JLabel age = new JLabel("Age: ");
    JTextField ageField = new JTextField();

    JLabel interest = new JLabel("Interests: ");
    JTextField interestField = new JTextField();

    JLabel aboutMe = new JLabel("About me: ");
    JTextField aboutMeField = new JTextField();

    JLabel phoneNum = new JLabel("Phone Number: ");
    JTextField phoneNumberField = new JTextField();

    JButton submit = new JButton("Save Profile");
    JButton deleteProfile = new JButton("Delete Profile");

    JButton uploadPic = new JButton("Upload Profile Picture");

    createProfilePage()
    {
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        //setting container
        setLayoutManager();
        setLocationAndSize();
        addComponents();
        addActionEvent();

        setTitle("Welcome");
        setSize(600, 500);
    }
    public void setLayoutManager() {
        container.setLayout(null);
    }
    public void setLocationAndSize()
    {
        //Setting location and Size of each components using setBounds() method.
        name.setBounds(50,100,100,30);
        age.setBounds(50,170,100,30);
        phoneNum.setBounds(50,240,100,30);
        interest.setBounds(50,310,100,30);
        aboutMe.setBounds(50,380,100,30);

        submit.setBounds(350, 240, 150, 30);
        deleteProfile.setBounds(350,310,150,30);
        uploadPic.setBounds(350,380,150,30);

        nameField.setBounds(150,100,150,30);
        ageField.setBounds(150,170,150,30);
        phoneNumberField.setBounds(150,240,150,30);
        interestField.setBounds(150,310,150,30);
        aboutMeField.setBounds(150,380,150,30);
    }
    public void addComponents() {
        container.add(name);
        container.add(age);
        container.add(phoneNum);
        container.add(interest);
        container.add(aboutMe);
        container.add(nameField);
        container.add(ageField);
        container.add(phoneNumberField);
        container.add(interestField);
        container.add(aboutMeField);
        container.add(submit);
        container.add(deleteProfile);
        container.add(uploadPic);
    }
    public void addActionEvent() {
        submit.addActionListener(this);
        deleteProfile.addActionListener(this);
        uploadPic.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == submit) {
            String name = nameField.getText();
            String age = ageField.getText();
            String phoneNum = phoneNumberField.getText();
            String interest = interestField.getText();
            String aboutMe = aboutMeField.getText();
            try {
                Socket socket = new Socket("localhost", 4242);
                ObjectInputStream reader = new ObjectInputStream(socket.getInputStream());
                //creating user object to send to the server
                User user = new User();
            } catch (IOException b) {
                b.printStackTrace();
            }



            JOptionPane.showMessageDialog(this, "Profile Creation Successful");
        } else if (e.getSource() == deleteProfile) {
            String name = null;
            String age = null;
            String phoneNum = null;
            String interest = null;
            String aboutMe = null;

            JOptionPane.showMessageDialog(this, "Profile Deletion Successful");
        } else if (e.getSource() == uploadPic) {
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
            int result = fileChooser.showOpenDialog(getParent());
            if (result == JFileChooser.APPROVE_OPTION) {
                try {
                    File file = fileChooser.getSelectedFile();
                    //ImageDrawer drawer = new ImageDrawer();
                    Toolkit toolkit = Toolkit.getDefaultToolkit();
                    String stringFile = file.toString();
                    Image image = toolkit.getImage(stringFile);
                    Path path = Paths.get(stringFile);
                    Path imagePath = path.toAbsolutePath();
                    String newStr = imagePath.toString();
                    BufferedImage picture = ImageIO.read(new File(newStr));

                    JLabel picLabel = new JLabel(new ImageIcon(picture));
                    picLabel.setBounds(350, 170, 150, 30);
                    add(picLabel);
                } catch (IOException g) {
                    JOptionPane.showMessageDialog(null,"ERROR");
                }
            }
        }
    }
}

在此處輸入圖像描述

好吧,它現在“起作用”了。 此代碼可以打開並顯示用戶選擇的圖像。 正如Upload Profile Pict...所暗示的那樣,布局仍然損壞(1)。 在這台計算機上猜測的寬度和截斷文本是在 GUI 中使用 position 元素的布局管理器、填充和邊框的眾多原因之一。

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;

final class createProfilePage extends JFrame implements ActionListener {

    Container container = getContentPane();

    JLabel name = new JLabel("Name: ");
    JTextField nameField = new JTextField();
    JLabel age = new JLabel("Age: ");
    JTextField ageField = new JTextField();
    JLabel interest = new JLabel("Interests: ");
    JTextField interestField = new JTextField();
    JLabel aboutMe = new JLabel("About me: ");
    JTextField aboutMeField = new JTextField();
    JLabel phoneNum = new JLabel("Phone Number: ");
    JTextField phoneNumberField = new JTextField();
    JLabel picLabel = new JLabel();

    JButton submit = new JButton("Save Profile");
    JButton deleteProfile = new JButton("Delete Profile");
    JButton uploadPic = new JButton("Upload Profile Picture");

    createProfilePage() {
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        //setting container
        setLayoutManager();
        setLocationAndSize();
        addComponents();
        addActionEvent();

        setTitle("Welcome");
        setSize(600, 500);
    }

    public void setLayoutManager() {
        container.setLayout(null);
    }

    public void setLocationAndSize() {
        //Setting location and Size of each components using setBounds() method.
        name.setBounds(50, 100, 100, 30);
        age.setBounds(50, 170, 100, 30);
        phoneNum.setBounds(50, 240, 100, 30);
        interest.setBounds(50, 310, 100, 30);
        aboutMe.setBounds(50, 380, 100, 30);

        submit.setBounds(350, 240, 150, 30);
        deleteProfile.setBounds(350, 310, 150, 30);
        uploadPic.setBounds(350, 380, 150, 30);

        nameField.setBounds(150, 100, 150, 30);
        ageField.setBounds(150, 170, 150, 30);
        phoneNumberField.setBounds(150, 240, 150, 30);
        interestField.setBounds(150, 310, 150, 30);
        aboutMeField.setBounds(150, 380, 150, 30);
        picLabel.setBounds(350, 50, 150, 150);
    }

    public void addComponents() {
        container.add(name);
        container.add(age);
        container.add(phoneNum);
        container.add(interest);
        container.add(aboutMe);
        container.add(nameField);
        container.add(ageField);
        container.add(phoneNumberField);
        container.add(interestField);
        container.add(aboutMeField);
        container.add(picLabel);
        container.add(submit);
        container.add(deleteProfile);
        container.add(uploadPic);
    }

    public void addActionEvent() {
        submit.addActionListener(this);
        deleteProfile.addActionListener(this);
        uploadPic.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == uploadPic) {
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
            int result = fileChooser.showOpenDialog(getParent());
            if (result == JFileChooser.APPROVE_OPTION) {
                try {
                    File file = fileChooser.getSelectedFile();
                    BufferedImage picture = ImageIO.read(file);

                    picLabel.setIcon(new ImageIcon(picture));
                    add(picLabel);
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                    JOptionPane.showMessageDialog(null, "ERROR");
                }
            }
        }
    }

    public static void main(String[] args) {
        Runnable r = () -> {
            new createProfilePage().setVisible(true);
        };
        SwingUtilities.invokeLater(r);
    }
}
  1. 就個人而言,我會對這個外觀采取不同的方法。 所有按鈕的頂部工具欄。 如那里所示,左側的兩列標簽和字段,但 label 文本向右對齊,並且字段根據需要不同大小。 甚至可以使“關於我:”成為一個文本區域,而不是一個字段。 然后,在標簽/字段組合的右側,專用於圖片 label 的寬度和高度的 rest。 它將顯示在滾動窗格中(除非圖片大小相同)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM