设置文本旁边的输入框(Java)

时间:2014-11-30 19:54:58

标签: java

所以我想在文本旁边找到我的鱼三明治的文本输入框。它一直在汉堡包输入框旁边按下。我似乎无法接下来。这是代码。 这是截图

screenshot

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package foodproject2;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
/**
 *
 * @author Travis
 */
public class Menu1 extends javax.swing.JFrame {
private Container contents;
   private JLabel name, courseNum, welcome, prompt, chickP, fishP, burgerP;
   private JLabel chargeSand, chargeService, totalBill;
   private JTextField numChick, numFish, numBurger;
   private JButton compute;

   public Menu1()
   {
      super("Travis' Menu");
      contents = getContentPane();
      contents.setLayout(new FlowLayout());

      name=new JLabel("Programmer: Travis Easton");
      courseNum=new JLabel("ITSD424");

      welcome=new JLabel("Welcome To Travis' Sandwich Shop");
      prompt=new JLabel("Enter number of Sandwiches for each; 0 if none");

      chickP=new JLabel("Chicken Sandwiches @ $4.99 each");
      chickP.setForeground(Color.BLACK);
      numChick=new JTextField(3);

      fishP=new JLabel("Salmon Sandwiches @ $4.99 each               ");
      fishP.setForeground(Color.BLACK);
      numFish=new JTextField(3);



          burgerP=new JLabel("Hamburger @ $4.99 each");
      burgerP.setForeground(Color.BLACK);
      numBurger=new JTextField(3);

      chargeSand = new JLabel("Charge for Sandwiches = $");
      chargeService = new JLabel("Charge for Service = $");
      totalBill= new JLabel("Total Bill = $");

      chargeSand = new JLabel("Sandwich cost");
      chargeService = new JLabel("Tax");
      totalBill = new JLabel("Total");

      compute = new JButton("Bill Total");

      contents.add(name);
      contents.add(courseNum);
      contents.add(welcome);
      contents.add(prompt);
      contents.add(chickP);
      contents.add(numChick);
      contents.add(fishP);
          contents.add(burgerP);
      contents.add(numFish);
          contents.add(numBurger);
      contents.add(chargeSand);
      contents.add(chargeService);
      contents.add(totalBill);
      contents.add(chargeSand);
      contents.add(chargeService);
      contents.add(totalBill);
      contents.add(compute);

      ButtonHandler bh = new ButtonHandler();
      compute.addActionListener(bh);

      setSize(400,400);
      setVisible(true);
   }

   private class ButtonHandler implements ActionListener
   {
      public void actionPerformed(ActionEvent e) {
          try
          {
             double one = Double.parseDouble(numChick.getText());
             double two = Double.parseDouble(numFish.getText());
             double three = Double.parseDouble(numBurger.getText());

             // Calculations for determining total price of bill
             double orderAmount = (one*4.99)+(two*4.99)+(three*4.99);
             double serviceAmount = (orderAmount)*.15;
             double totalAmount = (orderAmount+serviceAmount);

             // Now to display the charges
             chargeSand.setText(new Double(orderAmount).toString());
             chargeService.setText(new Double(serviceAmount).toString());
             totalBill.setText(new Double(totalAmount).toString());
          }

          catch(NumberFormatException ex)
          {
             numChick.setText("0");
             numFish.setText("0");
             numBurger.setText("0");
             totalBill.setText("0");
          }
        }
   }
    /**
     * Creates new form Menu
     */
     {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Menu1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Menu1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Menu1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Menu1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Menu1().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}

2 个答案:

答案 0 :(得分:0)

在您的代码中,传递给fishP JLabel的字符串在末尾包含空格。 我认为应该是:

fishP=new JLabel("Salmon Sandwiches @ $4.99 each");

还有一个变化:

contents.add(chickP);
contents.add(numChick);
contents.add(fishP);
contents.add(numFish);
contents.add(burgerP);
contents.add(numBurger);

答案 1 :(得分:0)

当您以错误的顺序添加内容时:

contents.add(chickP);
contents.add(numChick);
contents.add(fishP);
contents.add(burgerP);
contents.add(numFish);
contents.add(numBurger);

向上移动numFish。