简体   繁体   English

Swing,JDK 7中的GUI异常

[英]GUI abnormalities in Swing, JDK 7

I'm relatively new to Java and attempting to get my first exposure using Swing, and so I've decided to mock up a calculator in Java using Swing and JButtons / JTextFields . 我是Java的新手,尝试使用Swing进行首次接触,因此我决定使用Swing和JButtons / JTextFields在Java中模拟一个计算器。

Problem being, most buttons display correctly and at the correct sizes, but there are a few abnormalities - particularly, the subtraction (-) button doesn't appear and the equation button (=) appears behind the others. 问题是,大多数按钮都可以正确显示并以正确的大小显示,但是有一些异常-特别是,减号(-)按钮没有出现,等式按钮(=)出现其他按钮的后面 Would post an image, but that should be sufficient and my rep won't allow me to inline an image; 可以发布图片,但这足够了,我的代表不允许我插入图片; I lost an older account I had on here and have to suffer the consequences. 我在这里失去了一个较旧的帐户,必须承担后果。

Without further ado, here's the code: 事不宜迟,这里是代码:

// 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);

If it's relevant, I'll proceed to post the constructors as well; 如果相关的话,我还将继续发布构造函数。 I'm using IntelliJ IDEA if that's of any pertinence. 如果有任何相关性,我正在使用IntelliJ IDEA。

In your code, buttonMinus has exactly the same bounds as buttonPlus . 在你的代码, buttonMinus具有完全相同的范围为buttonPlus No doubt an innocent cut-and-paste error. 毫无疑问,这是无辜的剪切和粘贴错误。

I don't know what you mean when you say the equals button is behind the others; 当您说“等于”按钮位于其他按钮之后时,我不知道您的意思。 is it totally obscured by them? 被他们完全掩盖了吗? Partly obscured? 被部分遮盖? Is it appearing in the correct row? 它出现在正确的行中吗? Is it too wide? 太宽了吗? Too narrow? 太窄?

Ultimately, it doesn't matter. 最终,没关系。 Andrew is right; 安德鲁是对的。 using a layout manager will prevent these headaches and many others, including the aforementioned cut-and-paste error: 使用布局管理器可以防止出现这些麻烦以及许多其他麻烦,包括前面提到的剪切和粘贴错误:

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);

I get that you wanted to proceed in small steps, but the simpler LayoutManagers (GridLayout, BorderLayout, FlowLayout, and BoxLayout) are a step that you should not skip/postpone. 我知道您想一步步进行,但是较简单的LayoutManager(GridLayout,BorderLayout,FlowLayout和BoxLayout)是您不应该跳过/推迟的步骤。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM