【问题标题】:Areanging calculator interface排列计算器界面
【发布时间】:2015-11-28 16:15:34
【问题描述】:

在这段代码中,前三个按钮出现在文本区域,我怎样才能让这些按钮从标题下方开始?在添加按钮之前,按钮区域从标题下方开始,但是一旦我添加了按钮,它就会出现在标题上

   public Design ()
   {

    header = new JPanel ();
    header.setBounds(0, 0, 300, 100);
    header.setBackground(Color.WHITE);
    header.setLayout (null);

    buttonarea = new JPanel ();
    buttonarea.setBounds (0,0, 300, 300);
    buttonarea.setBackground(Color.BLACK);
    buttonarea.setLayout(new GridLayout (6,3));

    add (header);
    add (buttonarea);

    zero = new JButton ("0");
    zero.setBackground(Color.PINK);
    one = new JButton ("1");
    one.setBackground(Color.PINK);
    two = new JButton ("2");
    two.setBackground(Color.PINK);
    three = new JButton ("3");
    three.setBackground(Color.PINK);
    four = new JButton ("4");
    four.setBackground(Color.PINK);
    five = new JButton ("5");
    five.setBackground(Color.PINK);
    six = new JButton ("6");
    six.setBackground(Color.PINK);
    seven = new JButton ("7");
    seven.setBackground(Color.PINK);
    eight = new JButton ("8");
    eight.setBackground(Color.PINK);
    nine = new JButton ("9");
    nine.setBackground(Color.PINK);
    add = new JButton ("+");
    add.setBackground(Color.PINK);
    subtract = new JButton ("-");
    subtract.setBackground(Color.PINK);
    divide = new JButton ("*");
    divide.setBackground(Color.PINK);
    multiply = new JButton ("/");
    multiply.setBackground(Color.PINK);
    square = new JButton ("x^2");
    square.setBackground(Color.PINK);
    equal = new JButton ("=");
    equal.setBackground(Color.PINK);
    c = new JButton ("C");
    c.setBackground(Color.PINK);
    clear = new JButton ("delete");
    clear.setBackground(Color.PINK);
    dot = new JButton (".");
    dot.setBackground(Color.PINK);

    written = new JTextArea ();
    written.setBackground(Color.WHITE);

   header.add (written);

    buttonarea.add (c);
    buttonarea.add (clear);
    buttonarea.add (equal);
    buttonarea.add (zero);
    buttonarea.add (one);
    buttonarea.add (add);
    buttonarea.add (two);   
    buttonarea.add (three);
    buttonarea.add (subtract);
    buttonarea.add (four);
    buttonarea.add (five);
    buttonarea.add (multiply);
    buttonarea.add (six);
    buttonarea.add (seven);
    buttonarea.add (square);
    buttonarea.add (eight); 
    buttonarea.add (nine);    
    buttonarea.add (dot);  
   }  

【问题讨论】:

    标签: java swing calculator


    【解决方案1】:

    虽然空布局和setBounds() 在 Swing 新手看来是创建复杂 GUI 的最简单和最好的方法,但创建的 Swing GUI 越多,使用它们时遇到的困难就越严重。当 GUI 调整大小时,它们不会调整您的组件大小,它们是增强或维护的皇家女巫,放置在滚动窗格中时它们完全失败,在与原始不同的所有平台或屏幕分辨率上查看时它们看起来很糟糕.

    对主 GUI 使用 BorderLayout,放置 JTextField BorderLayout.PAGE_START。使用 JPanel 将 JButtons 放置在 GridLayout 中,并将此 JPanel 放置在主 GUI 的 BorderLayout.CENTER 位置。

    例如这个图形用户界面:

    可以用这个 GUI 制作:

    import java.awt.BorderLayout;
    import java.awt.Font;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class SimpleCalc extends JPanel {
        private static final String[][] BTN_TEXTS = {
            {"C", "Del", "X^2", "+"},
            {"7", "8", "9", "-"},
            {"4", "5", "6", "*"},
            {"1", "2", "3", "/"},
            {"0", ".", "", "="}
        };
        private static final int COLS = 12;
        private static final int GAP = 3;
        private static final char[] NUMBER_ARRAY = "0123456789".toCharArray();
        private static final List<Character> NUMBER_LIST = new ArrayList<>();
        private static final float FONT_SIZE = 36f;
        private JTextField display = new JTextField(COLS);
    
        static {
            for (char c : NUMBER_ARRAY) {
                NUMBER_LIST.add(c);
            }
        }
    
        public SimpleCalc() {
            display.setFont(display.getFont().deriveFont(FONT_SIZE));
            display.setFocusable(false);
    
            int rows = BTN_TEXTS.length;
            int cols = BTN_TEXTS[0].length;
            JPanel buttonPanel = new JPanel(new GridLayout(rows, cols, GAP, GAP));
            for (String[] btnTextRow : BTN_TEXTS) {
                for (String btnText : btnTextRow) {
                    if (btnText.isEmpty()) {
                        buttonPanel.add(new JLabel());
                    } else {
                        Action action = null;
                        if (NUMBER_LIST.contains(btnText.charAt(0))) {
                            action = new NumericAction(btnText);
                        } else if (".".equals(btnText)) {
                            action = new DotAction(btnText);
                        } else {
                            action = new OperationAction(btnText);
                        }
                        JButton button = new JButton(action);
                        button.setFont(button.getFont().deriveFont(Font.BOLD, FONT_SIZE));
                        buttonPanel.add(button);
                    }
                }
            }
    
            setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
            setLayout(new BorderLayout(GAP, GAP));
            add(display, BorderLayout.PAGE_START);
            add(buttonPanel, BorderLayout.CENTER);
        }
    
        private class NumericAction extends AbstractAction {
            public NumericAction(String name) {
                super(name);
            }
    
            @Override
            public void actionPerformed(ActionEvent e) {
                String text = display.getText();
                text += e.getActionCommand();
                display.setText(text);
            }
        }
    
        private class DotAction extends AbstractAction {
            public DotAction(String name) {
                super(name);
            }
    
            @Override
            public void actionPerformed(ActionEvent e) {
                String text = display.getText();
                if (text.contains(".")) {
                    return; // only one dot allowed
                }
                text += e.getActionCommand();
                display.setText(text);
            }
        }
    
        private class OperationAction extends AbstractAction {
            public OperationAction(String name) {
                super(name);
            }
    
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO write code for operations buttons
            }
        }
    
        private static void createAndShowGui() {
            SimpleCalc mainPanel = new SimpleCalc();
    
            JFrame frame = new JFrame("SimpleCalc");
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.getContentPane().add(mainPanel);
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGui();
                }
            });
        }
    }
    

    【讨论】:

      【解决方案2】:
      add (header);
      add (buttonarea);
      

      我猜你正在使用一个使用 BorderLayout 的 JFrame。上面的代码在 BorderLayout 的 CENTER 中添加了两个组件,这是不允许的。

      相反,您应该指定适当的约束:

      add (header, BorderLayout.PAGE_NORTH);
      add (buttonarea, BorderLayout.CENTER);
      

      如果您没有在框架上使用 BorderLayout,那么您应该这样做。阅读 How to Use BorderLayout 上的 Swing 教程部分,了解更多信息和示例。

      另外,在创建 JTextAra 时,您应该使用:

      written = new JTextArea (row, columns);
      

      给文本区域一个合适的大小。在这种情况下,您可以直接将文本区域添加到框架中:

      //add (header, BorderLayout.PAGE_NORTH);
      add (written, BorderLayout.PAGE_NORTH);
      

      不需要包装面板。

      【讨论】:

        猜你喜欢
        • 2016-02-16
        • 1970-01-01
        • 2019-09-05
        • 1970-01-01
        • 2016-12-02
        • 1970-01-01
        • 1970-01-01
        • 2021-12-25
        相关资源
        最近更新 更多