java swing布局创建简单计算器的UI

时间:2014-06-12 16:12:26

标签: java swing layout grid-layout border-layout

我正在尝试创建一个简单的计算器。我即将完成其UI,但我在使用布局时遇到了问题。我试图找到一种方法,在面板2的正下方显示面板3,而不是左边,但我不能。 (这是学术用途)

所以,这是MyFrame类。

package lab5;
import java.awt.*;
    import javax.swing.*;

public class MyFrame extends JFrame {


    public MyFrame(){

        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();
        JPanel panel3 = new JPanel();
        JPanel panel4 = new JPanel();

        JTextArea txt = new JTextArea(3,28);


        //Frame
        this.setSize(320, 375);
        this.setTitle("Calculator by Liath");
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setResizable(false);
        this.validate();



        //TextArea
        txt.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        panel1.add(txt);

        //Panels
        this.add(panel1, BorderLayout.PAGE_START);
        this.add(panel2, BorderLayout.EAST);
        this.add(panel3, BorderLayout.CENTER);
        this.add(panel4,BorderLayout.SOUTH);
        panel3.setLayout(new GridLayout(3, 4));

        //Buttons
        JButton C = new JButton("C");
        JButton Slash = new JButton("/");
        JButton seven = new JButton("7");
        JButton eight = new JButton("8");
        JButton four = new JButton("4");
        JButton one = new JButton("1");
        JButton five = new JButton("5");
        JButton two = new JButton("2");
        JButton nine = new JButton("9");
        JButton six = new JButton("6");
        JButton three = new JButton("3");
        JButton x = new JButton("*");
        JButton minus = new JButton("-");
        JButton plus = new JButton("+");
        JButton zero = new JButton("0");
        JButton point = new JButton(".");
        JButton equal = new JButton("=");




        panel2.add(C);
        panel2.add(Slash);
        panel3.add(seven);
        panel3.add(eight);
        panel3.add(nine);
        panel3.add(x);
        panel3.add(four);
        panel3.add(five);
        panel3.add(six);   
        panel3.add(minus);
        panel3.add(one);
        panel3.add(two);
        panel3.add(three);

        panel3.add(plus);
        panel4.add(zero);
        panel4.add(point);
        panel4.add(equal); 

    }
}

这是我的主要课程

package main;


public class Main {


        public static void main(String[] args) {
            MyFrame frame= new MyFrame();

        }

    }

0 个答案:

没有答案