简体   繁体   中英

GUI using applet/GridBagLayout

I need to create a java applet of a calculator with 2 bottoms on the top that take the whole row and under that a text area with a scroll pane and also below that 12 bottoms 4x4. Now I need to make it on an applet not on a frame which is where I am having trouble with because my GridBagLayout is not working and my buttons wont expand.

The applet I need to make.

Write the Java applet which implements a four operation calculator,The calculator will work as follows: 1. The scrollable text area will show each digit as entered. 2. Decimal numbers are allowed, and you must ensure that only one decimal symbol is entered for each number (you decide what the behaviour of the calculator should be if the user breaks this rule, but it must be controlled by your program) 3. When an operator ('+', '-', '/', or '*') is clicked, the operator should be displayed after the last digit entered, and subsequent numbers should be displayed on a new line 4. After entering a sequence of firstnumber operator secondnumber the user can click: a. '=', in which case the calculator must display: i. the '=' symbol after the last digit of the secondnumber ii. the result of the operation, on a new line iii. anything else entered after the '=' symbol is part of a new calculation, and must be displayed on a separate line For example, the user clicks “123.45+456.2=1”. The screen should look like this: 123.45+ ← entered by user 456.2= ← entered by user 579.65  calculated & displayed by your program 1 ←beginning of a new calculation, entered by user b. any new operator, in which case the calculator must assume an implicit '=' and: i. display an '=' sign after the last number ii. display the result of the previous operation on a new line, followed by the new operator iii. treat the result as the first number of a new calculation, and display the new operator right after it iv. allow the user to enter a new number For example, the user clicks “123.45+456.2/5=”. The calculator interpretation is that the user wants the first two numbers added, and the sum divided by 5. The screen should look like this: 123.45+ ← entered by user 456.2= 456.2 entered by user, '=' added by the program 579.65/ 579.65 calculated by program, '/' entered by user 5= new calculation, entered by user 115.93 ←calculated by your program 5. When the user enters two or more consecutive operators, write an error message in the text area stating that operator 'x' has been ignored. The message should be on a separate line, and everything else following should be on a new line. 6. When the user clicks “Clear Last” the calculator should delete the last character displayed on the screen from the screen. Further behaviour of your calculator

    import java.applet.*;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.ScrollPane;
    import java.awt.TextArea;

    public class MarksProc extends Applet implements ActionListener{

      TextField t1, t2;
      Label l3,l4;
      JButton b1, b2, b3;
      String best ="";
      TextArea ta;
      ScrollPane scroll;
      Button b[]=new Button[16];
      String cmd[]={"+","-","*","/","=","C"};
      int sum=0, count=0, oldmark=0;
      public void init(){


        Panel p1 = new Panel(new GridBagLayout());
        p1.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        b1 = new JButton("Clear Last");
        b1.setSize(10,30);
         c.fill = GridBagConstraints.BOTH;
         c.gridx = 0;
         c.gridy = 0;
         c.ipady = 40;
        // p1.add(b1,c);
         add(b1,c);

         b2 = new JButton("Clear All");
         c.fill = GridBagConstraints.HORIZONTAL;
         c.weightx = 0.5;
         c.gridx = 1;
         c.gridy = 0; 
       // p1.add(b2,c);

         add(b2,c);




        //add(b1, "North");
        //add(b2);



        Panel p2 = new Panel();
        p2.setLayout(new GridBagLayout());
        c.fill = GridBagConstraints.HORIZONTAL;
         c.weightx = 0.5;
         c.gridx = 0;
         c.gridy = 1; 
        ta = new TextArea();
       // scroll = new ScrollPane(ta);
       //  scroll.setBounds(10, 11, 455, 249);  
       // scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        p2.add(ta,c);



        add(ta,"Center");

        ta.setText("0");


        Panel p3=new Panel();

        p3.setLayout(new GridLayout(4,4));


        for(int i=0;i<16;i++)

        {

          if(i<10)

            b[i]=new Button(String.valueOf(i));

          else

            b[i]=new Button(cmd[i%10]);


          b[i].setFont(new Font("Arial",Font.BOLD,80));

          p3.add(b[i]);

          add(p3,"Center");



          b[i].addActionListener(this);
         // setSize(800, 900);
        }

      }

      public void actionPerformed(ActionEvent e){
      }
    }

带GBL的计算器

Note: I factored out adding the component, to a separate method partly to enforce specifying each of the layout constraints that might change.

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

public class CalculatorWithGBL {

    CalculatorWithGBL() {
        initUI();
    }

    public void initUI() {
        if (ui != null) {
            return;
        }

        ui = new JPanel(new GridBagLayout());
        ui.setBorder(new EmptyBorder(4, 4, 4, 4));

        gbc.gridheight = 1;
        gbc.fill = GridBagConstraints.BOTH; // fill cell
        gbc.weightx = .5; //expand to fill container size
        gbc.weighty = .5; //  "    "   "      "       "   

        addComponentToContainer(new JButton("Clear Last"), 0, 0, 2);
        addComponentToContainer(new JButton("Clear All"), 2, 0, 2);

        addComponentToContainer(new JScrollPane(new JTextArea(4, 20)), 0, 1, 4);

        addComponentToContainer(new JButton("7"), 0, 2, 1);
        addComponentToContainer(new JButton("8"), 1, 2, 1);
        addComponentToContainer(new JButton("9"), 2, 2, 1);
        addComponentToContainer(new JButton("+"), 3, 2, 1);

        addComponentToContainer(new JButton("4"), 0, 3, 1);
        addComponentToContainer(new JButton("5"), 1, 3, 1);
        addComponentToContainer(new JButton("6"), 2, 3, 1);
        addComponentToContainer(new JButton("-"), 3, 3, 1);

        addComponentToContainer(new JButton("1"), 0, 4, 1);
        addComponentToContainer(new JButton("2"), 1, 4, 1);
        addComponentToContainer(new JButton("3"), 2, 4, 1);
        addComponentToContainer(new JButton("*"), 3, 4, 1);

        addComponentToContainer(new JButton("0"), 0, 5, 1);
        addComponentToContainer(new JButton("."), 1, 5, 1);
        addComponentToContainer(new JButton("="), 2, 5, 1);
        addComponentToContainer(new JButton("/"), 3, 5, 1);
    }

    private final void addComponentToContainer(
            Component c, int gridx, int gridy, int gridwidth) {
        gbc.gridx = gridx;
        gbc.gridy = gridy;
        gbc.gridwidth = gridwidth;

        ui.add(c, gbc);
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(
                            UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                CalculatorWithGBL o = new CalculatorWithGBL();

                JFrame f = new JFrame("Calculator with GBL");
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }

    private JComponent ui = null;
    private GridBagConstraints gbc = new GridBagConstraints();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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