将图像和文本字段放在同一个 class

Place image and textfield in the same class

我下面的 java 代码试图将图像和文本字段放在同一个 class 中。我是 java 编程的新手,我知道用另一种语言我会去缩放 var pic = image 和 var text = textfield。这就是我在此 java 代码中寻找的全部内容。只需声明一个文本字段和图像并将其显示在相同的 window.

import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.*;  

class Main extends JFrame {

    public static void main(String[] args0) {

        try {
            JFrame frame = new JFrame();
            File imageFile = new File("/Users/johnzalubski/Desktop/j/gg.jpg");
            Image i = ImageIO.read(imageFile);
            ImageIcon image = new ImageIcon(i);
            JLabel imageLabel = new JLabel(image);
            frame.add(imageLabel);
            frame.setLayout(null);
            imageLabel.setLocation(0, 0);
            imageLabel.setSize(300, 250);
            imageLabel.setVisible(true);
            frame.setVisible(true);
            frame.setSize(1000, 750);
            frame.setDefaultCloseOperation(EXIT_ON_CLOSE);

            JFrame f= new JFrame("TextField Example");  

            JTextField t1,t2;  
            t1=new JTextField("Welcome to Javatpoint.");  
            t1.setBounds(50,100, 200,30);  
            t2=new JTextField("AWT Tutorial");  
            t2.setBounds(50,150, 200,30);  
            frame.add(t1); f.add(t2);  
            frame.setSize(4000,400);  
            frame.setLayout(null);  
            frame.setVisible(true);  

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

这里有一个简单的例子可以让你开始。请注意以下评论:

import java.awt.BorderLayout;
import java.io.IOException;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

class Main {

    public static void main(String[] args0) {

        try {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //use public resources when posting questions or answers
            URL url = new URL("http://www.digitalphotoartistry.com/rose1.jpg");
            ImageIcon image= new ImageIcon(url);
            JLabel imageLabel = new JLabel(image);
            //add the label to the frame using the default layout manager (BorderLayout)
            frame.add(imageLabel); //add label to the default position BorderLayout.CENTER
            //frame.setLayout(null); //do not use null layout
            //imageLabel.setLocation(0, 0); //do not set bounds, that is the job of the layout manager

            JTextField t1 =new JTextField("Welcome to Javatpoint.");
            frame.add(t1, BorderLayout.SOUTH); //add text field to the frame using BorderLayout other position
            frame.pack();
            frame.setVisible(true);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

编辑:Main 不需要扩展 JFrame。这只是一个剩菜。