gpt4 book ai didi

java - JFrame布局管理

转载 作者:行者123 更新时间:2023-11-30 03:51:42 25 4
gpt4 key购买 nike

使用以下代码,我尝试为计算器框架创建布局。

我得到的输出似乎出现了严重错误,但我无法发现它。

输出:

enter image description here

我的问题是你能尝试编译这个并看看你是否得到与我相同的输出吗?

如果你这样做,在遇到此类问题时我应该检查什么。谢谢

代码:

package mycalculator;

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

public class MyCalculatorGUI extends JFrame {

//initialise all the components
JPanel mainPanel = new JPanel();
JPanel memPanel = new JPanel();
JPanel advFuncPanel = new JPanel();
JPanel wordFuncPanel = new JPanel();
JPanel numPanel = new JPanel();
JPanel basicFuncPanel = new JPanel();
JTextField txtDisplay = new JTextField(100);
JToggleButton mPlus = new JToggleButton("M+");
JButton mClear = new JButton("CM");
JButton mA = new JButton("A");
JButton mB = new JButton("B");
JButton mC = new JButton("C");
JButton mD = new JButton("D");
JButton aSq = new JButton("x"+ "\u00B2");
JButton aCu = new JButton("x" + "\u00B3");
JButton aPowa = new JButton("x" + "\u00AA");
JButton aInv = new JButton("x" + "\u00AF" + "\u00B9");
JButton aSqrt = new JButton("\u221A" + "x");
JButton aFact = new JButton("x!");
JButton btnSin = new JButton("sin(x)");
JButton btnCos = new JButton("cos(x)");
JButton btnTan = new JButton("tan(x)");
JButton btnaSin = new JButton("arcsin(x)");
JButton btnaCos = new JButton("arccos(x)");
JButton btnaTan = new JButton("arctan(x)");
JToggleButton hyp = new JToggleButton("hyp");
JButton btnAbs = new JButton("abs(x)");
JButton btnExp = new JButton("exp(x)");
JButton btnLog = new JButton("log(x)");
JButton btnLn = new JButton("ln(x)");
JButton btnLoga = new JButton("loga(x)");
JButton btn1 = new JButton("1");
JButton btn2 = new JButton("2");
JButton btn3 = new JButton("3");
JButton btn4 = new JButton("4");
JButton btn5 = new JButton("5");
JButton btn6 = new JButton("6");
JButton btn7 = new JButton("7");
JButton btn8 = new JButton("8");
JButton btn9 = new JButton("9");
JButton btn0 = new JButton("0");
JButton btnDot = new JButton(".");
JButton btnCl = new JButton("Cl");
JButton btnBs = new JButton("\u00AB");
JButton btnPlus = new JButton("+");
JButton btnMinus = new JButton("-");
JButton btnMult = new JButton("*");
JButton btnDiv = new JButton("\u00F7");
JButton btnEq = new JButton("=");

public static void main(String[] args) {

new MyCalculatorGUI();

}

public MyCalculatorGUI() {

// create the frame for the Calculator
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setTitle("Calculator");

// parent panel containes every other panel
mainPanel.setLayout(new GridBagLayout());
this.add(mainPanel);

// text display
txtDisplay.setEditable(false);
addComp(mainPanel, txtDisplay, 0, 0, 4, 1);

// --------------------------------------------------------------------------

// memory panel, flow layout, 6 buttons same size, spans gridwidth 2
memPanel.setLayout(new FlowLayout());

// add button to panel
memPanel.add(mPlus);
memPanel.add(mClear);
memPanel.add(mA);
memPanel.add(mB);
memPanel.add(mC);
memPanel.add(mD);

// add panel to main panel
addComp(mainPanel, memPanel, 0, 1, 2, 1);

// --------------------------------------------------------------------------

// advFuncPanel, flow layout, 6 buttons same size, spans gridwidth 2
advFuncPanel.setLayout(new FlowLayout());

// add button to panel
advFuncPanel.add(aSq);
advFuncPanel.add(aCu);
advFuncPanel.add(aPowa);
advFuncPanel.add(aInv);
advFuncPanel.add(aSqrt);
advFuncPanel.add(aFact);

// add panel to main panel
addComp(mainPanel, advFuncPanel, 0, 2, 2, 1);

// --------------------------------------------------------------------------

// wordFuncPanel, grid layout (3 rows, 4 columns), 12 buttons same size, spans gridwidth 2
wordFuncPanel.setLayout(new GridLayout(3, 4));

// add buttons to panel
wordFuncPanel.add(btnSin);
wordFuncPanel.add(btnaSin);
wordFuncPanel.add(hyp);
wordFuncPanel.add(btnLog);
wordFuncPanel.add(btnCos);
wordFuncPanel.add(btnaCos);
wordFuncPanel.add(btnAbs);
wordFuncPanel.add(btnLn);
wordFuncPanel.add(btnTan);
wordFuncPanel.add(btnaTan);
wordFuncPanel.add(btnExp);
wordFuncPanel.add(btnLoga);

// add panel to main panel
addComp(mainPanel, wordFuncPanel, 0, 3, 2, 1);

// --------------------------------------------------------------------------

// numPanel, gridbaglayout, spans gridheight 3
numPanel.setLayout(new GridBagLayout());

// add buttons to panel
addComp(numPanel, btn1, 0, 0, 1, 1);
addComp(numPanel, btn2, 1, 0, 1, 1);
addComp(numPanel, btn3, 2, 0, 1, 1);
addComp(numPanel, btn4, 0, 1, 1, 1);
addComp(numPanel, btn5, 1, 1, 1, 1);
addComp(numPanel, btn6, 2, 1, 1, 1);
addComp(numPanel, btn7, 0, 2, 1, 1);
addComp(numPanel, btn8, 1, 2, 1, 1);
addComp(numPanel, btn9, 2, 2, 1, 1);
addComp(numPanel, btn0, 0, 3, 2, 1);
addComp(numPanel, btnDot, 2, 3, 1, 1);
addComp(numPanel, btnCl, 0, 4, 2, 1);
addComp(numPanel, btnBs, 2, 4, 1, 1);

// add panel to mainpanel
addComp(mainPanel, numPanel, 2, 1, 1, 3);

// --------------------------------------------------------------------------

//basicFuncPanel, verticalboxlayout, spans gridheight 3
basicFuncPanel.setLayout(new BoxLayout(basicFuncPanel, BoxLayout.Y_AXIS));

// add buttons to panel
basicFuncPanel.add(btnPlus);
basicFuncPanel.add(btnMinus);
basicFuncPanel.add(btnMult);
basicFuncPanel.add(btnDiv);
basicFuncPanel.add(btnEq);

// add panel to main panel
addComp(mainPanel, basicFuncPanel, 3, 1, 1, 3);

// --------------------------------------------------------------------------

this.pack();
this.setVisible(true);

}


private void addComp(JPanel panel, JComponent comp, int xPos, int yPos, int compWidth, int compHeight) {

GridBagConstraints gridConstraints = new GridBagConstraints();

gridConstraints.gridx = xPos;
gridConstraints.gridy = yPos;
gridConstraints.gridwidth = compWidth;
gridConstraints.gridheight = compHeight;
gridConstraints.weightx = 100;
gridConstraints.weighty = 100;
gridConstraints.insets = new Insets(5, 5, 5, 5);
gridConstraints.anchor = GridBagConstraints.CENTER;
gridConstraints.fill = GridBagConstraints.BOTH;

panel.add(comp, gridConstraints);

}

private JComponent[] components = { mPlus, mClear, mA, mB, mC, mD, aSq, aCu, aPowa, aInv, aSqrt, aFact,
btnSin, btnCos, btnTan, btnaSin, btnaCos, btnaTan, hyp, btnAbs, btnExp, btnLog, btnLn, btnLoga,
btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn0, btnDot, btnCl, btnBs, btnPlus, btnMinus,
btnMult, btnDiv, btnEq };

public JComponent[] getComponents() {
return components;
}


}

最佳答案

只需进行一项更改,一切都会正常工作。

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
new MyCalculatorGUI();
}
});
}
<小时/>

Use SwingUtilities.invokeLater() to make sure that EDT is initialized properly.

了解更多

<小时/>

编辑

  • 有时 this.setResizable(false); 在重新调整大小或打包 JFrame 中的组件时会产生问题。

关于java - JFrame布局管理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24271944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com