大家好我正在制作计算器,所以很多按钮必须显示在面板上。 我想找到绝对按钮,所以我称之为'panel.setLayout(null)'但按钮 不会出现所以我发表这个声明,然后我无法处理按钮位置 表达,我该怎么办? 3个按钮位置的底部是错误的。
class CalcTest extends JFrame implements ActionListener {
private JPanel panel;
static JTextField ansField;
static JButton mcButton, mrButton, msButton, mpButton, mmButton;
static JButton backButton,ceButton,cButton,plusMinusButton,divButton,multiButton,subButton,addButton,rootButton,percentButton,reciprocButton,equalButton,decimalButton;
static JButton oneButton,twoButton,threeButton,fourButton,fiveButton,sixButton,sevenButton,eightButton,nineButton,zeroButton;
static int addClick = 0, subClick = 0, multiClick = 0, divClick = 0, percentClick = 0, reciprocClick = 0;
static double num1, num2, ans;
public CalcTest(){
// frame basic options
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Calculator");
setSize(220,310);
setResizable(false);
setLocationRelativeTo(null);
panel = new JPanel();
panel.setSize(220,310);
**panel.setLayout(null);**
// adding buttons and stuff
ansField = new JTextField();
ansField.setPreferredSize(new Dimension(190,50));
ansField.setLocation(12,31);
ansField.setEditable(false);
ansField.setHorizontalAlignment(JTextField.RIGHT);
mcButton = new JButton("MC");
mrButton = new JButton("MR");
msButton = new JButton("MS");
mpButton = new JButton("M+");
mmButton = new JButton("M-");
backButton = new JButton("←");
ceButton = new JButton("CE");
cButton = new JButton("C");
plusMinusButton = new JButton("±");
rootButton = new JButton("√");
sevenButton = new JButton("7");
eightButton = new JButton("8");
nineButton = new JButton("9");
divButton = new JButton("/");
percentButton = new JButton("%");
fourButton = new JButton("4");
fiveButton = new JButton("5");
sixButton = new JButton("6");
multiButton = new JButton("*");
reciprocButton = new JButton("1/x");
oneButton= new JButton("1");
twoButton = new JButton("2");
threeButton = new JButton("3");
subButton = new JButton("-");
zeroButton = new JButton("0");
decimalButton = new JButton(".");
addButton = new JButton("+");
equalButton = new JButton("=");
mcButton.setPreferredSize(new Dimension(34,27));
mrButton.setPreferredSize(new Dimension(34,27));
msButton.setPreferredSize(new Dimension(34,27));
mpButton.setPreferredSize(new Dimension(34,27));
mmButton.setPreferredSize(new Dimension(34,27));
backButton.setPreferredSize(new Dimension(34,27));
ceButton.setPreferredSize(new Dimension(34,27));
cButton.setPreferredSize(new Dimension(34,27));
plusMinusButton.setPreferredSize(new Dimension(34,27));
rootButton.setPreferredSize(new Dimension(34,27));
sevenButton.setPreferredSize(new Dimension(34,27));
eightButton.setPreferredSize(new Dimension(34,27));
nineButton.setPreferredSize(new Dimension(34,27));
divButton.setPreferredSize(new Dimension(34,27));
percentButton.setPreferredSize(new Dimension(34,27));
fourButton.setPreferredSize(new Dimension(34,27));
fiveButton.setPreferredSize(new Dimension(34,27));
sixButton.setPreferredSize(new Dimension(34,27));
multiButton.setPreferredSize(new Dimension(34,27));
reciprocButton.setPreferredSize(new Dimension(34,27));
oneButton.setPreferredSize(new Dimension(34,27));
twoButton.setPreferredSize(new Dimension(34,27));
threeButton.setPreferredSize(new Dimension(34,27));
subButton.setPreferredSize(new Dimension(34,27));
zeroButton.setPreferredSize(new Dimension(34,27));
decimalButton.setPreferredSize(new Dimension(34,27));
addButton.setPreferredSize(new Dimension(34,27));
equalButton.setPreferredSize(new Dimension(34,27));
mcButton.setFont(new Font("Arial",Font.PLAIN,11));
mrButton.setFont(new Font("Arial",Font.PLAIN,11));
msButton.setFont(new Font("Arial",Font.PLAIN,11));
mpButton.setFont(new Font("Arial",Font.PLAIN,11));
mmButton.setFont(new Font("Arial",Font.PLAIN,11));
backButton.setFont(new Font("Arial",Font.BOLD,13));
ceButton.setFont(new Font("Arial",Font.PLAIN,11));
cButton.setFont(new Font("Arial",Font.PLAIN,11));
plusMinusButton.setFont(new Font("Arial",Font.PLAIN,11));
rootButton.setFont(new Font("Arial",Font.PLAIN,11));
sevenButton.setFont(new Font("Arial",Font.PLAIN,11));
eightButton.setFont(new Font("Arial",Font.PLAIN,11));
nineButton.setFont(new Font("Arial",Font.PLAIN,11));
divButton.setFont(new Font("Arial",Font.PLAIN,11));
percentButton.setFont(new Font("Arial",Font.PLAIN,11));
fourButton.setFont(new Font("Arial",Font.PLAIN,11));
fiveButton.setFont(new Font("Arial",Font.PLAIN,11));
sixButton.setFont(new Font("Arial",Font.PLAIN,11));
multiButton.setFont(new Font("Arial",Font.PLAIN,11));
reciprocButton.setFont(new Font("Arial",Font.PLAIN,11));
oneButton.setFont(new Font("Arial",Font.PLAIN,11));
twoButton.setFont(new Font("Arial",Font.PLAIN,11));
threeButton.setFont(new Font("Arial",Font.PLAIN,11));
subButton.setFont(new Font("Arial",Font.PLAIN,11));
zeroButton.setFont(new Font("Arial",Font.PLAIN,11));
decimalButton.setFont(new Font("Arial",Font.PLAIN,11));
addButton.setFont(new Font("Arial",Font.PLAIN,11));
equalButton.setFont(new Font("Arial",Font.BOLD,13));
mcButton.setMargin(new Insets(0,0,0,0));
mrButton.setMargin(new Insets(0,0,0,0));
msButton.setMargin(new Insets(0,0,0,0));
mpButton.setMargin(new Insets(0,0,0,0));
mmButton.setMargin(new Insets(0,0,0,0));
backButton.setMargin(new Insets(0,0,0,0));
ceButton.setMargin(new Insets(0,0,0,0));
cButton.setMargin(new Insets(0,0,0,0));
plusMinusButton.setMargin(new Insets(0,0,0,0));
rootButton.setMargin(new Insets(0,0,0,0));
sevenButton.setMargin(new Insets(0,0,0,0));
eightButton.setMargin(new Insets(0,0,0,0));
nineButton.setMargin(new Insets(0,0,0,0));
divButton.setMargin(new Insets(0,0,0,0));
percentButton.setMargin(new Insets(0,0,0,0));
fourButton.setMargin(new Insets(0,0,0,0));
fiveButton.setMargin(new Insets(0,0,0,0));
sixButton.setMargin(new Insets(0,0,0,0));
multiButton.setMargin(new Insets(0,0,0,0));
reciprocButton.setMargin(new Insets(0,0,0,0));
oneButton.setMargin(new Insets(0,0,0,0));
twoButton.setMargin(new Insets(0,0,0,0));
threeButton.setMargin(new Insets(0,0,0,0));
subButton.setMargin(new Insets(0,0,0,0));
zeroButton.setMargin(new Insets(0,0,0,0));
decimalButton.setMargin(new Insets(0,0,0,0));
addButton.setMargin(new Insets(0,0,0,0));
equalButton.setMargin(new Insets(0,0,0,0));
mcButton.setFocusable(false);
mrButton.setFocusable(false);
msButton.setFocusable(false);
mpButton.setFocusable(false);
mmButton.setFocusable(false);
backButton.setFocusable(false);
ceButton.setFocusable(false);
cButton.setFocusable(false);
plusMinusButton.setFocusable(false);
rootButton.setFocusable(false);
sevenButton.setFocusable(false);
eightButton.setFocusable(false);
nineButton.setFocusable(false);
divButton.setFocusable(false);
percentButton.setFocusable(false);
fourButton.setFocusable(false);
fiveButton.setFocusable(false);
sixButton.setFocusable(false);
multiButton.setFocusable(false);
reciprocButton.setFocusable(false);
oneButton.setFocusable(false);
twoButton.setFocusable(false);
threeButton.setFocusable(false);
subButton.setFocusable(false);
zeroButton.setFocusable(false);
decimalButton.setFocusable(false);
addButton.setFocusable(false);
equalButton.setFocusable(false);
mcButton.setLocation(12,67);
mrButton.setLocation(51,67);
msButton.setLocation(90,67);
mpButton.setLocation(129,67);
mmButton.setLocation(168,67);
backButton.setLocation(12,118);
ceButton.setLocation(51,118);
cButton.setLocation(90,118);
plusMinusButton.setLocation(129,118);
rootButton.setLocation(168,118);
sevenButton.setLocation(12,169);
eightButton.setLocation(51,169);
nineButton.setLocation(90,169);
divButton.setLocation(129,169);
percentButton.setLocation(168,169);
fourButton.setLocation(12,220);
fiveButton.setLocation(51,220);
sixButton.setLocation(90,220);
multiButton.setLocation(129,220);
reciprocButton.setLocation(168,220);
oneButton.setLocation(12,271);
twoButton.setLocation(51,271);
threeButton.setLocation(90,271);
subButton.setLocation(129,271);
equalButton.setLocation(168,271);
zeroButton.setLocation(12,322);
decimalButton.setLocation(90,322);
addButton.setLocation(129,322);
panel.add(ansField);
panel.add(mcButton);
panel.add(mrButton);
panel.add(msButton);
panel.add(mpButton);
panel.add(mmButton);
panel.add(backButton);
panel.add(ceButton);
panel.add(cButton);
panel.add(plusMinusButton);
panel.add(rootButton);
panel.add(sevenButton);
panel.add(eightButton);
panel.add(nineButton);
panel.add(divButton);
panel.add(percentButton);
panel.add(fourButton);
panel.add(fiveButton);
panel.add(sixButton);
panel.add(multiButton);
panel.add(reciprocButton);
panel.add(oneButton);
panel.add(twoButton);
panel.add(threeButton);
panel.add(subButton);
panel.add(equalButton);
panel.add(zeroButton);
panel.add(decimalButton);
panel.add(addButton);
add(panel);
// button listener
// setvisible
setVisible(true);
}
public void actionPerformed(ActionEvent e){
}
}
public class Calc {
public static void main(String args[]){
CalcTest c = new CalcTest();
}
}
答案 0 :(得分:1)
在没有LayoutManager
的情况下工作时,您需要自己设置界限组件。
使用setBounds(..)
或setLocation(..)
和setSize(..)
。
在您的代码中,您设置了首选大小,但这仅与LayoutManager
相关,setPreferredSize(..)
在布局组件和计算容器的首选大小时使用此大小作为提示。
TL / DR:
将setSize(..)
替换为{{1}},它应该有效。