java - Netbeans GUI 不显示对象

标签 java swing netbeans calculator builder

由于某种原因,我在 netbeans 中的 GUI 程序无法正常工作; 我尝试制作一个程序,说明下载某些内容之前需要多长时间,因此我有一个用于每秒传输多少 kb/mb/gb 的微调器,一个用于已完成进度的微调器和一个用于目标的微调器kb/gb/mb。这是我的代码

 /*
 * 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.
 */
package GUI;

import java.math.BigInteger;

/**
 *
 * @author Jari
 */
public class InternetSpeed extends javax.swing.JPanel {

    /**
     * Creates new form InternetSpeed
     */
    public InternetSpeed() {
        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() {

        spinnerComboSpeed = new javax.swing.JSpinner();
        comboUnitSpeed = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jSpinner1 = new javax.swing.JSpinner();
        comboTarget = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        Progress = new javax.swing.JProgressBar();
        completed = new javax.swing.JLabel();
        comboCompleted = new javax.swing.JComboBox();
        completedSpinner = new javax.swing.JSpinner();
        calcButton = new javax.swing.JButton();
        totLabel = new javax.swing.JLabel();

        comboUnitSpeed.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "KB", "MB", "GB" }));

        jLabel1.setText("Snelheid per seconde");

        comboTarget.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "KB", "MB", "GB" }));

        jLabel2.setText("te behalen volume");

        completed.setText("Momenteel behaald");

        comboCompleted.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "KB", "MB", "GB" }));
        comboCompleted.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboCompletedActionPerformed(evt);
            }
        });

        calcButton.setText("Bereken");
        calcButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                calcButtonMouseClicked(evt);
            }
        });

        totLabel.setText("0s");
        totLabel.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                totLabelPropertyChange(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()
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(calcButton)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(Progress, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                    .addComponent(completed))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jSpinner1, javax.swing.GroupLayout.DEFAULT_SIZE, 44, Short.MAX_VALUE)
                                    .addComponent(spinnerComboSpeed)
                                    .addComponent(completedSpinner))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(comboUnitSpeed, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(comboTarget, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(comboCompleted, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(totLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(130, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(118, 118, 118)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(spinnerComboSpeed, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(comboUnitSpeed, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(completed)
                    .addComponent(comboCompleted, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(completedSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(comboTarget, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(totLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(Progress, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(calcButton)
                .addContainerGap(43, Short.MAX_VALUE))
        );

        calcButton.getAccessibleContext().setAccessibleParent(calcButton);
        totLabel.getAccessibleContext().setAccessibleParent(calcButton);
    }// </editor-fold>                        

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

    private void calcButtonMouseClicked(java.awt.event.MouseEvent evt) {                                        
        // TODO add your handling code here:
        int speed = 0, comp = 0, tot= 0;
        switch ((char) comboUnitSpeed.getSelectedIndex()) {
            case '0':
                speed = (int) spinnerComboSpeed.getValue() * 1;
            case '1':
                speed = (int) spinnerComboSpeed.getValue() * 1000;
            case '2':
                speed = (int) spinnerComboSpeed.getValue() * 1000000;
        }
        switch ((char) comboCompleted.getSelectedIndex()) {
            case '0':
                comp = (int) completedSpinner.getValue() * 1;
            case '1':
                comp = (int) completedSpinner.getValue() * 1000;
            case '2':
                comp = (int) completedSpinner.getValue() * 1000000;
        }
        switch ((char) comboTarget.getSelectedIndex()) {
            case '0':
                tot = (int) jSpinner1.getValue() * 1;
            case '1':
                tot = (int) jSpinner1.getValue() * 1000;
            case '2':
                tot = (int) jSpinner1.getValue() * 1000000;
        }
        int totaal = tot - comp;
        BigInteger sp = BigInteger.valueOf(speed);
        BigInteger t = BigInteger.valueOf(totaal);
        BigInteger u = t.gcd(sp);

        int uit = u.intValue();
        totLabel.setText(uit+"s");
    }                                       

    private void totLabelPropertyChange(java.beans.PropertyChangeEvent evt) {                                        
        // TODO add your handling code here:
    }                                       


    // Variables declaration - do not modify                     
    private javax.swing.JProgressBar Progress;
    private javax.swing.JButton calcButton;
    private javax.swing.JComboBox comboCompleted;
    private javax.swing.JComboBox comboTarget;
    private javax.swing.JComboBox comboUnitSpeed;
    private javax.swing.JLabel completed;
    private javax.swing.JSpinner completedSpinner;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JSpinner jSpinner1;
    private javax.swing.JSpinner spinnerComboSpeed;
    private javax.swing.JLabel totLabel;
    // End of variables declaration                   
}

最佳答案

尝试将所有 GUI 放入面板中,然后输入:

int uit = u.intValue();
totLabel.setText(uit+"s");
panel1.repaint();

希望对你有帮助。

关于java - Netbeans GUI 不显示对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30273635/

相关文章:

java - 使用生产者-消费者模式处理巨大的 CSV 文件

java - 添加/删除元素时创建新数组?

java - 选项卡是哪种对象?

java - Netbeans 删除一个内置类?

java - 如何在 Maven 中仅针对 "war"包类型运行插件?

java - Dropwizard 自定义验证注释不起作用

java - Java 应用程序中的 PKIX 路径构建失败

java - 从 Java 中的组合框中删除所有项目

java - Apache Derby [Hibernate 框架] 看不到创建的表。 [Java EE + JSP]

带有嵌入式数据库的 Java 桌面应用程序