由于某些原因,我的GUI程序在netbeans不能正常工作;我试着编写一个程序,显示下载东西之前需要多少时间,所以我有一个微调项,表示每秒传输多少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();
我希望它能帮助你。