Java GUI 表单中的图像未显示,对话框消息未工作。

huangapple go评论82阅读模式
英文:

java gui form image not showing dialog message not working

问题

以下是您提供的代码的翻译部分:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class MyFrame extends JFrame {
    public MyFrame() {
        super("自我介绍");
        setLayout(new FlowLayout());

        JLabel nameLabel = new JLabel("姓名");
        JTextField nameTextField = new JTextField("随机名字", 20);
        nameTextField.setEnabled(false);
        nameTextField.setBackground(Color.gray);
        nameLabel.setVerticalAlignment(SwingConstants.TOP);
        add(nameLabel);
        add(nameTextField);

        JLabel dobLabel = new JLabel("出生日期");
        JTextField dobTextField = new JTextField("不是今天", 20);
        dobTextField.setEnabled(false);
        dobTextField.setBackground(Color.gray);
        dobLabel.setVerticalAlignment(SwingConstants.CENTER);
        add(dobLabel);
        add(dobTextField);

        JLabel emailLabel = new JLabel("电子邮箱");
        JTextField emailTextField = new JTextField("thisismyemail@gmail.com", 20);
        emailTextField.setEnabled(false);
        emailTextField.setBackground(Color.gray);
        emailLabel.setVerticalAlignment(SwingConstants.BOTTOM);
        add(emailLabel);
        add(emailTextField);

        ImageIcon imageIcon = new ImageIcon("a.jpg");
        JLabel imageLabel = new JLabel("我对这个主题的评论,将来会进一步更新", imageIcon, SwingConstants.LEFT);
        imageLabel.setToolTipText("我是一只兔子");
        add(imageLabel);

        JTextField comment1TextField = new JTextField(20);
        add(comment1TextField);

        JTextField comment2TextField = new JTextField(20);
        add(comment2TextField);

        ActionListener commentListener = new ActionListener() {
            String c1 = comment1TextField.getText();
            String c2 = comment2TextField.getText();
            String s = "1. " + c1 + " 2. " + c2;

            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, s, "对课程的建议", JOptionPane.WARNING_MESSAGE);
            }
        };

        comment1TextField.addActionListener(commentListener);
        comment2TextField.addActionListener(commentListener);
    }
}

class Lab3 {
    public static void main(String[] args) {
        MyFrame myFrame = new MyFrame();
        myFrame.setSize(200, 300);
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.setVisible(true);
    }
}

请注意,我已经根据您提供的代码进行了翻译,但由于您希望只返回翻译好的部分,所以我没有包含任何其他内容。如果您有任何其他问题或需要进一步的帮助,请随时问我。

英文:

I would like to insert an image into my JFrame but it is not showing. The image file is "a.jpg"

This is the output I get

Java GUI 表单中的图像未显示,对话框消息未工作。

Here is my code.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class myself extends JFrame
{	
	public myself()
	{
		super("Introduction to myself");
		setLayout(new FlowLayout());

        JLabel jl = new JLabel("Name");
		JTextField jtf = new JTextField("random name",20);
        jtf.setEnabled(false);
        jtf.setBackground(Color.gray);
        jl.setVerticalAlignment(SwingConstants.TOP);
        add(jl);
        add(jtf);

        JLabel jl1 = new JLabel("Date of Birth");
		JTextField jtf1 = new JTextField("not today",20);
		jtf1.setEnabled(false);
        jtf1.setBackground(Color.gray);
        jl1.setVerticalAlignment(SwingConstants.CENTER);
        add(jl1);
        add(jtf1);

        JLabel jl2 = new JLabel("Email");
        JTextField jtf2 = new JTextField("thisismyemail@gmail.com",20);
        jtf2.setEnabled(false);
        jtf2.setBackground(Color.gray);
        jl2.setVerticalAlignment(SwingConstants.BOTTOM);
        add(jl2);
        add(jtf2);

		ImageIcon ic = new ImageIcon("a.jpg");
        JLabel jl3 = new JLabel("My comments to the subject, will update further", ic, SwingConstants.LEFT);
        jl3.setToolTipText ("I am rabbit");
        add(jl3);

        JTextField comment1 = new JTextField(20);
        add(comment1);

        JTextField comment2 = new JTextField(20);
		add(comment2);

        comment1.addActionListener(new ActionListener()
        {
            String c1 = comment1.getText();
            String c2 = comment2.getText();
            String s = "1. " + c1 + " 2. " + c2;

            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, s, "My suggestion to the course", JOptionPane.WARNING_MESSAGE);
            }
        });

        comment2.addActionListener(new ActionListener()
        {
            String c1 = comment1.getText();
            String c2 = comment2.getText();
            String s = String.format("1. " + c1 + " 2. " + c2);

            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, s, "My suggestion to the course", JOptionPane.WARNING_MESSAGE);
            }
        });
    }
}

class Lab3
{
	public static void main (String [ ] args)
	{
		myself ms = new myself();
		ms.setSize(200, 300);
		ms.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		ms.setVisible(true);
	}
}

Also, apart from the image problem, I am also facing problem with my ActionListener.
I would like to get this as my output, after I finish typing in the text boxes and hit ENTER

Java GUI 表单中的图像未显示,对话框消息未工作。

But this is what I get with the code I have right now.

Java GUI 表单中的图像未显示,对话框消息未工作。

答案1

得分: 1

  1. 将类名 myself 更改为 Myself,根据 Java 命名规范

  2. 使用更合适的布局管理器。GridBagLayout 适用于包含 JLabelJTextField 配对的表单。参考 在容器内布置组件

  3. 你的图像文件未找到。根据你的代码,它应该位于当前工作目录中,该值由以下代码返回。

System.getProperty("user.dir")

参考 如何使用图标

  1. 为了使 actionPerformed() 方法能够正确显示来自 comment1comment2 的文本,你需要从方法内部获取文本。参考 嵌套类

  2. 调用方法 pack 而不是 setSize()

  3. 还考虑调用方法 setLocationByPlatform

以下是你重构后的代码版本。

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class Lab3 {
    public static void main(String[] args) {
        Myself ms = new Myself();
        ms.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ms.pack();
        ms.setLocationByPlatform(true);
        ms.setVisible(true);
    }
}

class Myself extends JFrame {
    public Myself() {
        super("Introduction to myself");
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();

        gbc.gridx = 0;
        gbc.gridy = 0;
        JLabel jl = new JLabel("Name");
        JTextField jtf = new JTextField("random name", 20);
        jtf.setEnabled(false);
        jtf.setBackground(Color.gray);
        jl.setVerticalAlignment(SwingConstants.TOP);
        add(jl, gbc);
        gbc.gridx = 1;
        add(jtf, gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        JLabel jl1 = new JLabel("Date of Birth");
        JTextField jtf1 = new JTextField("not today", 20);
        jtf1.setEnabled(false);
        jtf1.setBackground(Color.gray);
        jl1.setVerticalAlignment(SwingConstants.CENTER);
        add(jl1, gbc);
        gbc.gridx = 1;
        add(jtf1, gbc);

        gbc.gridx = 0;
        gbc.gridy = 2;
        JLabel jl2 = new JLabel("Email");
        JTextField jtf2 = new JTextField("thisismyemail@gmail.com", 20);
        jtf2.setEnabled(false);
        jtf2.setBackground(Color.gray);
        jl2.setVerticalAlignment(SwingConstants.BOTTOM);
        add(jl2, gbc);
        gbc.gridx = 1;
        add(jtf2, gbc);

        gbc.gridx = 0;
        gbc.gridy = 3;
        ImageIcon ic = new ImageIcon("a.jpg");
        JLabel jl3 = new JLabel("My comments to the subject, will update further",
                                ic,
                                SwingConstants.LEFT);
        jl3.setToolTipText("I am rabbit");
        add(jl3, gbc);

        gbc.gridx = 1;
        JTextField comment1 = new JTextField(20);
        add(comment1, gbc);

        gbc.gridy = 4;
        JTextField comment2 = new JTextField(20);
        add(comment2, gbc);

        comment1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                String c1 = comment1.getText();
                String c2 = comment2.getText();
                String s = "1. " + c1 + " 2. " + c2;
                JOptionPane.showMessageDialog(Myself.this, s, "My suggestion to the course",
                        JOptionPane.WARNING_MESSAGE);
            }
        });

        comment2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String c1 = comment1.getText();
                String c2 = comment2.getText();
                String s = String.format("1. " + c1 + " 2. " + c2);
                JOptionPane.showMessageDialog(Myself.this, s, "My suggestion to the course",
                        JOptionPane.WARNING_MESSAGE);
            }
        });
    }
}

以上是你的代码翻译和整理后的版本。

英文:
  1. Change name of class myself to Myself according to java naming conventions
  2. Use a more appropriate layout manager. GridBagLayout is good for forms that contain JLabel and JTextField pairs. Refer to Laying Out Components Within a Container
  3. Your image file is not found. According to your code, it is supposed to be in the current working directory which is the value returned by this code.
System.getProperty("user.dir")

Refer to How to Use Icons

  1. In order for the actionPerformed() method to correctly display the text from comment1 and comment2, you need to get the text from inside the method. Refer to Nested Classes
  2. Call method pack rather than setSize().
  3. Consider also calling method setLocationByPlatform

Here is my refactored version of your code.

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class Lab3 {
    public static void main(String[] args) {
        Myself ms = new Myself();
        ms.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ms.pack();
        ms.setLocationByPlatform(true);
        ms.setVisible(true);
    }
}

class Myself extends JFrame {
    public Myself() {
        super("Introduction to myself");
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();

        gbc.gridx = 0;
        gbc.gridy = 0;
        JLabel jl = new JLabel("Name");
        JTextField jtf = new JTextField("random name", 20);
        jtf.setEnabled(false);
        jtf.setBackground(Color.gray);
        jl.setVerticalAlignment(SwingConstants.TOP);
        add(jl, gbc);
        gbc.gridx = 1;
        add(jtf, gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        JLabel jl1 = new JLabel("Date of Birth");
        JTextField jtf1 = new JTextField("not today", 20);
        jtf1.setEnabled(false);
        jtf1.setBackground(Color.gray);
        jl1.setVerticalAlignment(SwingConstants.CENTER);
        add(jl1, gbc);
        gbc.gridx = 1;
        add(jtf1, gbc);

        gbc.gridx = 0;
        gbc.gridy = 2;
        JLabel jl2 = new JLabel("Email");
        JTextField jtf2 = new JTextField("thisismyemail@gmail.com", 20);
        jtf2.setEnabled(false);
        jtf2.setBackground(Color.gray);
        jl2.setVerticalAlignment(SwingConstants.BOTTOM);
        add(jl2, gbc);
        gbc.gridx = 1;
        add(jtf2, gbc);

        gbc.gridx = 0;
        gbc.gridy = 3;
        ImageIcon ic = new ImageIcon("a.jpg");
        JLabel jl3 = new JLabel("My comments to the subject, will update further",
                                ic,
                                SwingConstants.LEFT);
        jl3.setToolTipText("I am rabbit");
        add(jl3, gbc);

        gbc.gridx = 1;
        JTextField comment1 = new JTextField(20);
        add(comment1, gbc);

        gbc.gridy = 4;
        JTextField comment2 = new JTextField(20);
        add(comment2, gbc);

        comment1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                String c1 = comment1.getText();
                String c2 = comment2.getText();
                String s = "1. " + c1 + " 2. " + c2;
                JOptionPane.showMessageDialog(Myself.this, s, "My suggestion to the course",
                        JOptionPane.WARNING_MESSAGE);
            }
        });

        comment2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String c1 = comment1.getText();
                String c2 = comment2.getText();
                String s = String.format("1. " + c1 + " 2. " + c2);
                JOptionPane.showMessageDialog(Myself.this, s, "My suggestion to the course",
                        JOptionPane.WARNING_MESSAGE);
            }
        });
    }
}

And this is how it looks when I run the above code.

Java GUI 表单中的图像未显示,对话框消息未工作。

huangapple
  • 本文由 发表于 2020年10月27日 12:33:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/64548354.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定