需要GUI和Java swing的帮助

时间:2013-12-04 05:09:27

标签: java swing

这是我的下面的程序,我试图弄清楚我的主要方法应该在哪里。我已经看到了一些在程序最后实现它的例子,但主要与我的不同。

主要方法(待实施):

               public class JFrame
                   {
                  public static void main(String[] args)
                   {


                   JFrame frame = new JFrame(); 

                    frame.setTitle("Components File");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setVisible(true); 
                    }
                       }

我的计划

  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.JPanel;
  import javax.swing.JTextField;

  public class Lab_10 extends JFrame
  {

  private final double EARTHQUAKE_RATE= 8.0;
  private final int FRAME_WIDTH= 300;
  private final int FRAME_HEIGHT= 200;

  private JLabel rLabel;
  private JTextField eField;
  private JButton button;
  private JLabel earthLabel;

  public Lab_10()
  {
  JLabel earthLabel = new JLabel("Most structures fall");

  makeTextField();
  makeButton();
  makePanel();
  setSize(FRAME_WIDTH, FRAME_HEIGHT);
   }

  private void makeTextField()
   {
    JLabel rLabel = new JLabel("Richter");

    final int FIELD_WIDTH = 10;
    eField = new JTextField(FIELD_WIDTH);
    eField.setText("" + EARTHQUAKE_RATE);
    }


    class AddLabelListener implements ActionListener
     {
     public void actionPerformed(ActionEvent event)
      {
     earthLabel.setText("Most structures fall");
        }
       }

      private void makeButton()
       {
      JButton button = new JButton("Enter");

       ActionListener listener = new AddLabelListener();
       button.addActionListener(listener);
        }

       private void makePanel()
        {
       JPanel panel = new JPanel();
       panel.add(rLabel);
       panel.add(eField);
       panel.add(button);
       panel.add(earthLabel);
       add(panel);
        }
         }

更新的代码(正在编译但正在运行,但是因为它实现了一个空框架,因为它实现了一个空框架[从我的主要方法中猜测得多]):

  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.JPanel;
  import javax.swing.JTextField;

  public class Lab_10 extends JFrame
  {

  private final double EARTHQUAKE_RATE= 8.0;
  private final int FRAME_WIDTH= 300;
  private final int FRAME_HEIGHT= 200;

  private JLabel rLabel;
  private JTextField eField;
  private JButton button;
  private JLabel earthLabel;

  public Lab_10()
  {
  JLabel earthLabel = new JLabel("Most structures fall");

  makeTextField();
  makeButton();
  makePanel();
  setSize(FRAME_WIDTH, FRAME_HEIGHT);
   }

   private void makeTextField()
    {
   JLabel rLabel = new JLabel("Richter");

    final int FIELD_WIDTH = 10;
     eField = new JTextField(FIELD_WIDTH);
     eField.setText("" + EARTHQUAKE_RATE);
     }


         class AddLabelListener implements ActionListener
          {
         public void actionPerformed(ActionEvent event)
           {
         earthLabel.setText("Most structures fall");
            }
           }

          private void makeButton()
          {
          JButton button = new JButton("Enter");

          ActionListener listener = new AddLabelListener();
           button.addActionListener(listener);
            }

          private void makePanel()
           {
           JPanel panel = new JPanel();
          panel.add(rLabel);
          panel.add(eField);
          panel.add(button);
          panel.add(earthLabel);
          add(panel);
           }
          public static void main(String[] args){
          javax.swing.SwingUtilities.invokeLater(new Runnable(){
           @Override
             public void run()
          { JFrame frame = new JFrame(); 

            frame.setTitle("Components File");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             frame.setVisible(true); 
             }



             });
             }
             }

1 个答案:

答案 0 :(得分:1)

您的'main'可能看起来有所不同,因为您没有使用SwingUtilities.invokeLater(Runnable doRun)

嗯,简而言之,必须始终使用它。因此,修改您的代码并使用它:

SwingUtilities.invokeLater(new Runnable(){
   @Override
   public void run(){
      // copy-paste your main() code
   }
});  

另外,为什么您的班级名为JFrame?不要使用已经由Java类使用的名称。