GUI中的按钮,文本字段,标签布局

时间:2016-04-30 08:06:19

标签: java swing layout

我知道这是很多代码。我试图得到它,所以布局更好,我无法得到任何工作。我想得到它所以HelpButton是右下角,HelpField是底部中心,Clear按钮是左下角。

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import java.io.*;

public class GUI extends JFrame { //open class

private final JTextField HelpField, tField, uField, xField, yField, tPlusuField, tMinusuField, uDividedtField, uTimestField, xPlusyField, xMinusyField, yDividedxField, yTimesxField;

private final JLabel tLabel, uLabel, xLabel, yLabel, tPlusuLabel, tMinusuLabel, uDividedtLabel, uTimestLabel, xPlusyLabel, xMinusyLabel, yDividedxLabel, yTimesxLabel; 

private final JButton doSimpleMath, Reset, HelpButton;

 public GUI() { //open 

   super("GUI");

   setLayout(new FlowLayout());
   JPanel panLeft = new JPanel(new BorderLayout());

   tLabel = new JLabel("Enter value you for t:");
   tField = new JTextField(2);
   add(tLabel);
   add(tField);


   uLabel = new JLabel("Enter value you for u:");
   uField = new JTextField(2);
   add(uLabel);
   add(uField);


   xLabel = new JLabel("Enter value you for x:");
   xField = new JTextField(2);
   add(xLabel);
   add(xField);


   yLabel = new JLabel("Enter value you for y:");
   yField = new JTextField(2);
   add(yLabel);
   add(yField);


   doSimpleMath = new JButton("Do Simple Math");
   add(doSimpleMath);

   Reset = new JButton("Reset");
   add(Reset);

   HelpButton = new JButton("Help");
   add(HelpButton);


   ButtonHandler handler = new ButtonHandler();
   doSimpleMath.addActionListener(handler);
   Reset.addActionListener(handler);
   HelpButton.addActionListener(handler);

   HelpField = new JTextField(40);
   add(HelpField);
   HelpField.setEditable(false);


   tPlusuLabel = new JLabel("t + u =");
   tPlusuField = new JTextField(5);
   tPlusuField.setEditable(false);
   add(tPlusuLabel);
   add(tPlusuField);

   tMinusuLabel = new JLabel("t - u =");
   tMinusuField = new JTextField(5);
   tMinusuField.setEditable(false);
   add(tMinusuLabel);
   add(tMinusuField);

   uDividedtLabel = new JLabel("u / t =");
   uDividedtField = new JTextField(5);
   uDividedtField.setEditable(false);
   add(uDividedtLabel);
   add(uDividedtField);

  uTimestLabel = new JLabel("u * t =");
  uTimestField = new JTextField(5);
  uTimestField.setEditable(false);
  add(uTimestLabel);
  add(uTimestField);

  xPlusyLabel = new JLabel("x + y =");
  xPlusyField = new JTextField(5);
  xPlusyField.setEditable(false);
  add(xPlusyLabel);
  add(xPlusyField);

  xMinusyLabel = new JLabel("x - y =");
  xMinusyField = new JTextField(5);
  xMinusyField.setEditable(false);
  add(xMinusyLabel);
  add(xMinusyField);

  yDividedxLabel = new JLabel("y / x =");
  yDividedxField = new JTextField(5);
  yDividedxField.setEditable(false);
  add(yDividedxLabel);
  add(yDividedxField);

  yTimesxLabel = new JLabel("y * x =");
  yTimesxField = new JTextField(5);
  yTimesxField.setEditable(false);
  add(yTimesxLabel);
  add(yTimesxField);

 } // close

 private class ButtonHandler implements ActionListener { //open ButtonHandler

   @Override

   public void actionPerformed(ActionEvent event) { //open actionPerformed

     if(event.getSource() == doSimpleMath){ //close if

       int t = Integer.parseInt(tField.getText()); //convert String to int 
       int u = Integer.parseInt(uField.getText());
       int x = Integer.parseInt(xField.getText());
       int y = Integer.parseInt(yField.getText());

       int tSumu = t + u; //define values
       int tMinusu = t - u;
       int uDividedt = t / u;
       int uTimest = u * t;
       int xSumy = x + y;
       int xMinusy = x - y;
       int yDividedx = y / x;
       int yTimesx = y * x;

       tPlusuField.setText(tSumu + " "); //disply output
       tMinusuField.setText(tMinusu + " ");  
       uDividedtField.setText(uDividedt + " "); 
       uTimestField.setText(uTimest + " "); 
       xPlusyField.setText(xSumy + " ");
       xMinusyField.setText(xMinusy + " ");
       yDividedxField.setText(yDividedx + " ");
       yTimesxField.setText(yTimesx + " ");

     } //close if 

     if (event.getSource() == Reset){ //open if

       tPlusuField.setText(""); //disply output
       tMinusuField.setText("");  
       uDividedtField.setText(""); 
       uTimestField.setText(""); 
       xPlusyField.setText("");
       xMinusyField.setText("");
       yDividedxField.setText("");
       yTimesxField.setText("");
       tField.setText("");
       uField.setText("");
       xField.setText("");
       yField.setText("");
       HelpField.setText("");

     } //close if 

     if (event.getSource() == HelpButton) { //open if 

       HelpField.setText("You Need Help.");

     } //close if 

   } // close actionPerformed

 }//close ButtonHandler

} //关闭课程

3 个答案:

答案 0 :(得分:2)

请记住,您可以使用任意数量的容器和布局,这些容器和布局可以组合在一起以生成复杂的布局,例如:

Form

public GUI() { //open 

    super("GUI");

    JPanel panLeft = new JPanel(new BorderLayout());


    JPanel fields = new JPanel();
    JPanel actions = new JPanel();

    tLabel = new JLabel("Enter value you for t:");
    tField = new JTextField(2);
    fields.add(tLabel);
    fields.add(tField);

    uLabel = new JLabel("Enter value you for u:");
    uField = new JTextField(2);
    fields.add(uLabel);
    fields.add(uField);

    xLabel = new JLabel("Enter value you for x:");
    xField = new JTextField(2);
    fields.add(xLabel);
    fields.add(xField);

    yLabel = new JLabel("Enter value you for y:");
    yField = new JTextField(2);
    fields.add(yLabel);
    fields.add(yField);

    doSimpleMath = new JButton("Do Simple Math");
    fields.add(doSimpleMath);

    HelpField = new JTextField(40);
    add(HelpField);
    HelpField.setEditable(false);

    Reset = new JButton("Reset");
    add(Reset);

    HelpButton = new JButton("Help");
    add(HelpButton);

    actions.add(Reset);
    actions.add(HelpField);
    actions.add(HelpButton);

    tPlusuLabel = new JLabel("t + u =");
    tPlusuField = new JTextField(5);
    tPlusuField.setEditable(false);
    fields.add(tPlusuLabel);
    fields.add(tPlusuField);

    tMinusuLabel = new JLabel("t - u =");
    tMinusuField = new JTextField(5);
    tMinusuField.setEditable(false);
    fields.add(tMinusuLabel);
    fields.add(tMinusuField);

    uDividedtLabel = new JLabel("u / t =");
    uDividedtField = new JTextField(5);
    uDividedtField.setEditable(false);
    fields.add(uDividedtLabel);
    fields.add(uDividedtField);

    uTimestLabel = new JLabel("u * t =");
    uTimestField = new JTextField(5);
    uTimestField.setEditable(false);
    fields.add(uTimestLabel);
    fields.add(uTimestField);

    xPlusyLabel = new JLabel("x + y =");
    xPlusyField = new JTextField(5);
    xPlusyField.setEditable(false);
    fields.add(xPlusyLabel);
    fields.add(xPlusyField);

    xMinusyLabel = new JLabel("x - y =");
    xMinusyField = new JTextField(5);
    xMinusyField.setEditable(false);
    fields.add(xMinusyLabel);
    fields.add(xMinusyField);

    yDividedxLabel = new JLabel("y / x =");
    yDividedxField = new JTextField(5);
    yDividedxField.setEditable(false);
    fields.add(yDividedxLabel);
    fields.add(yDividedxField);

    yTimesxLabel = new JLabel("y * x =");
    yTimesxField = new JTextField(5);
    yTimesxField.setEditable(false);
    fields.add(yTimesxLabel);
    fields.add(yTimesxField);


    add(fields);
    add(actions, BorderLayout.SOUTH);

} // close

请查看A Visual Guide to Layout ManagersLaying Out Components Within a Container了解详情

答案 1 :(得分:0)

在您的JFrame中添加BorderLayout。在SOUTH中放了另一个BorderLayout。在最后一个BorderLayout中,将您的组件放在WESTCENTEREAST中。

答案 2 :(得分:-1)

尝试在setLayout中使用(new AbsoluteLayout())并为每个组件分配x和y坐标。它会很有帮助..你可以把你的组件放在任何你想要的地方。