我正在尝试添加一个“个人资料图片”到一个用户的个人资料页。基本上,我有它到那里他们可以选择一个文件从他们的计算机上传到应用程序,它将显示他们的个人资料图片。但它不工作,我认为它目前不能显示它,但生成它不正确。
null
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中定位元素的众多原因之一。
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);
}
}
问题内容: 我正在用Swift编写我的第一个iOS应用程序(仅iPhone)。主应用程序视图应允许用户从相册中选择图像。 我找到了以下 ViewController.swift 示例代码: 并具有以下View Controller Scene- 但是,当我启动该应用程序时,仅显示黑屏。我做错了什么?我发现的另一个示例代码是在Objective-C中,这对我没有帮助。 问题答案: 如果只想让用户使用
我们的应用程序正在使用 gapi.auth.登录进行身份验证。问题是,当用户登录到多个帐户时,不会显示帐户选择下拉列表。目前,为了克服这个问题,应用程序设置。显然,这并不是很有效。 使用时是否可以显示多用户选择下拉列表gapi.auth.signin? 用gapi.auth.authorize代替吗?(相关问题) 非常感谢。
在我的Swing应用程序中,我有2个JFrameA和B。当我单击JFrameA上的按钮时,它会打开JFrameB并隐藏它自己(我设法完成了这部分) 在JFrame B上,我在JTabbedPane上放置了4个JPanels。每个JPanel有2个JButtons。 我该怎么做? //JPanel类 公共类AddItemPanel扩展javax.swing.jPanel{
问题内容: 我想在我的网站上实时显示所有在线用户。虽然不知道如何去做。在新用户登录后添加新用户并不难,但我还需要删除不再登录的用户。 任何想法如何做到这一点?我应该使用jQuery检查哪些用户已注销并从列表中将其删除吗? 问题答案: 您的问题将是人们在不注销的情况下离开,他们的会话将仍然存在多长时间,直到您设置了超时时间才可以收集他们的会话数据(实际上可能更长) 为了获得真正准确的登录者和访问站点
问题内容: 我有点卡住。为什么不做这项工作?我只是收到一条错误消息: java.lang.NoSuchMethodError:主要 线程“主”中的异常 问题答案: main需要是静态的,并且必须具有String []而不是String的参数。 为了解决这个问题,将所有东西都放在构造函数中,例如
问题内容: 我想在同一JFrame中显示同一图像的变体,例如在JFrame中显示图像,然后将其替换为同一图像的灰度。 问题答案: 每当您更新图像时,都必须重新粉刷。 这是一个有关该主题的简单Google提出的内容:(我将这些教程用于我的所有Java编码) Java教程:绘制图像