最后一个按钮在Java中传播整个GUI

时间:2018-05-30 18:37:33

标签: java swing awt

我创建了一个Java类,其中包含一个小型计算器,用于执行加法和乘法功能,其中包含3个用于输入输出的文本字段和一些带按钮的标签。问题是,当我将所有项目放置到它们的x,y坐标并设置它们的宽度和高度时,最后一个按钮在GUI窗口的背景上在整个屏幕上展开。这是代码,请告诉我这个问题。感谢

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

public class SmallCalcApp implements ActionListener{

    JFrame frame;
    JLabel firstOperand, secondOperand, answer; 
    JTextField op1, op2, ans;
    JButton plus, mul;

    public SmallCalcApp(){
        initGUI();
    }

    public void initGUI(){

        frame = new JFrame();
        Container con = frame.getContentPane();


        //initialization of objects
        plus = new JButton("+");
        mul = new JButton("*");
        op1 = new JTextField();
        op2 = new JTextField();
        ans = new JTextField();
        firstOperand = new JLabel("First Number: ");
        secondOperand = new JLabel("Second Number: ");
        answer = new JLabel("Calculated Result: ");


        firstOperand.setBounds(0, 0, 150, 20);
        op1.setBounds(200, 0, 150, 20);
        secondOperand.setBounds(0, 200, 150, 20);
        op2.setBounds(200, 200, 150, 20);
        answer.setBounds(0, 300, 150, 20);
        ans.setBounds(300, 300, 150, 20);
        plus.setBounds(0, 400, 50, 50);
        mul.setBounds(200, 400, 50, 50);




        con.add(firstOperand);
        con.add(op1);
        con.add(secondOperand); 
        con.add(op2);   
        con.add(answer);    
        con.add(ans);
        con.add(plus);
        con.add(mul);



        plus.addActionListener(this);
        mul.addActionListener(this);



        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(200,200);
        frame.setVisible(true);
    }

    public void actionPerformed(ActionEvent event){
            String oper, result;
            int num1, num2, res;



            if(event.getSource() == plus){
                oper = op1.getText();
                num1 = Integer.parseInt(oper);

                oper = op2.getText();
                num2 = Integer.parseInt(oper);

                res = num1 + num2;
                result = res + "";

                ans.setText(result);
            }

            if(event.getSource() == mul){
                oper = op1.getText();
                num1 = Integer.parseInt(oper);

                oper = op2.getText();
                num2 = Integer.parseInt(oper);

                res = num1 * num2;
                result = res + "";

                ans.setText(result);
            }
        } 


    public static void main(String[] args){
        SmallCalcApp sc = new SmallCalcApp();
    }
}

2 个答案:

答案 0 :(得分:0)

对于任何挥杆应用程序布局是必须的。所以你必须设置" setLayout"对于任何容器。设置布局' null'或任何你想要的布局。如果你没有设置布局,每个组件将占用整个容器。对于Calculater类型应用程序" GridLayout"适合。要快速解决方案,您可以使用以下行。

    Container con = frame.getContentPane();
    con.setLayout(null);

答案 1 :(得分:0)

一般来说,你应该避免绝对定位。如果您需要明确指定对象的像素坐标,请选择GridBagLayout之类的布局管理器,这样可以直接控制网格大小和间距。

为了您的目的,我认为最有意义的是构建一个9x9网格,每个50个像素,然后指定每个对象,每个对象占用几个这样的框。

因此,在initGUI方法中,我们首先设置布局:

GridBagLayout layout;
public void initGUI() {
    /*...*/
    con.setLayout(layout = new GridBagLayout());
    layout.columnWidths = new int[9];
    Arrays.fill(layout.columnWidths, 50);
    layout.rowHeights = new int[9];
    Arrays.fill(layout.rowHeights, 50);

然后,在此之后,我们将为您的应用中显示的每个对象定义约束。这有点冗长,但很多都是样板代码。

//Label will be 150 (50*3) pixels wide, start at 0,0, and we'll add 30 pixels of padding below it.
GridBagConstraints firstOperandConstraints = new GridBagConstraints(
        0,0,
        3,1,
        1,1,
        GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH,
        new Insets(0, 0, 30, 0),
        0, 0
        );
//Text field will be 150 (50*3) pixels wide, start at 200(50*4),0, and have 30 pixels of padding below it
GridBagConstraints op1Constraints = new GridBagConstraints(
        4,0,
        3,1,
        1,1,
        GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH,
        new Insets(0, 0, 30, 0),
        0, 0
        );
//Label will be 150 (50*3) pixels wide, start at 0,200 (50*4), and we'll add 30 pixels of padding below it.
GridBagConstraints secondOperandConstraints = new GridBagConstraints(
        0,4,
        3,1,
        1,1,
        GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH,
        new Insets(0, 0, 30, 0),
        0, 0
        );
//Field will be 150 (50*3) pixels wide, start at 200,200 (50*4), and we'll add 30 pixels of padding below it.
GridBagConstraints op2Constraints = new GridBagConstraints(
        4,4,
        3,1,
        1,1,
        GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH,
        new Insets(0, 0, 30, 0),
        0, 0
        );
//Label will be 150 (50*3) pixels wide, start at 0,300 (50*6), and we'll add 30 pixels of padding below it.
GridBagConstraints answerConstraints = new GridBagConstraints(
        0,6,
        3,1,
        1,1,
        GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH,
        new Insets(0, 0, 30, 0),
        0, 0
        );
//Label will be 150 (50*3) pixels wide, start at 300,300 (50*6), and we'll add 30 pixels of padding below it.
GridBagConstraints ansConstraints = new GridBagConstraints(
        6,6,
        3,1,
        1,1,
        GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH,
        new Insets(0, 0, 30, 0),
        0, 0
        );
//Button will be 50 pixels wide/tall, start at 0,400 (50*8), and it won't have any padding
GridBagConstraints plusConstraints = new GridBagConstraints(
        0,8,
        1,1,
        1,1,
        GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH,
        new Insets(0, 0, 0, 0),
        0, 0
        );
//Button will be 50 pixels wide/tall, start at 200,400 (50*4/50*8), and it won't have any padding
GridBagConstraints mulConstraints = new GridBagConstraints(
        4,8,
        1,1,
        1,1,
        GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH,
        new Insets(0, 0, 0, 0),
        0, 0
        );

我们将在添加按钮/标签/字段时指定每个约束:

con.add(firstOperand, firstOperandConstraints);
con.add(op1, op1Constraints);
con.add(secondOperand, secondOperandConstraints); 
con.add(op2, op2Constraints);   
con.add(answer, answerConstraints);    
con.add(ans, ansConstraints);
con.add(plus, plusConstraints);
con.add(mul, mulConstraints);

最后,我们将使框架不可调整以确保事物不会移动:

frame.pack();
frame.setResizable(false);

这会导致应用看起来像这样:

App

添加了网格线(手动手动,所以它们有点不均匀),我们可以看到它如何划分屏幕:

App with Grid Lines

根据您的要求,您可能需要通过移动组件或调整大小来“美化”此应用程序。需要考虑的一些事项:

  • 您可以通过组合网格位置和插入来获得所需的任意位置。占据[230,170]-[290,320]的方框可以通过让对象从位置4,3开始,宽度/高度值为2,4,并指定Insets of (20, 30, 30, 10)来轻松表示。
  • 如果您希望在不改变组件位置/大小的情况下调整框架大小,则需要将这些框架捆绑到各自的面板中,并将该面板添加到根内容窗格中。

下面是一个示例,它允许在不移动按钮/字段/标签的情况下调整框架本身的大小,或者调整太小以显示这些组件:

JPanel panel = new JPanel();

panel.setLayout(layout = new GridBagLayout());
layout.columnWidths = new int[9];
Arrays.fill(layout.columnWidths, 50);
layout.rowHeights = new int[9];
Arrays.fill(layout.rowHeights, 50);

panel.add(firstOperand, firstOperandConstraints);
panel.add(op1, op1Constraints);
panel.add(secondOperand, secondOperandConstraints); 
panel.add(op2, op2Constraints);   
panel.add(answer, answerConstraints);    
panel.add(ans, ansConstraints);
panel.add(plus, plusConstraints);
panel.add(mul, mulConstraints);

GridBagLayout rootLayout = new GridBagLayout();
rootLayout.columnWidths = new int[]{450};
rootLayout.rowHeights = new int[]{450};
con.setLayout(rootLayout);
con.add(panel, new GridBagConstraints(0,0,1,1,1,1,GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0,0,0,0),0,0));

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setMinimumSize(null); //To make sure the pack() directive doesn't result in a frame larger than the components
frame.pack();
frame.setMinimumSize(frame.getSize());
frame.setVisible(true);