![](/img/trans.png)
[英]Unit testing issues with Javax Swing GUI application - suitable JDK 7 option?
[英]GUI abnormalities in Swing, JDK 7
我是Java的新手,嘗試使用Swing進行首次接觸,因此我決定使用Swing和JButtons
/ JTextFields
在Java中模擬一個計算器。
問題是,大多數按鈕都可以正確顯示並以正確的大小顯示,但是有一些異常-特別是,減號(-)按鈕沒有出現,等式按鈕(=)出現在其他按鈕的后面 。 可以發布圖片,但這足夠了,我的代表不允許我插入圖片; 我在這里失去了一個較舊的帳戶,必須承擔后果。
事不宜遲,這里是代碼:
// Set the display JTextField as the top element in the GUI stackup:
disp.setBounds(50,25,400,50);
button7.setBounds(50,75,x,y);
button8.setBounds(150,75,x,y);
button9.setBounds(250,75,x,y);
buttonPlus.setBounds(350,75,x,y);
button4.setBounds(50,125,x,y);
button5.setBounds(150,125,x,y);
button6.setBounds(250,125,x,y);
buttonMinus.setBounds(350,75,x,y);
button1.setBounds(50,175,x,y);
button2.setBounds(150,175,x,y);
button3.setBounds(250,175,x,y);
buttonMult.setBounds(350,175,x,y);
buttonClear.setBounds(50,225,x,y);
button0.setBounds(150,225,x,y);
buttonEqual.setBounds(250,225,x,y);
buttonDiv.setBounds(350,225,x,y);
frame.add(disp);
frame.add(button0);
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);
frame.add(button6);
frame.add(button7);
frame.add(button8);
frame.add(button9);
frame.add(buttonClear);
frame.add(buttonPlus);
frame.add(buttonMinus);
frame.add(buttonMult);
frame.add(buttonDiv);
frame.add(buttonEqual);
//Display the window.
frame.pack();
frame.setVisible(true);
如果相關的話,我還將繼續發布構造函數。 如果有任何相關性,我正在使用IntelliJ IDEA。
在你的代碼, buttonMinus
具有完全相同的范圍為buttonPlus
。 毫無疑問,這是無辜的剪切和粘貼錯誤。
當您說“等於”按鈕位於其他按鈕之后時,我不知道您的意思。 被他們完全掩蓋了嗎? 被部分遮蓋? 它出現在正確的行中嗎? 太寬了嗎? 太窄?
最終,沒關系。 安德魯是對的。 使用布局管理器可以防止出現這些麻煩以及許多其他麻煩,包括前面提到的剪切和粘貼錯誤:
JPanel buttonPanel = new JPanel(new GridLayout(0, 4, 3, 3));
buttonPanel.add(button7);
buttonPanel.add(button8);
buttonPanel.add(button9);
buttonPanel.add(buttonPlus);
buttonPanel.add(button4);
buttonPanel.add(button5);
buttonPanel.add(button6);
buttonPanel.add(buttonMinus);
buttonPanel.add(button1);
buttonPanel.add(button2);
buttonPanel.add(button3);
buttonPanel.add(buttonMult);
buttonPanel.add(buttonClear);
buttonPanel.add(button0);
buttonPanel.add(buttonEqual);
buttonPanel.add(buttonDiv);
我知道您想一步步進行,但是較簡單的LayoutManager(GridLayout,BorderLayout,FlowLayout和BoxLayout)是您不應該跳過/推遲的步驟。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.