有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaJTextField在Eclipse中一切看起来都正常,但不会编译

有人能告诉我这个代码有什么问题吗? 我需要它弹出一个框架,用特定字段填充框架,一个名字字段和一个入口字段,并且在网格底部显示一个按钮。 欢迎任何帮助,谢谢

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

    public class FrameDemo 
    //To create the gui and show it.
    {   
    public static void createAndShowGUI()
    {
        //create and set up the window.
        JFrame frame = new JFrame("RungeKutta");
        GridLayout first = new GridLayout(1,14);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create, name and Populate TextField   
        JTextField PL = new JTextField("Pendulum Length", 20);
        //Set TextField to Uneditable. Each will have Empty Field Below For Variables   
        PL.setEditable(false);  
        //Set Textfield for user entered dat
        JTextField PLv = new JTextField();
        //Allow handler for user input on Empty Textfield?

        JTextField AD = new JTextField("Angular Displacement", 20);
        AD.setEditable(false);
            JTextField ADv = new JTextField();

        JTextField AV = new JTextField("Angular Velocity", 20);
        AV.setEditable(false);
        JTextField Avv = new JTextField();

        JTextField TS= new JTextField("Time Steps", 20);
        TS.setEditable(false);
        JTextField TSv = new JTextField();

        JTextField MT = new JTextField("Max Time", 20);
        MT.setEditable(false);
        JTextField MTv = new JTextField();

        JTextField V = new JTextField("Viscosity (0-1)", 20);
        V.setEditable(false);
        JTextField Vv = new JTextField();

        //Create Button to Restart
    JButton BNewGraph = new JButton("Draw New Graph"); //Button to restart entire drawing process
        JLabel emptyLabel = new JLabel("");
    emptyLabel.setPreferredSize(new Dimension(600,500));
    frame.getContentPane().add(PL, first);
    frame.getContentPane().add(PLv, first);
    frame.getContentPane().add(AD, first);
    frame.getContentPane().add(ADv, first);
    frame.getContentPane().add(AV, first);
    frame.getContentPane().add(Avv, first);
    frame.getContentPane().add(TS, first);
    frame.getContentPane().add(TSv, first);
    frame.getContentPane().add(MT, first);
    frame.getContentPane().add(MTv, first);
    frame.getContentPane().add(V, first);
    frame.getContentPane().add(Vv, first);
    frame.getContentPane().add(BNewGraph, first);
    //display the window
    frame.pack();
    frame.setVisible(true);
    }

    public static void main(String[] args)
    {
        //add job to event scheduler
        //create and show GUI
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
            }
    }

共 (1) 个答案

  1. # 1 楼答案

    这不是您使用GridLayout的方式:

    frame.getContentPane().add(PL, first);
    

    而是使用布局管理器设置容器的布局:

    frame.getContentPane().setLayout(first);
    

    然后将组件添加到容器中:

    frame.getContentPane().add(PL);
    frame.getContentPane().add(PLv);
    frame.getContentPane().add(AD);
    frame.getContentPane().add(ADv);
    frame.getContentPane().add(AV);
    frame.getContentPane().add(Avv);
    frame.getContentPane().add(TS);
    frame.getContentPane().add(TSv);
    frame.getContentPane().add(MT);
    frame.getContentPane().add(MTv);
    // and so on for all the components.
    

    您需要阅读关于如何使用GridLayout的教程,您可以在这里找到:GridLayout Tutorial

    另外,请注意:

    frame.getContentPane().add(PL);
    

    可以缩短为:

    frame.add(PL);
    

    您还需要学习Java命名约定:类名以大写字母开头,所有方法和变量以小写字母开头。另外,还要避免使用pl或plv、ad或adv等变量名,而是使用稍微长一点且有意义的名称,这些名称会使代码自我注释。因此,代替AD,考虑JTTrror的名称的角位移字段。


    我自己,我会使用GridBagLayout并做一些类似的事情:

    import java.awt.BorderLayout;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import java.awt.event.ActionEvent;
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.Map;
    import java.util.Set;
    import javax.swing.*;
    
    public class GuiDemo extends JPanel {
    
       // or better   use an enum for this
       public static final String[] FIELD_LABELS = {
          "Pendulum Length", "Angular Displacement", "Angular Velocity",
          "Time Steps", "Max Time", "Viscocity (0-1)"
       };
       private static final int TEXT_FIELD_COLUMNS = 10;
       private static final int GAP = 3;
       private static final Insets RIGHT_GAP_INSETS = new Insets(GAP, GAP, GAP, 3 * GAP);
       private static final Insets BALANCED_INSETS = new Insets(GAP, GAP, GAP, GAP);
       private Map<String, JTextField> labelFieldMap = new HashMap<>();
    
       public GuiDemo() {
          JPanel labelFieldPanel = new JPanel(new GridBagLayout());
          int row = 0;
    
          // to make sure that no focusAccelerator is re-used
          Set<Character> focusAccelSet = new HashSet<>();
          for (String fieldLabelLText : FIELD_LABELS) {
             JLabel fieldLabel = new JLabel(fieldLabelLText);
             JTextField textField = new JTextField(TEXT_FIELD_COLUMNS);
             labelFieldPanel.add(fieldLabel, getGbc(row, 0));
             labelFieldPanel.add(textField, getGbc(row, 1));
             labelFieldMap.put(fieldLabelLText, textField);
    
             for (char c : fieldLabelLText.toCharArray()) {
                if (!focusAccelSet.contains(c)) {
                   textField.setFocusAccelerator(c);
                   fieldLabel.setDisplayedMnemonic(c);
                   focusAccelSet.add(c);
                   break;
                }
             }
    
             row++;
          }
          JButton button = new JButton(new DrawGraphAction("Draw New Graph"));
    
          labelFieldPanel.add(button, getGbc(row, 0, 2, 1));
    
          setLayout(new BorderLayout(GAP, GAP));
          add(labelFieldPanel, BorderLayout.CENTER);
       }
    
       private class DrawGraphAction extends AbstractAction {
          public DrawGraphAction(String name) {
             super(name);
             int mnemonic = (int) name.charAt(0);
             putValue(MNEMONIC_KEY, mnemonic);
          }
    
          @Override
          public void actionPerformed(ActionEvent e) {
             // TODO calculation method
    
          }
       }
    
       public static GridBagConstraints getGbc(int row, int column) {
          GridBagConstraints gbc = new GridBagConstraints();
          gbc.gridx = column;
          gbc.gridy = row;
          gbc.gridwidth = 1;
          gbc.gridheight = 1;
          gbc.fill = GridBagConstraints.HORIZONTAL;
          if (column == 0) {
             gbc.anchor = GridBagConstraints.LINE_START;
             gbc.fill = GridBagConstraints.BOTH;
             gbc.insets = RIGHT_GAP_INSETS;
          } else {
             gbc.anchor = GridBagConstraints.LINE_END;
             gbc.fill = GridBagConstraints.HORIZONTAL;
             gbc.insets = BALANCED_INSETS;
          }
    
          return gbc;
       }
    
       public static GridBagConstraints getGbc(int row, int column, int width, int height) {
          GridBagConstraints gbc = getGbc(row, column);
          gbc.gridwidth = width;
          gbc.gridheight = height;
          gbc.insets = BALANCED_INSETS;
          gbc.fill = GridBagConstraints.BOTH;
          return gbc;
       }
    
       private static void createAndShowGui() {
          JFrame frame = new JFrame("Gui Demo");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(new GuiDemo());
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }