当前位置: 首页 > 面试题库 >

如何从主类加载Java GUI类?

司空思聪
2023-03-14
问题内容

我是Java UI编程的新手。

如何在Java中从主类调用UI类

public static void main(String[] args) {
    // TODO code application logic here
    FileExtractorGUI gui = new FileExtractorGUI();
    gui.setVisible(true);
}

基本上

FileExtractorGUI is a gui class.

运行程序时如何加载GUI。目前,当我运行代码时什么也没有发生。

GUI CLass

package fileextractor;

import javax.swing.JOptionPane;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author 
 */
public class FileExtractorGUI extends javax.swing.JPanel {

    /**
     * Creates new form FileExtractorGUI
     */
    public FileExtractorGUI() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        SourceField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        DestinationField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        Extract = new javax.swing.JButton();

        SourceField.setText("SourceField");
        SourceField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SourceFieldActionPerformed(evt);
            }
        });

        jLabel1.setText("SourceFolder");

        DestinationField.setText("DestinationField");

        jLabel2.setText("DestinationFolder");

        Extract.setText("Extract");
        Extract.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ExtractActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(38, 38, 38)
                        .addComponent(SourceField, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24, Short.MAX_VALUE)
                        .addComponent(DestinationField, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(74, 74, 74))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(Extract)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(SourceField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(DestinationField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 194, Short.MAX_VALUE)
                .addComponent(Extract)
                .addContainerGap())
        );
    }// </editor-fold>

    private void SourceFieldActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
    }

    private void ExtractActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        String src = SourceField.getText();
        String dest = DestinationField.getText();
        CopyDirectoryExample copy = new CopyDirectoryExample();
        copy.extract(src, dest);
        JOptionPane.showMessageDialog(null, "Extracted!");
    }


    // Variables declaration - do not modify                     
    private javax.swing.JTextField DestinationField;
    private javax.swing.JButton Extract;
    private javax.swing.JTextField SourceField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    // End of variables declaration                   
}

根据评论中的要求,我已经上传了GUI类代码


问题答案:
public static void main(String[] args) {    
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            FileExtractorGUI gui = new FileExtractorGUI();
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(gui);
            frame.pack();
            frame.setVisible(true);
        }
    });
}


 类似资料:
  • 问题内容: 带有以下源代码: 我可以用: 实例化该类,但我必须将此源文件()放在类路径中 我想从数据库动态加载此类(将源代码存储为或) 这可能吗 ? 问题答案: 假设您已经编译了该类,则可以创建一个,从数据库中加载该类。 如果数据库仅包含源代码,则必须首先对其进行编译-查看Java编译器API,了解如何在没有任何文件的情况下执行此操作。 请注意,只要类加载器处于活动状态,以这种方式加载的类就会保持

  • 问题内容: 我正在使用一些第三方代码,这些代码在给定“ -classpath”命令行参数时不会设置java.class.path,而是仅创建一个类加载器,将命令行中指定的类路径中所有项的所有url添加到类加载器,然后将其设置为上下文类加载器。在我编写的此代码的插件类中,我获得了该类加载器的实例,并且需要以某种方式使用它来获取基础类路径,以便可以在JavaCompiler.getTask(… ),并

  • 如何从JNI中JAR文件的字节数组加载所有类<我的代码 我需要读取JAR文件的字节,将它们转换成jbytes并从这些字节加载所有java类 比如java中的代码

  • 我的Netbeans在早些时候使用jdk 1.8.0.65时工作正常,但由于某些原因,我丢失了数据,我安装了jdk 1.8.0.73,现在我的Netbeans上的每个程序都显示了以下错误: 运行:错误:无法找到或加载主类序列。序列C:\Users\owais\AppData\Local\NetBeans\Cache\8.1\executor snippets\run。xml:53:Java返回:1

  • 我正在尝试将一个字符串从topicRNG加载到changeXML。我以前在类之间加载过变量,但现在无法运行 首先,我有我的代码,我试图加载它。包装试验; 然后是我试图加载的代码, 当我从GenreDefiner加载int时,我把它设置成这样, 我尝试过“将公共静态字符串放入topicFinal”,但它给了我一个错误,当我将它放在“公共静态void main(String args[]){}”之外时

  • 问题内容: 我正在尝试在Android平台上的运行时动态加载类。该类包含在单独的库JAR文件中,但与APK打包在一起(按照SDK中的新库机制)。使用Class.forname方法时,我收到未找到的类异常。我已经看到了有关DexClassLoader方法的一些讨论,但是我找不到如何使用它的好例子(以及是否是最好的使用方法- 它似乎比forname方法复杂得多!)。 如果有人可以提供示例代码片段来说明