这个Java随机软件代码有什么问题?

时间:2015-11-08 11:54:58

标签: java

我用Java制作了软件,可以生成范围内的随机数。但它不起作用。对不起,奇怪的语言和评论。我有三个类:main,用于生成和GUI。当我点击按钮生成它在屏幕上显示0。请帮帮我。

public class glavnaKlasa {
static public int broj;

public static void main(String[] args) {
    GUI guiObject = new GUI();
    guiObject.mainGUI();

    generisanje generisanjeObject = new generisanje();
    broj = generisanjeObject.glavno(guiObject.min, guiObject.max);
}
}


import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class GUI {

private JFrame frmNasumicniBroj;
private JTextField textField;
private JTextField textField_1;
private JLabel lblNasumicniBroj;
private JTextField textField_2;

/**
 * Launch the application.
 */
public static void mainGUI() {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                GUI window = new GUI();
                window.frmNasumicniBroj.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public GUI() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
public int min, max;

private void initialize() {
    frmNasumicniBroj = new JFrame();
    frmNasumicniBroj.setTitle("Nasumicni broj");
    frmNasumicniBroj.getContentPane().setBackground(Color.CYAN);
    frmNasumicniBroj.setBounds(100, 100, 400, 300);
    frmNasumicniBroj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frmNasumicniBroj.getContentPane().setLayout(null);

    JLabel lblNewLabel = new JLabel("Donja granica: ");
    lblNewLabel.setForeground(Color.YELLOW);
    lblNewLabel.setFont(new Font("Wide Latin", Font.PLAIN, 12));
    lblNewLabel.setBounds(10, 11, 154, 14);
    frmNasumicniBroj.getContentPane().add(lblNewLabel);

    textField = new JTextField();
    textField.setBounds(200, 8, 154, 20);
    frmNasumicniBroj.getContentPane().add(textField);
    textField.setColumns(10);

    JLabel lblGornjaGranica = new JLabel("Gornja granica: ");
    lblGornjaGranica.setForeground(Color.YELLOW);
    lblGornjaGranica.setFont(new Font("Wide Latin", Font.PLAIN, 12));
    lblGornjaGranica.setBounds(10, 36, 165, 14);
    frmNasumicniBroj.getContentPane().add(lblGornjaGranica);

    textField_1 = new JTextField();
    textField_1.setColumns(10);
    textField_1.setBounds(200, 33, 154, 20);
    frmNasumicniBroj.getContentPane().add(textField_1);

    lblNasumicniBroj = new JLabel("Nasumicni broj: ");
    lblNasumicniBroj.setForeground(Color.YELLOW);
    lblNasumicniBroj.setFont(new Font("Wide Latin", Font.PLAIN, 12));
    lblNasumicniBroj.setBounds(10, 144, 180, 14);
    frmNasumicniBroj.getContentPane().add(lblNasumicniBroj);

    textField_2 = new JTextField();
    textField_2.setEditable(false);
    textField_2.setBounds(200, 141, 154, 20);
    frmNasumicniBroj.getContentPane().add(textField_2);
    textField_2.setColumns(10);

    JButton btnNewButton = new JButton("GENERISI");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            try{
                min = Integer.parseInt(textField.getText());
                max = Integer.parseInt(textField_1.getText());
                glavnaKlasa glKlasa = new glavnaKlasa();
                String brString = Integer.toString(glKlasa.broj);
                textField_2.setText(brString);
            }catch (Exception e){
                JOptionPane.showMessageDialog(null, e);
            }
        }
    });
    btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 14));
    btnNewButton.setBounds(10, 61, 345, 72);
    frmNasumicniBroj.getContentPane().add(btnNewButton);

    JLabel lblVerzija = new JLabel("Verzija: 1.0");
    lblVerzija.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 11));
    lblVerzija.setBounds(10, 180, 71, 14);
    frmNasumicniBroj.getContentPane().add(lblVerzija);

    JLabel label_1 = new JLabel("Autor: Djordje Milanovic");
    label_1.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 11));
    label_1.setBounds(10, 205, 141, 14);
    frmNasumicniBroj.getContentPane().add(label_1);

    JLabel label_2 = new JLabel("Copyright: Alfin Informatics");
    label_2.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 11));
    label_2.setBounds(9, 236, 166, 14);
    frmNasumicniBroj.getContentPane().add(label_2);
}}

import java.util.Random;
import javax.swing.JOptionPane;

public class generisanje {
public static int glavno(int min, int max){
    if (min >= max) {
        JOptionPane.showMessageDialog(null, "Maksimalni broj mora biti veci od minimalnog");
    }
    Random rnd = new Random();
    return rnd.nextInt((max - min) + 1) + min;
}
}

1 个答案:

答案 0 :(得分:0)

此:

  glavnaKlasa glKlasa = new glavnaKlasa();

使用空构造函数创建对象,您的main将不会在那里运行,因此broj仍然未初始化。

实际上,你不需要在那里实例化这个类,只需要这样:

public void actionPerformed(ActionEvent arg0) {
            try{
                min = Integer.parseInt(textField.getText());
                max = Integer.parseInt(textField_1.getText());                  
                int broj = generisanje.glavno(guiObject.min, guiObject.max);
                String brString = Integer.toString(broj);
                textField_2.setText(brString);
            }catch (Exception e){
                JOptionPane.showMessageDialog(null, e);
            }
        }

另外考虑按照Java编码标准工作(类名首先是大写等等,维护代码会容易得多)。