当我把它放在它自己的方法中时,Java gui没有显示出来

时间:2015-02-12 11:54:50

标签: java swing user-interface jframe

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

public class Calculator1{

    public void Calculator1(){
        JFrame frame = new JFrame("Calculator");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 500);

        GridLayout grid = new GridLayout(4, 3, 10, 10);
        frame.setLayout(grid);

        JLabel op1Label= new JLabel("Operand One:");
        JTextField operandOne = new JTextField();
        operandOne.setText("");
        operandOne.setEditable(true);
        frame.add(op1Label);
        frame.add(operandOne);

        JLabel op2Label = new JLabel("Operand Two:");
        JTextField operandTwo = new JTextField();
        operandTwo.setText("");
        operandTwo.setEditable(true);
        frame.add(op2Label);
        frame.add(operandTwo);

        JButton plus = new JButton("+");
        frame.add(plus);
        JButton minus = new JButton("-");
        frame.add(minus);

        JButton multiply = new JButton("*");
        frame.add(multiply);

        JButton divide = new JButton("/");
        frame.add(divide);

        JButton exponent = new JButton("^");
        frame.add(exponent);

        JButton route = new JButton("\u221A");
        frame.add(route);

        JButton increment = new JButton("Increment");
        frame.add(increment);

        JButton decrement = new JButton("Decrement");
        frame.add(decrement);

        JButton reciprocal = new JButton("Reciprocal");
        frame.add(reciprocal);

        JLabel resultLabel= new JLabel("Result:");
        JTextField result = new JTextField();
        result.setText("0");
        result.setEditable(false);
        frame.add(resultLabel);
        frame.add(result);
        frame.pack();

        frame.setVisible(true);
    }


    public static void main(String[] args) {
        Calculator1 calc = new Calculator1();
    }
}

我是Java的新手并试图编写计算器。在我得到要展示的东西之后我会做的动作听众,当它在主要部分时我让它们工作所以它不应该太难。它显示当我将所有代码放入main时但是当我尝试将其放入自己的方法时,程序编译并运行但除了cmd行之外没有任何显示。有什么帮助吗?

4 个答案:

答案 0 :(得分:3)

问题是您正在调用构造函数(使用new),但您的Calculator1方法只是一个恰好与类同名的方法,但不是< / em>该类的构造函数,因此它使用默认构造函数。要使其成为构造函数,请删除void返回值。

public Calculator1() { // without return value it's a constructor
    ...      
}

public static void main(String[] args) {
    Calculator1 calc = new Calculator1(); // call the constructor
}

答案 1 :(得分:1)

使用你的类的构造函数或使用calc.Calculator1()。

答案 2 :(得分:0)

更改public void Calculator1()---&gt; public static void Calculator1() 并在main()

中调用该函数
public class Calculator1{

public static void Calculator1(){
    JFrame frame = new JFrame("Calculator");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 500);

    GridLayout grid = new GridLayout(4, 3, 10, 10);
    frame.setLayout(grid);

        JLabel op1Label= new JLabel("Operand One:");
        JTextField operandOne = new JTextField();
        operandOne.setText("");
        operandOne.setEditable(true);
        frame.add(op1Label);
        frame.add(operandOne);

        JLabel op2Label = new JLabel("Operand Two:");
        JTextField operandTwo = new JTextField();
        operandTwo.setText("");
        operandTwo.setEditable(true);
        frame.add(op2Label);
        frame.add(operandTwo);

                JButton plus = new JButton("+");
                frame.add(plus);
                JButton minus = new JButton("-");
                frame.add(minus);

                JButton multiply = new JButton("*");
                frame.add(multiply);

                JButton divide = new JButton("/");
                frame.add(divide);

                JButton exponent = new JButton("^");
                frame.add(exponent);

                JButton route = new JButton("\u221A");
                frame.add(route);

                JButton increment = new JButton("Increment");
                frame.add(increment);

                JButton decrement = new JButton("Decrement");
                frame.add(decrement);

                JButton reciprocal = new JButton("Reciprocal");
                frame.add(reciprocal);

                JLabel resultLabel= new JLabel("Result:");
                JTextField result = new JTextField();
                result.setText("0");
                result.setEditable(false);
                frame.add(resultLabel);
                frame.add(result);
                frame.pack();

                frame.setVisible(true);
 }

 public static void main(String[] args) {
     Calculator1 calc = new Calculator1();
     Calculator1();
 }
 }

答案 3 :(得分:0)

更改public void Calculator1(){ /* rest of the code */ }

public Calculator1(){ /* rest of the code */ }