Java网格布局

时间:2013-11-16 20:18:37

标签: java swing layout jpanel jbutton

我正在创建一个计算器,我希望我的JTextField显示在顶部,旁边没有其他按钮,就像在普通计算器中看到的那样。

http://i.stack.imgur.com/cS6zN.png

这是我的代码:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Calculator {

// oh boy.. lots of declarations here
JLabel current = new JLabel();
static JPanel panel = new JPanel();
JButton add = new JButton("+");
JButton subtract = new JButton("-");
JButton multiply = new JButton("x");
JButton divide = new JButton("/");
JButton solve = new JButton("=");
JButton number1 = new JButton("1");
JButton number2 = new JButton("2");
JButton number3 = new JButton("3");
JButton number4 = new JButton("4");
JButton number5 = new JButton("5");
JButton number6 = new JButton("6");
JButton number7 = new JButton("7");
JButton number8 = new JButton("8");
JButton number9 = new JButton("9");
JButton number0 = new JButton("0");
JButton dot = new JButton(".");
JTextField solution = new JTextField(10);
boolean addboolean = false;
boolean subtractboolean = false;
boolean multiplyboolean = false;
boolean divideboolean = false;

// just some variables for our dimensions
int width = 150;
int height = 150;

public Calculator() {
    // house keeping... initialization mostly
    panel.setSize(width, height);

    GridLayout grid = new GridLayout(5, 1);
    panel.setLayout(grid);

    solution.setEditable(false);

    // add components to panel

    panel.add(solution, BorderLayout.NORTH);
    panel.add(number1);
    panel.add(number2);
    panel.add(number3);
    panel.add(add);
    panel.add(number4);
    panel.add(number5);
    panel.add(number6);
    panel.add(subtract);
    panel.add(number7);
    panel.add(number8);
    panel.add(number9);
    panel.add(multiply);
    panel.add(number0);
    panel.add(dot);
    panel.add(divide);
    panel.add(solve);

    // button listeners, they set our booleans to true on button click and
    // set the symbol
    add.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            addboolean = true;
            current.setText("+");
        }
    });

    subtract.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            subtractboolean = true;
            current.setText("-");
        }
    });

    multiply.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            multiplyboolean = true;
            current.setText("x");
        }
    });

    divide.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            divideboolean = true;
            current.setText("/");
        }
    });

    // listener for solve button which just parses our numbers and submits
    // it to the solve method
    solve.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            // double num1 = Double.parseDouble(firstNum.getText());
            // double num2 = Double.parseDouble(secondNum.getText());
            // solve(num1, num2);
        }
    });

}

// solves the equation
public void solve(double num1, double num2) { // takes 2 parameters (the
                                                    // first number and second
                                                // number)
    double result = 0; // initialize our result

    // checks to see which type of equation we will be solving, then solves
    // it
    if (addboolean) {
        result = num1 + num2;
    } else if (subtractboolean) {
        result = num1 - num2;
    } else if (multiplyboolean) {
        result = num1 * num2;
    } else if (divideboolean) {
        result = (num1) / (num2);
    }

    // prints out final text
    solution.setText("The final result is: " + result);

    // sets our booleans back to true so they can be reused during the same
    // runtime
    addboolean = false;
    subtractboolean = false;
    multiplyboolean = false;
    divideboolean = false;
}

// runs the program
public static void main(String[] args) {
    new Calculator();
}

}

3 个答案:

答案 0 :(得分:4)

DYM喜欢这个吗?

enter image description here

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

public class Calculator {

// oh boy.. lots of declarations here
JLabel current = new JLabel();
// static is usually BAD NEWS & rarely necessary for GUI elements.
//static JPanel panel = new JPanel();
JPanel panel = new JPanel(new GridLayout(0,4,3,3));
JPanel gui = new JPanel(new BorderLayout(3,3));

JButton add = new JButton("+");
JButton subtract = new JButton("-");
JButton multiply = new JButton("x");
JButton divide = new JButton("/");
JButton solve = new JButton("=");
JButton number1 = new JButton("1");
JButton number2 = new JButton("2");
JButton number3 = new JButton("3");
JButton number4 = new JButton("4");
JButton number5 = new JButton("5");
JButton number6 = new JButton("6");
JButton number7 = new JButton("7");
JButton number8 = new JButton("8");
JButton number9 = new JButton("9");
JButton number0 = new JButton("0");
JButton dot = new JButton(".");
JTextField solution = new JTextField(10);
boolean addboolean = false;
boolean subtractboolean = false;
boolean multiplyboolean = false;
boolean divideboolean = false;

// just some variables for our dimensions
int width = 150;
int height = 150;

public Calculator() {
    // house keeping... initialization mostly
    panel.setSize(width, height);

    solution.setEditable(false);

    // add components to panel

    gui.add(solution, BorderLayout.PAGE_START);
    gui.add(panel, BorderLayout.CENTER);
    panel.add(number1);
    panel.add(number2);
    panel.add(number3);
    panel.add(add);
    panel.add(number4);
    panel.add(number5);
    panel.add(number6);
    panel.add(subtract);
    panel.add(number7);
    panel.add(number8);
    panel.add(number9);
    panel.add(multiply);
    panel.add(number0);
    panel.add(dot);
    panel.add(divide);
    panel.add(solve);

    // button listeners, they set our booleans to true on button click and
    // set the symbol
    add.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            addboolean = true;
            current.setText("+");
        }
    });

    subtract.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            subtractboolean = true;
            current.setText("-");
        }
    });

    multiply.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            multiplyboolean = true;
            current.setText("x");
        }
    });

    divide.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            divideboolean = true;
            current.setText("/");
        }
    });

    // listener for solve button which just parses our numbers and submits
    // it to the solve method
    solve.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            // double num1 = Double.parseDouble(firstNum.getText());
            // double num2 = Double.parseDouble(secondNum.getText());
            // solve(num1, num2);
        }
    });

}

// solves the equation
public void solve(double num1, double num2) { // takes 2 parameters (the
                                                    // first number and second
                                                // number)
    double result = 0; // initialize our result

    // checks to see which type of equation we will be solving, then solves
    // it
    if (addboolean) {
        result = num1 + num2;
    } else if (subtractboolean) {
        result = num1 - num2;
    } else if (multiplyboolean) {
        result = num1 * num2;
    } else if (divideboolean) {
        result = (num1) / (num2);
    }

    // prints out final text
    solution.setText("The final result is: " + result);

    // sets our booleans back to true so they can be reused during the same
    // runtime
    addboolean = false;
    subtractboolean = false;
    multiplyboolean = false;
    divideboolean = false;
}

// runs the program
public static void main(String[] args) {
    Calculator c = new Calculator();
    JOptionPane.showMessageDialog(null, c.gui);
}

}

答案 1 :(得分:3)

我建议您使用包含BorderLayout

的主面板
JPanel masterPanel = new JPanel(new BorderLayout());

添加解决方案:

masterPanel.add(solution, BorderLayout.NORTH);

以及之后带有键的GridLayout面板:

masterPanel.add(panel, BorderLayout.CENTER);

其他布局管理器也可以正常工作(取决于您所需的设计),即:BoxLayout

N.B。:您的行panel.add(solution, BorderLayout.NORTH);对布局没有影响,因为您的panel使用了GridLayout

答案 2 :(得分:1)

请注意,这不是我的头脑,但是 这应该工作。只是不要将面板布局设置为网格,而是执行以下操作。

Gridlayout mainlayout= new GridLayout(1,2);
GridLayout grid = new GridLayout(5, 1); //Probably need to change this,
//as this is creating a grid containing 5 columns and 1 row. If you are
//trying to make this like the picture you'll need at least a 4x4 grid.

//Important!! Add items to grid here instead of panel.
mainlayout.add(grid);
mainlayout.add(solution);
panel.setLayout(mainlayout);