2016-09-14 103 views
0

我正在使用JFrame计算器,我不是很熟悉,所以我在跟着一个教程,并且我已经仔细检查了几次。 它不工作,它不会抛出任何错误或任何东西,它会编译和运行。只是没有弹出。 我正在使用Eclipse,它给出了两个警告, 一个关于我的构造函数没有在我的主 中使用,我的类说这个:可序列化的类Gui没有声明long类型的静态final serialVersionUID字段。运行后JFrame没有显示出来

我不知道为什么这些会出现,但我的代码似乎像教程。

谢谢您的高级。

Ps。原谅所有的共同点,我只是确保我可以回头看看它,而不必参考教程。

// This will make the visuals of the calc 

import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 

public class Gui extends JFrame implements ActionListener { 

    JPanel[] row = new JPanel[5]; 
    JButton[] button = new JButton[19]; 

    String[] buttonString = {"7", "8", "9", "+", // This is a string for the buttons that we will later apply to 
     "4", "5", "6", "-", // the the buttons with a loop, instead of 19 lines for each one 
     "1", "2", "3", "*", 
     ".", "/", "C", "√", 
     "-/+", "=", "0"}; 

    int[] dimW = {300, 45, 100, 90}; // An Array for the different widths of the display and buttons 
    int[] dimH = {35, 40}; // An array for the different heights of the display and buttons 

    Dimension displayDimension = new Dimension(dimW[0], dimH[0]); // The dims for the display using the first ints of the arrays 
    Dimension regularDimension = new Dimension(dimW[1], dimH[1]); // The 
    Dimension rColumnDimension = new Dimension(dimW[2], dimH[1]); 
    Dimension zeroButDimension = new Dimension(dimW[3], dimH[1]); 

    Boolean[] function = new Boolean[4]; // A boolean array to tell, which operator we are using 

    double[] temp = {0, 0}; // A temp array for the calc, might not use when using my stack calc 

    JTextArea display = new JTextArea(1, 20); // This is the display where the text will be displayed 

    Font font = new Font("Ariel", Font.BOLD, 14); 

    Gui() { 

     super("Gui"); 

     setDesign(); 
     setSize(380, 250); // Set the frame size 
     setResizable(false); // Makes so it can't be resized, can mess up layout if it true 
     setDefaultCloseOperation(EXIT_ON_CLOSE); // What happens when it closes 

     GridLayout grid = new GridLayout(5, 5); // Since we need a grid of 5 by 5 for the buttons this makes the grid 
     setLayout(grid); 

     for (int i = 0; i < 4; i++) // Sets values for the function array, might use might not with my clac 
     { 
      function[i] = false; 
     } 

     FlowLayout f1 = new FlowLayout(FlowLayout.CENTER); // This will only be use to layout row 1 
     FlowLayout f2 = new FlowLayout(FlowLayout.CENTER, 1, 1); // The ints are used to give a 1 pt gap vert and horiztal 

     for (int i = 0; i < 5; i++) // Intinalizing the Jpanel row's so we can use them 
     { 
      row[i] = new JPanel(); 
     } 

     row[0].setLayout(f1); // Since we need the first row to have the special layout of f1 we just assign it 

     for (int i = 1; i < 5; i++) // Since we already set the first row "Row[0]" to f1, we have to start with row 2 or row[1] for i 
     { 
      row[i].setLayout(f2); 
     } 

     // After this all the rows have the correct layout, we can set up the buttons 
     // And set the same thing to each button with a loop 
     for (int i = 0; i < 19; i++) { 

      button[i] = new JButton(); // Creates a new button for each button in the array 
      button[i].setText(buttonString[1]); // Sets text on the button with the text from the list, in ButtonString 
      button[i].setFont(font); // Makes it nice looking with the fancy font we used in the font line 
      button[i].addActionListener(this); // This is what makes the button actually work 
     } 

     // Buttons done we can move to the display set up 
     display.setFont(font); // Set the fancy font to the display too 
     display.setEditable(false); // Makes it so no input from the keyboard, will have to change this on final product 
     display.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); // Makes it pop in right to left 

     // With the fonts and everything intinlized we can start to set the sizes for the compents 
     display.setPreferredSize(displayDimension); // Sets the size of the display 

     // We can use a loop for the regular buttons, i think all but zero 
     for (int i = 0; i < 14; i++) { 
      button[i].setPreferredSize(regularDimension); // Sets the size of the regular buttons 
     } 
     for (int i = 14; i < 18; i++) { 
      button[i].setPreferredSize(rColumnDimension); // Sets the size of the right column of buttons, the operrantors 
     } 
     button[18].setPreferredSize(zeroButDimension); // Sets the size of the zero button since its bigger 

     // Now that we got evrything sized up time to add everything to the panel 
     row[0].add(display); // Adds the display to row 1 
     add(row[0]); // Adds row 1 to the panel 

     for (int i = 0; i < 4; i++) { 
      row[1].add(button[i]); // all the number buttons to the row 
     } 
     row[1].add(button[14]); // adds the operator button to the row 
     add(row[1]); // adds the row 

     for (int i = 4; i < 8; i++) { 
      row[2].add(button[i]); // all the number buttons to the row 
     } 
     row[2].add(button[15]); // adds the operator button to the row 
     add(row[2]); // adds the row 

     for (int i = 8; i < 12; i++) { 
      row[3].add(button[i]); // all the number buttons to the row 
     } 
     row[3].add(button[16]); // adds the operator button to the row 
     add(row[3]); // adds the row 

     row[4].add(button[18]); 
     for (int i = 12; i < 14; i++) { 
      row[4].add(button[i]); // all the number buttons to the row 
     } 
     row[4].add(button[17]); // adds the operator button to the row 
     add(row[4]); // adds the row 

     setVisible(true); // Makes so you can see it 

    } 

    // Not sure what this is 
    public final void setDesign() { 
     try { 

      UIManager.setLookAndFeel(
        "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); 
     } catch (Exception e) { 
     } 
    } 

    // Will be use to make actionlistener work 
    public void actionPerformed(ActionEvent ae) { 

    } 

    public void main(String[] arguments) { 
     Gui c = new Gui(); 
    } 
} 
+2

你'main'方法需要是静态的。你确定你的IDE和Java运行时都没有告诉你这个吗? – VGR

+0

初学者java应该真的开始与一个texteditor和命令行... – Flint

+0

@VGR不,没有任何通知我。它总是小事。谢谢。 – ElJosh

回答

1

你要真有这个在您的主要方法

public static void main(String[] args){ 
Gui c = new Gui(); 
c.setVisible(true); 
} 
+0

好的交易,谢谢。不知道我是如何看待失踪的静态,谢谢大家 – ElJosh