简体   繁体   English

Java GUI布局问题

[英]Java GUI Layout Issue

I have the code working for this assignment, but can't figure out how to get it to look properly. 我有代码工作这个任务,但无法弄清楚如何让它看起来正确。 I am a bit baffled. 我有点困惑。 Need to use a Border Layout with all five regions added to the container. 需要使用边框布局,将所有五个区域添加到容器中。 Just need to be pointed in the right direction...thanks. 只需要指出正确的方向......谢谢。 I can't post a pic...but it is something like this... 我不能发布图片...但它是这样的......

              Sum Numbers(Centered)
                   Text box 1
                   Text box 2
                   Text box 3
                   Result is: sum of numbers
                   Button 1(Sum Numbers)Button 2(Close)

Code: 码:

import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Vector;

public class Calculator extends JFrame {

    // Declare GUI components
    private JTextField jtfFirst;
    private JTextField jtfSecond;
    private JTextField jtfThird;
    private JTextField jtfResult;
    private JLabel jlblTitle;
    private JLabel jlblFirst;
    private JLabel jlblSecond;
    private JLabel jlblThird;
    private JLabel jlblResult;
    private JPanel entryPanel;
    private JPanel buttonPanel;
    private JButton jbtAdd;
    private JButton jbtClear;

    // main method to instantiate and customize frame
    public static void main(String[] args)
    {
        Vector<String> myVector=new Vector<String>();
        Calculator frame = new Calculator();
        frame.setTitle( "Assignment 5");
        int windowWidth = 300;
        int windowHeight = 200;
        frame.setSize( 350, 150);
        frame.setLocation(400, 400);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    // GUI constructor
    public Calculator()
    {
        // a bottom JPanel to place everything on.
        //JPanel totalGUI = new JPanel();
        //totalGUI.setLayout(null);

        JLabel textLabel = new JLabel("Add Numbers", JLabel.CENTER);
        textLabel.setFont(new Font("Ariel",Font.PLAIN,24));

        //textLabel.setPreferredSize(new Dimension(300, 100));

        jlblResult = new JLabel( "Total is: ", JLabel.LEFT );
        jtfFirst = new JTextField(9);
        jtfSecond = new JTextField(9);
        jtfThird = new JTextField(9);
        jtfResult = new JTextField(6);
        entryPanel = new JPanel();

        // make result text field uneditable
        jtfResult.setEditable( false );

        // Set layout manager of panel
        entryPanel.setLayout( new GridLayout(6,1,2,2));

        // add GUI components to panel

        entryPanel.add(textLabel );
        entryPanel.add( jtfFirst );
        entryPanel.add( jtfSecond );
        entryPanel.add( jtfThird );
        entryPanel.add( jlblResult);
        entryPanel.add( jtfResult );

        // add entryPanel to frame
        add(entryPanel, BorderLayout.CENTER);

        // Instantiate GUI components for bottom of frame
        jbtAdd = new JButton( "Show Total" );
        jbtClear = new JButton( "Clear" );
        buttonPanel = new JPanel();

        // add buttons to panel
        buttonPanel.add( jbtAdd );
        buttonPanel.add( jbtClear );

        // add buttonPanel to frame
        add( buttonPanel, BorderLayout.SOUTH);

        //Event Handler
        jbtAdd.addMouseListener(new List_ButtonADD());
        jbtClear.addMouseListener(new List_ButtonCLEAR());
    }

    private class List_ButtonADD implements MouseListener
    {
        public void mouseClicked(MouseEvent event)
        {
            double x=Double.parseDouble(jtfFirst.getText());
            double y=Double.parseDouble(jtfSecond.getText());
            double z=Double.parseDouble(jtfThird.getText());

            jtfResult.setText(String.valueOf(x+y+z));

         public void mousePressed(MouseEvent event)
         {
         }
         public void mouseReleased(MouseEvent event)
         {
         }
         public void mouseEntered(MouseEvent event)
         {
         }
         public void mouseExited(MouseEvent event)
         {
         }
    }

    private class List_ButtonCLEAR implements MouseListener
    {
        public void mouseClicked(MouseEvent event)
        {
            System.exit (0);
        }
        public void mousePressed(MouseEvent event)
        {
        }
        public void mouseReleased(MouseEvent event)
        {
        }
        public void mouseEntered(MouseEvent event)
        {
        }
        public void mouseExited(MouseEvent event)
        {
        }
    }
}

图片

Ok here is how you go about doing it: 好的,这是你如何去做:

  1. get a JFrame and set its layout to BorderLayout . 获取一个JFrame并将其布局设置为BorderLayout
  2. Create a JLabel with text set to "Add Numbers" and add to the JFrame it using BorderLayout.NORTH 创建一个文本设置为“Add Numbers”的JLabel ,并使用BorderLayout.NORTH将其添加到JFrame
  3. create a JPanel , set its layout to BorderLayout . 创建一个JPanel ,将其布局设置为BorderLayout
  4. Create the 3 JTextField , add them to the JPanel using BorderLayout.NORTH , BorderLayout.CENTER and BorderLayout.SOUTH 创建3 JTextField ,使用BorderLayout.NORTHBorderLayout.CENTERBorderLayout.SOUTH将它们添加到JPanel
  5. Create another JPanel . 创建另一个JPanel
  6. Create a JLabel to display "Total is 60". 创建一个JLabel以显示“Total is 60”。 Also create the two JButtons . 还要创建两个JButtons
  7. Set the Layout of JPanel of step 5 to BorderLayout . 将步骤5的JPanel布局设置为BorderLayout Add the JLabel using BorderLayout.NORTH . 使用BorderLayout.NORTH添加JLabel
  8. Create another JPanel with FlowLayout , add the two buttons. 使用FlowLayout创建另一个JPanel ,添加两个按钮。 Then add this JPanel to the JPanel in step 5 with BorderLayout.SOUTH . 然后在步骤5中使用BorderLayout.SOUTH将此JPanel添加到JPanel
  9. Add all the JPanels to your JFrame . 将所有JPanels添加到JFrame

Here is an example for you. 这是一个例子。 I have used GridBagLayout because it's very flexible. 我使用了GridBagLayout因为它非常灵活。

public class ExampleFrame extends JFrame {

private JTextField jtfFirst;
private JTextField jtfSecond;
private JTextField jtfThird;
private JTextField jtfResult;

public ExampleFrame() {
    super();
    setLayout(new GridBagLayout());

    GridBagConstraints c = new GridBagConstraints();
    c.insets = new Insets(5, 5, 5, 5);
    c.gridwidth = 2;
    c.gridx = 0;
    c.gridy = 0;
    add(new JLabel("Sum Numbers"),c);

    c.gridwidth = 1;
    c.anchor = GridBagConstraints.WEST;
    c.gridy = 1;
    add(new JLabel("Text1"),c);

    c.gridy =2;
    add(new JLabel("Text2"),c);

    c.gridy =3;
    add(new JLabel("Text3"),c);

    c.gridy =4;
    add(new JLabel("Result"),c);

    c.gridy =5;
    JButton b1 = new JButton("b1");
    b1.addActionListener(getActionB1Listener());
    add(b1,c);

    c.gridx = 1;
    c.gridy = 1;
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 1;
    add(jtfFirst = new JTextField(),c);

    c.gridy = 2;
    add(jtfSecond = new JTextField(),c);

    c.gridy = 3;
    add(jtfThird = new JTextField(),c);

    c.gridy = 4;
    add(jtfResult = new JTextField(),c);
    JButton b2 = new JButton("b2");
    b2.addActionListener(getActionB2Listener());

    c.fill = GridBagConstraints.NONE;
    c.weightx = 0;
    c.gridy = 5;
    add(b2,c);
    pack();
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);

}


private ActionListener getActionB1Listener() {
    return new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            System.out.println("b1 clicked");

        }
    };
}

private ActionListener getActionB2Listener() {
    return new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            System.out.println("b2 clicked");

        }
    };
}


public static void main(String... s) {
    new ExampleFrame();
}
}

And never do that if you use MouseListener only for clicks : 如果您仅将MouseListener用于点击,那么永远不要这样做:

 jbtAdd.addMouseListener(new List_ButtonADD());
 jbtClear.addMouseListener(new List_ButtonCLEAR());

MouseListener uses for another purposes. MouseListener用于其他目的。 Instead of that use ActionListener it uses for button actions. 而不是使用ActionListener它用于按钮操作。

在此输入图像描述

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

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