如何设置按钮的大小? (摇摆)

时间:2017-07-11 11:38:24

标签: java swing user-interface layout-manager border-layout

我想用按钮填充右下角&#34; Equal&#34;在计算器中。 现在这个按钮只占这个空间的1/4。我试过修复它 setPreferedSize public class Calculator implements ActionListener{ JPanel windowContent; JTextField displayField; JButton[] button = new JButton[10]; JButton buttonPoint; JButton buttonEqual; JButton buttonPlus; JButton buttonMinus; JButton buttonMultiply; JButton buttonDivide; JButton buttonClean; JPanel p1; JPanel p2; JPanel p3; int temp; double firstNum; double secondNum; String operation; ArrayList<String> mathOperation; Calculator(){ temp = 0; windowContent= new JPanel(); BorderLayout bl= new BorderLayout(); windowContent.setLayout(bl); displayField = new JTextField(30); displayField.setText(""); windowContent.add("North", displayField); for(int i = 0; i < 10; i++) { button[i] = new JButton(""+i); button[i].addActionListener(this); } buttonPoint= new JButton("."); buttonEqual= new JButton("="); buttonEqual.setSize(80, 80); buttonPlus= new JButton("+"); buttonMinus= new JButton("-"); buttonMultiply= new JButton("*"); buttonDivide= new JButton("/"); buttonClean = new JButton("C"); buttonEqual.setSize(100,200); buttonPoint.addActionListener(this); buttonEqual.addActionListener(this); buttonPlus.addActionListener(this); buttonMinus.addActionListener(this); buttonMultiply.addActionListener(this); buttonDivide.addActionListener(this); buttonClean.addActionListener(this); p1=new JPanel(); GridLayout gl= new GridLayout(4,3); p1.setLayout(gl); for(int i = 1; i <= 10; i++) { if(i==10) p1.add(button[0]); else p1.add(button[i]); } p1.add(buttonPoint); p1.add(buttonClean); windowContent.add("Center",p1); p2= new JPanel(); p2.setLayout(gl); p2.add(buttonMinus); p2.add(buttonPlus); p2.add(buttonDivide); p2.add(buttonMultiply); p2.add(buttonEqual); windowContent.add("East",p2); JFrame frame= new JFrame("Calculator"); frame.setContentPane(windowContent); frame.pack(); frame.setVisible(true); } public static void main(String[] args){ Calculator calc= new Calculator(); } public void actionPerformed(ActionEvent e) { if(e.getSource() == button[0]) displayField.setText(displayField.getText() + "0"); if(e.getSource() == button[1]) displayField.setText(displayField.getText() + "1"); if(e.getSource() == button[2]) displayField.setText(displayField.getText() + "2"); if(e.getSource() == button[3]) displayField.setText(displayField.getText() + "3"); if(e.getSource() == button[4]) displayField.setText(displayField.getText() + "4"); if(e.getSource() == button[5]) displayField.setText(displayField.getText() + "5"); if(e.getSource() == button[6]) displayField.setText(displayField.getText() + "6"); if(e.getSource() == button[7]) displayField.setText(displayField.getText() + "7"); if(e.getSource() == button[8]) displayField.setText(displayField.getText() + "8"); if(e.getSource() == button[9]) displayField.setText(displayField.getText() + "9"); if(e.getSource() == buttonPlus) { firstNum = Double.parseDouble(displayField.getText()); displayField.setText(""); operation = "+"; } if(e.getSource() == buttonMinus) { firstNum = Double.parseDouble(displayField.getText()); displayField.setText(""); operation = "-"; } if(e.getSource() == buttonMultiply) { firstNum = Double.parseDouble(displayField.getText()); displayField.setText(""); operation = "*"; } if(e.getSource() == buttonDivide) { firstNum = Double.parseDouble(displayField.getText()); displayField.setText(""); operation = "/"; } if(e.getSource() == buttonEqual) { secondNum = Double.parseDouble(displayField.getText()); if(operation == "+") displayField.setText(firstNum+secondNum+""); if(operation == "-") displayField.setText(firstNum-secondNum+""); if(operation == "*") displayField.setText(firstNum*secondNum+""); if(operation == "/") displayField.setText(firstNum/secondNum+""); } if(e.getSource() == buttonClean) { String tmp = displayField.getText(); String temp = tmp.substring(0, tmp.length() -1); displayField.setText(temp); } } public double result(ArrayList s) { double res =0; for(int i =0; i< s.size(); i++) { if(s.get(i).equals("+")) { res = Double.parseDouble(mathOperation.get(i-1)) + Double.parseDouble(mathOperation.get(i+1)); } } return res; } } ,但所有尝试都失败了。我真的 不熟悉Swing。

我该怎么办?我应该调整什么?

&.!=

3 个答案:

答案 0 :(得分:2)

此:

enter image description here

可以这样实现..

    windowContent.add(p1, BorderLayout.CENTER);
    p2 = new JPanel();
    p2.setLayout(new GridLayout(0,2));
    p2.add(buttonMinus);
    p2.add(buttonPlus);
    p2.add(buttonDivide);
    p2.add(buttonMultiply);
    //p2.add(buttonEqual);  // Oops.. slight correction here!

    //windowContent.add("East", p2);

    JPanel pEast = new JPanel(new GridLayout(0, 1));
    windowContent.add(pEast, BorderLayout.EAST);
    pEast.add(p2);
    pEast.add(buttonEqual);

请注意,它也可以通过其他各种方式实现,包括使用单个GridBagLayout

另见calculator example。它使用ScriptEngine来评估文本字段中的表达式。

答案 1 :(得分:1)

GridLayout不支持此功能。您必须使用其他布局管理器。标准库中有GridBagLayout,但它并不容易理解。

我建议MigLayout。我没有长时间使用UI,但请记住它非常简单并允许创建复杂的UI。

答案 2 :(得分:0)

我们必须使用Gridbag layout

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JFrame;

public class TestFrame extends JFrame {
    public TestFrame() {
        GridBagLayout gridBagLayout = new GridBagLayout();
        getContentPane().setLayout(gridBagLayout);

        JButton btn1 = new JButton("1");
        GridBagConstraints gbc_btn1 = new GridBagConstraints();
        gbc_btn1.insets = new Insets(0, 0, 5, 5);
        gbc_btn1.gridx = 0;
        gbc_btn1.gridy = 0;
        getContentPane().add(btn1, gbc_btn1);

        JButton btn2 = new JButton("2");
        GridBagConstraints gbc_btn2 = new GridBagConstraints();
        gbc_btn2.insets = new Insets(0, 0, 5, 0);
        gbc_btn2.gridx = 1;
        gbc_btn2.gridy = 0;
        getContentPane().add(btn2, gbc_btn2);

        JButton btn3 = new JButton("3");
        GridBagConstraints gbc_btn3 = new GridBagConstraints();
        gbc_btn3.gridwidth = 2;
        gbc_btn3.fill = GridBagConstraints.HORIZONTAL;
        gbc_btn3.insets = new Insets(0, 0, 0, 5);
        gbc_btn3.gridx = 0;
        gbc_btn3.gridy = 1;
        getContentPane().add(btn3, gbc_btn3);
    }

}

作为附加信息,如果您使用eclipse,请尝试window builder进行GUI创建。