2016-09-14 6 views
0

私はJFrameの電卓を使っています。私はよく慣れていないので、私はチュートリアルに従っていました。 それは動作していない、何かエラーや何かを投げていない、それはコンパイルして実行されます。ちょうど何もポップアップしない。 私はEclipseを使用しています。私のクラスでは、 私のコンストラクタが私のメインで使われていないという警告が2つあります。 私のクラスはこれを次のように言います:serializableクラスGuiはlong型のstatic 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

でこれを持っている必要があります。あなたのIDEもJavaランタイムもこれについてあなたに通知していませんか? – VGR

+0

Javaの初心者は、テキストエディタとコマンドラインから実際に始める必要があります。 – Flint

+0

@VGRいいえ、何も知らせません。それは常に小さなものです。ありがとうございました。 – ElJosh

答えて

1

あなたは本当にあなたの主な方法あなたの `main`メソッドが静的である必要が

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

良いお取引、ありがとうございます。不足している静的なものをどう見ているのかよくわからない – ElJosh

関連する問題