Java swing 不显示源代码的更改

发布于 2024-08-31 19:01:53 字数 390 浏览 3 评论 0原文

我使用 Java Swing 图形编辑器和 netbeans 来制作我的项目...但是使用它会带来一些限制,例如我无法使用 java swing 选项向 jpanel 添加图像。所以我需要对其进行编码,实现一个新的 jPanel。

我的问题是 java swing 图形编辑器生成的代码无法编辑,因此我不是在 initComponents() 部分添加新的 JPanel 代码,而是在主 JPanel 的构造函数中调用此函数后执行此操作。

但是我添加的任何代码都不会被“设计器”识别,这意味着在制作编码对象后,我无法在“设计器”中使用它们,并且所有内容都必须编码,考虑到预览和移动要容易得多,这是一个痛苦“设计器”工具中的元素。

我怎样才能编码我想要的东西,但钢出现在“DEsigner”中?

提前谢谢

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.

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.

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.

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

Thx in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

向地狱狂奔 2024-09-07 19:01:53

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

非设计者:第一种方法修改构造函数来设置背景图像,类重写 paintComponent() 来绘制图像。编辑器折叠内的代码没有更改。

设计器:使用 GUI 设计器,第二种方法添加一个名为 imageLabelJLabel。创建带有居中 IconJLabel 的代码位于名为 Custom 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() 方法来显示面板:

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

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.

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: 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

}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文