java - 如何消除 BorderLayout.CENTER 占用的空隙?

标签 java swing

这可能是一个愚蠢的问题,可以通过更改我为此使用的布局来解决,但是,在运行我的计算器程序时,我很聪明,直到我发现我的文本区域大小不会拉伸(stretch)以适应它所在的 Pane 的大小,而无需我在第一次创建它时设置大小,而且,我该如何消除 BorderLayout.CENTER 在我的按钮面板之间占据的巨大差距?

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

public class calculatorProg extends JFrame implements ActionListener
{
    private Container contents;
    private JButton add, subtr, mult, div, pow, pow10, b1, b2, b3, b4, b5, b6, b7, b8, b9, b0;
    private JTextArea win;
    private JPanel numArea, win1, arithArea, numPlusArith;
    private JMenu men;
    private JMenuItem menItem;
    JSplitPane calcPane;

    public calculatorProg()
    {
        super("Calculator");
        contents = getContentPane();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        contents.setLayout(new BorderLayout());

        //Arithmetic buttons
        add = new JButton("+");
        subtr = new JButton("-");
        mult = new JButton("x");
        div = new JButton("/");
        pow = new JButton("x^");
        pow10 = new JButton("x^10");

        //Buttons
        b1 = new JButton("1");
        b5 = new JButton("5");
        b2 = new JButton("2");
        b6 = new JButton("6");
        b3 = new JButton("3");
        b7 = new JButton("7");
        b4 = new JButton("4");
        b8 = new JButton("8");
        b9 = new JButton("9");
        b0 = new JButton("0");

        //Creating the panels
        win = new JTextArea(2, 25);
        win.setEditable(false);
        win.setVisible(true);
        win1 = new JPanel();
        win1.setLayout(new FlowLayout(FlowLayout.LEFT));
        win1.add(win);
        numArea = new JPanel(new GridLayout(0, 3));
        arithArea = new JPanel(new GridLayout(3, 3));
        arithArea.setSize(new Dimension(8, 8));
        numPlusArith = new JPanel(new BorderLayout());

        //Adding to num area
        numArea.add(b1);
        numArea.add(b2);
        numArea.add(b3);
        numArea.add(b4);
        numArea.add(b5);
        numArea.add(b6);
        numArea.add(b7);
        numArea.add(b8);
        numArea.add(b9);
        numArea.add(b0);
        arithArea.add(add);
        arithArea.add(subtr);
        arithArea.add(div);
        arithArea.add(mult);
        arithArea.add(pow);
        arithArea.add(pow10);
        numPlusArith.add(arithArea, BorderLayout.LINE_START);
        numPlusArith.add(numArea, BorderLayout.LINE_END);

        //Lay out the panes
        calcPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, win1, numPlusArith);
        calcPane.setOneTouchExpandable(true);
        calcPane.setDividerLocation(50);

        Dimension minimumSize = new Dimension(100, 50);
        win1.setMinimumSize(minimumSize);
        numArea.setMinimumSize(minimumSize);

        //add to contents
        contents.add(calcPane, BorderLayout.CENTER);

        setSize(320, 370);
        setVisible(true);
    }

    public static void main(String[] args)
    {
        calculatorProg cp = new calculatorProg();
    }
}

有人可能会提到 0 在 numArea 中偏离中心(我在 Java 文档中阅读了关于 GridBagLayout 的内容,但我什至连一半的脚趾甲都没有插入其中水域,但是。)

编辑代码:

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

class calculatorProg extends JFrame implements ActionListener {
private Container contents;
private JButton add, subtr, mult, div, pow, pow10, clr, plmi, b1, b2, b3, b4, b5,   b6, b7, b8, b9, b0;
private JTextArea win;
private JPanel numArea, win1, arithArea, numPlusArith;
private JMenu men;
private JMenuItem menItem;
JSplitPane calcPane;

public calculatorProg(){
    super("Calculator");
contents = getContentPane();
setDefaultCloseOperation(EXIT_ON_CLOSE);
contents.setLayout(new BorderLayout());

//Arithmetic buttons
add = new JButton("+");
subtr = new JButton("-");
mult = new JButton("x");
div = new JButton("/");
pow = new JButton("x^");
pow10 = new JButton("x^10");


//Buttons
b1 = new JButton("1"); b5 = new JButton("5");
b2 = new JButton("2"); b6 = new JButton("6");   
b3 = new JButton("3"); b7 = new JButton("7");
b4 = new JButton("4"); b8 = new JButton("8");
b9 = new JButton("9"); b0 = new JButton("0");
clr = new JButton("C"); plmi = new JButton("+/-");


//Creating the panels
win = new JTextArea(2, 25);
win.setLineWrap(true);
win.setWrapStyleWord(true);
win.setEditable(false);
win.setVisible(true);
win1 = new JPanel();
win1.setLayout(new BorderLayout());
win1.add(new JScrollPane(win), BorderLayout.CENTER);
numArea = new JPanel(new GridLayout(0,3));
arithArea = new JPanel(new GridLayout(3, 3));
//arithArea.setSize(new Dimension(8, 8));
numPlusArith = new JPanel(new GridLayout(0, 2, 0, 0));

//Adding to num area
numArea.add(b1); numArea.add(b2);
numArea.add(b3); numArea.add(b4);
numArea.add(b5); numArea.add(b6);
numArea.add(b7); numArea.add(b8);
numArea.add(b9); numArea.add(b0); 
numArea.add(clr); numArea.add(plmi);
arithArea.add(add);
arithArea.add(subtr);
arithArea.add(div);
arithArea.add(mult);
arithArea.add(pow);
arithArea.add(pow10);
numPlusArith.add(arithArea);
numPlusArith.add(numArea);

//Lay out the panes
calcPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, win1, numPlusArith);
calcPane.setOneTouchExpandable(true);
calcPane.setDividerLocation(50);

Dimension minimumSize = new Dimension(100, 50);
win1.setMinimumSize(minimumSize);
numArea.setMinimumSize(minimumSize);

//add to contents
contents.add(calcPane, BorderLayout.CENTER);

pack();
setVisible(true);

}
public static void main(String[] args) {
    calculatorProg cp = new calculatorProg();

}

private double addition(double a, double b){
    a=0.0; b=0.0;
    double finAnswer = a + b;

    return finAnswer;
}
private double subtraction(double a, double b){
    a=0.0; b=0.0;
    double finAnswer = a + b;

    return finAnswer;
}
private double division(double a, double b){
    a=0.0; b=0.0;
    double finAnswer = a / b;

    return finAnswer;
}
private double multiplication(double a, double b){
    a=0.0; b=0.0;
    double finAnswer = a * b;
    return finAnswer;
}
private double pow(double a, double b){
    a=0.0; b=0.0;
    double finAnswer = Math.pow(a, b);
    return finAnswer;
}
private double pow10(double a){
    a = 0.0;
    double finAnswer = Math.pow(a, 10);

    return finAnswer;
}
@Override
public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub

}

}

如您所见,我进行了必要的更改。看起来干净多了。数字区域周围仍然存在微小差距,但它更干净了。 pack() 肯定会有所作为。感谢大家的帮助!

最佳答案

感谢 SSCCE,为此 +1。所缺少的只是 actionPerformed 方法(在最近的编辑之前,您还缺少最后一个 })。

无论如何,您的问题是您正在为包含您的JTextArea 的面板使用FlowLayout。更改这些行:

win1.setLayout(new FlowLayout(FlowLayout.LEFT));
win1.add(win);

为此:

win1.setLayout(new BorderLayout());
win1.add(new JScrollPane(win), BorderLayout.CENTER);

您可能还注意到我将文本区域包裹在滚动 Pane 中。这将使任何文本溢出都可以滚动。您还应该添加以下行以打开换行:

win = new JTextArea(2, 25);
win.setLineWrap(true);
win.setWrapStyleWord(true);

关于java - 如何消除 BorderLayout.CENTER 占用的空隙?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18810100/

相关文章:

java - 如何在RecyclerView Item中的populateViewHolder中设置addSnapshotListener和remove?

java - JAXB - 编码器写入系统输出但不写入 XML 文件

java - @Transactional 不适用于方法级别

java - 如何使用Optional调用类方法

java - showInputDialog 卡住其他窗口

java - 类-图形-drawString()

java - 无法启动组件 [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/JDBC_DBO]]

java - Swing 按钮使标签或文本字段发生变化

java - 如何制作 JScrollPane/JTextPane "Intelligent"

java - JTextFields 只接受数字的常用方法