运行

时间:2016-09-14 21:27:55

标签: java eclipse jframe

我正在使用JFrame计算器,我不熟悉所以我正在学习一个教程,并且我已经多次检查了几次。 它不起作用,它不会抛出任何错误或任何东西,它将编译并运行。什么都没弹出来。 我正在使用Eclipse,它正在发出两个警告, 一个关于我的构造函数没有在我的主要使用 我的类说的是:可序列化的类Gui没有声明类型为long的静态最终serialVersionUID字段。

我不确定为什么会出现,但我的代码似乎就像教程一样。

先谢谢你。

聚苯乙烯。原谅所有公地,我只是确保我可以回顾它而不必再回到教程。

// 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();
    }
}

1 个答案:

答案 0 :(得分:1)

你应该在你的主要方法中真正拥有这个

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