Swing没有显示对源代码的更改

Swing没有显示对源代码的更改

本文介绍了Java Swing没有显示对源代码的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在使用带有Netbeans的Java Swing图形编辑器来创建我的项目...但是使用它带来了一些限制,例如我无法使用Java swing选项将图像添加到jpanel.因此,我需要对其进行编码,以实现一个新的jPanel.

I'm using Java Swing graphical editor with netbeans to make my project...but using it brings some limitations like I can't add to a jpanel an image,using java swing options. So i'll need to code it, implementing a new jPanel.

我的问题是无法编辑由Java swing图形编辑器生成的代码,因此与其在initComponents()节中添加新的JPanel代码,不如在我的构造函数中调用此函数后执行此操作主JPanel.

My problem is that the code generated by the java swing graphical editor can't be edited so instead of adding the new JPanel code in the initComponents() section I'm doing it after this function is called in the constructor of my main JPanel.

但是我添加的任何代码都不会被"Designer"识别,这意味着在创建编码对象之后,我将无法在"Designer"中使用它们,因此必须对所有内容进行编码,考虑到这样做的难易程度,这很痛苦在设计器"工具中预览和移动元素.

But any code I add is not recognized by the "Designer" which means that after making my coded objects I can't use them in the "Designer" and everything must be coded, which is a pain considering how much easier is previewing and moving elements in the "Designer" tool.

我如何编码我想要的东西,但是钢会出现在"DEsigner"中?

How can I code what I want but steel appear in the "DEsigner"?

提前谢谢

推荐答案

以下是使用NetBeans GUI编辑器将图像添加到JPanel的两种方法.下面的类ImagePanel是使用New JPanel Form命令创建的.

Here are two ways to add an image to a JPanel using the NetBeans GUI editor. The class ImagePanel below is created using the New JPanel Form command.

非设计者:第一种方法修改了构造函数以设置背景图像,并且该类重写了paintComponent()来绘制图像.编辑器内部没有代码可以更改更改.

Non-designer: The first approach modifies the constructor to set a background image, and the class overrides paintComponent() to draw the image. No code inside the editor fold changes.

Designer :第二种方法是使用GUI设计器添加名为imageLabelJLabel.用居中的Icon创建JLabel的代码在名为Custom Creation Code的属性中,而以下两行在Post-Creation Code中.

Designer: Using the GUI designer, the second approach adds a JLabel named imageLabel. The code to create the JLabel with a centered Icon goes in the property named Custom Creation Code, while the following two lines go in the Post-Creation Code.

package overflow;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

public class ImagePanel extends javax.swing.JPanel {

    private Image image;

    /** Creates new form ImagePanel */
    public ImagePanel(Image image) {
        this.image = image;
        this.setPreferredSize(new Dimension(
            image.getWidth(null), image.getHeight(null)));
        initComponents();
    }

    @Override
    protected void paintComponent(Graphics g) {
        g.drawImage(image, 0, 0, null);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        imageLabel = new JLabel(new ImageIcon("image2.jpg"), JLabel.CENTER);
        imageLabel.setHorizontalTextPosition(JLabel.CENTER);
        imageLabel.setVerticalTextPosition(JLabel.TOP);

        setLayout(new java.awt.GridLayout());

        imageLabel.setText("imageLabel");
        add(imageLabel);
    }// </editor-fold>


    // Variables declaration - do not modify
    private javax.swing.JLabel imageLabel;
    // End of variables declaration

}

这里是一个合适的Main类和main()方法来显示面板:

Here's a suitable Main class and main() method to display the panel:

package overflow;

import java.awt.EventQueue;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;

public class Main {
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                try {
                    f.add(new ImagePanel(ImageIO.read(new File("image1.jpg"))));
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                f.pack();
                f.setVisible(true);
            }
        });
    }
}

http://i41.tinypic.com/dmw4nl.png http://i41.tinypic. com/dmw4nl.png

这篇关于Java Swing没有显示对源代码的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 08:30