GUI框架/按钮

时间:2015-08-13 04:14:49

标签: java swing compiler-errors

我试图用两个按钮提交和取消来创建一个框架。我是java的新手,无法运行它。我尝试了一些不同的方法,但我无法完成任何操作。

import javax.swing.*;
public static void main(String[] args){
    JFrame frame=new JFrame("Select One";)
    //add a button
    JButton jbtSubmit=new JButton ("Submit");{
    frame.add(JbtSubmit);

    JButton jbtCancel=new JButton ("Cancel");{
    frame.add(JbtCancel);

    frame.setsize(300,400);// sets frame size
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //frame.getContentPane().add(emptyLabel, BorderLayout.CENTER)//components to put in frame
    frame.sertLocationRelativeTo(null);//Centers frame
    frame.setVisable(true);//display the frame
    }
}





my errors

Description Resource    Path    Location    Type
Syntax error on tokens, delete these tokens yButton.java    /ICS141/src line 3  Java Problem

Syntax error on token "JFrame", ( expected after this token yButton.java    /ICS141/src line 3  Java Problem
Syntax error, insert ")" to complete Arguments  yButton.java    /ICS141/src line 5  Java Problem

Build path specifies execution environment CDC-1.1/Foundation-1.1. There are no JREs installed in the workspace that are strictly compatible with this environment.     ICS141      Build path  JRE System Library Problem
Syntax error, insert ")" to complete ClassInstanceCreationExpression    yButton.java    /ICS141/src line 5  Java Problem
Syntax error, insert "]" to complete ArrayAccess    yButton.java    /ICS141/src line 2  Java Problem

Build path specifies execution environment CDC-1.1/Foundation-1.1. There are no JREs installed in the workspace that are strictly compatible with this environment.     two     Build path  JRE System Library Problem
Syntax error on token "]", invalid (    yButton.java    /ICS141/src line 2  Java Problem

Syntax error, insert "enum Identifier" to complete EnumHeader   yButton.java    /ICS141/src line 2  Java Problem

Build path specifies execution environment CDC-1.1/Foundation-1.1. There are no JREs installed in the workspace that are strictly compatible with this environment.     Eclipse     Build path  JRE System Library Problem
Syntax error, insert ")" to complete SingleMemberAnnotation yButton.java    /ICS141/src line 2  Java Problem

Syntax error on tokens, AnnotationName expected instead yButton.java    /ICS141/src line 2  Java Problem

2 个答案:

答案 0 :(得分:1)

请参阅我在您的代码中所做更改的评论:

    import java.awt.Dimension;
    import java.awt.FlowLayout;

    import javax.swing.JButton;
    import javax.swing.JFrame;

    //added class deceleration : see @MadProgrammer comment
    public class Test {

        public static void main(String[] args){


            JFrame frame= new JFrame("Select One"); //moved ;
            frame.getContentPane().setLayout(new FlowLayout());//added layout manager

            //add a button
            JButton jbtSubmit=new JButton ("Submit"); //removed {
            frame.getContentPane().add(jbtSubmit);

            JButton jbtCancel= new JButton ("Cancel");//removed {
            frame.getContentPane().add(jbtCancel); //changed JbtCancel to

            frame.setPreferredSize(new Dimension(300,400)) ;// changed to setPreferredSize
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            //frame.getContentPane().add(emptyLabel, BorderLayout.CENTER)//components to put in frame
            frame.setLocationRelativeTo(null);// changed sertLocationRelativeTo(null) to setLocationRelativeTo
            frame.pack(); //added
            frame.setVisible(true);//changed setVisable tp setVisible
            }

    }

答案 1 :(得分:0)

Java是一种面向对象的语言。您正在尝试以面向过程的方式执行您的程序。我认为您必须从基本的hello世界开始