【问题标题】:How to load a Java GUI class from a main class?如何从主类加载 Java GUI 类?
【发布时间】:2014-09-12 07:55:46
【问题描述】:

我是 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类

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                   
}

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

【问题讨论】:

  • 显示FileExtractorGUI类的内容。
  • 您需要添加一个顶级容器并将您的 UI 作为子级放入此顶级容器中,请参阅docs.oracle.com/javase/tutorial/uiswing/components/…
  • 您应该将面板放在JFrame 中,然后您可以将其设置为可见。此外,您应该始终只在事件调度线程中创建和访问 swing 组件,如 here 所示。

标签: java swing user-interface


【解决方案1】:
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);
        }
    });
}

【讨论】:

    【解决方案2】:

    您无法显示FileExtractorGUI,因为它扩展了JPanel。 每个组件必须是 JFrame 或 Window 的子组件。

    换行

    public class FileExtractorGUI extends javax.swing.JPanel
    

    到

    public class FileExtractorGUI extends JFrame
    

    【讨论】:

      【解决方案3】:

      首先创建一个 JFrame,然后用您的面板填充它。

      来自doc

      //1. Create the frame.
      JFrame frame = new JFrame("YouFrame");
      //2. Optional: What happens when the frame closes?
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      //3. Create your panel and put it in the frame.
      frame.getContentPane().add(new FileExtractorGUI(), BorderLayout.CENTER);
      //4. Size the frame.
      frame.pack();
      //5. Show it.
      frame.setVisible(true);
      

      【讨论】:

        【解决方案4】:

        尝试创建一个JFrame并添加Panel

                FileExtractorGUI gui = new FileExtractorGUI();
        
                JFrame jf=new JFrame();
                jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                jf.setSize(500, 500);
                jf.add(gui);
                jf.setVisible(true);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-06-13
          • 2017-11-26
          • 1970-01-01
          • 2021-04-21
          相关资源
          最近更新 更多