GUI程序不是从BlueJ开始的

时间:2014-02-20 15:34:08

标签: java swing constructor bluej

我尝试制作我的第一个GUI程序,我是Java的大量菜鸟,只是按照教程启动并运行。我逐行复制了下面的代码,试图了解发生了什么,总的来说我是。但是该程序无法在BlueJ中运行。 VM永远不会开始。

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;



public class GUI{
    public void GUI(){
       JFrame guiFrame1= new JFrame();
       guiFrame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       guiFrame1.setTitle("First GUI");
       guiFrame1.setSize(300,250);
       guiFrame1.setLocationRelativeTo(null);

        String[] fruitOptions = {"Apple", "Apricot", "Banana"
                ,"Cherry", "Date", "Kiwi", "Orange", "Pear", "Strawberry"};

        //Options for the JList
        String[] vegOptions = {"Asparagus", "Beans", "Broccoli", "Cabbage"
                , "Carrot", "Celery", "Cucumber", "Leek", "Mushroom"
                , "Pepper", "Radish", "Shallot", "Spinach", "Swede"
                , "Turnip"};


       //fruits
       final JPanel comboPanel= new JPanel();
       JLabel comboLbl= new JLabel("Fruits");
       JComboBox fruits = new JComboBox(fruitOptions);
       comboPanel.add(comboLbl);
       comboPanel.add(fruits);

       //veg
       final JPanel listPanel= new JPanel();
       listPanel.setVisible(false);
       JLabel listLbl1= new JLabel("Vegetables:");
       JList vegs= new JList (vegOptions);
       vegs.setLayoutOrientation(JList.HORIZONTAL_WRAP);

       listPanel.add(listLbl1);
       listPanel.add(vegs);

       //button 
       JButton vegFruitBut = new JButton("Fruit or Veg");

       vegFruitBut.addActionListener(new ActionListener()
       {
           @Override
           public void actionPerformed(ActionEvent event)
           {
               listPanel.setVisible(!listPanel.isVisible());
               comboPanel.setVisible(!comboPanel.isVisible());
           }

        });

       guiFrame1.add(comboPanel, BorderLayout.NORTH);
       guiFrame1.add(listPanel, BorderLayout.CENTER);
       guiFrame1.add(vegFruitBut,BorderLayout.SOUTH);
       guiFrame1.setVisible(true);


        }

    public static void main(String args[]){
        GUI s= new GUI();

    }
}

1 个答案:

答案 0 :(得分:3)

方法有返回类型,构造函数没有 - 删除void关键字,以便可以调用Swing代码

public GUI() {
       ^

阅读:Providing Constructors for Your Classes

相关问题