2015-11-08 58 views
-3

我在Java中製作了軟件,可以生成範圍內的隨機數。但它不起作用。對不起,奇怪的語言和評論。我有三個類:main,用於生成和GUI。當我點擊按鈕生成它在屏幕上顯示0。請幫幫我。這個Java隨機軟件代碼有什麼問題?

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

你傳遞給'glavno'方法的值是什麼? – Eran

+0

我將textField和textField_1文本傳遞給整型主類,並在主類中將它們作爲glavno方法中的min和max傳遞。 –

+0

如果需要知道,我使用WindowBuilder製作了GUI。 –

回答

0

此:

glavnaKlasa glKlasa = new glavnaKlasa(); 

創建了空構造的對象,你的主要將無法運行在那裏,所以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編碼標準(類名首大寫等,這將是更容易維護代碼)。

+0

那麼,我需要做什麼? –

+0

查看更新的答案。 –

+0

謝謝,現在它工作。 –