佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 709|回复: 4

不知那里错了。。。。。(JAVA)

[复制链接]
发表于 21-2-2006 03:52 PM | 显示全部楼层 |阅读模式
当我开始写的时候,一切没有问题。。。。。
可是compile几次后。。。。我的button不见了。。。
我找了一天也找不到。。。试写新的还是一样。。。。
就是象interface不能用。。。。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
                  
public class DSAP extends JFrame implements ActionListener
{
    public static void main(String[] args)
    {
       DSAP g = new DSAP();
    }
    public DSAP ()
    {            
        JFrame newFrame = new JFrame();
        newFrame.setTitle("DSAP - Asssignment");
        newFrame.setSize(800,600); //x,y      
        newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JMenuBar menu = new JMenuBar();
        newFrame.setJMenuBar(menu);
        JMenu File = new JMenu("File");
        File.add(new JMenuItem("Add New Record"));
        File.add(new JMenuItem("Open Record"));
        File.addSeparator();
        JMenuItem exit = new JMenuItem("Exit");
        File.add(exit);
        
        //File.add(new JMenuItem("Exit"));
        
        JMenu Edit = new JMenu("Edit");
        Edit.add(new JMenuItem("Update Record"));
        Edit.add(new JMenuItem("Delete Record"));      
        
        menu.add(File);
        menu.add(Edit);
        
     
        JButton btnPress = new JButton("HELOOOOOOOoo");
        Container C = getContentPane();
        JPanel F = new JPanel();
        C.setLayout(new BorderLayout(5,10));
        F.setLayout(new GridLayout(3,1));
        C.add(btnPress,BorderLayout.NORTH);
        newFrame.setVisible(true);
        exit.addActionListener(this);
      
    }
   
    public void actionPerformed(ActionEvent e)
    {
        String actionCommand = e.getActionCommand();
        if(e.getSource() instanceof JMenuItem)
        {
            if("Exit".equals(actionCommand))
                System.exit(0);
        }
    }
   
}


谁可一帮我看看。。。。。
谢谢个位的大恩大德。。。。。。。。
回复

使用道具 举报


ADVERTISEMENT

发表于 21-2-2006 07:41 PM | 显示全部楼层
swing刚开始是有点吃力,code多了就会好..
你的code 有点乱, try to standardize it! 不然你也会失去方向。。。

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class DSAP extends JFrame implements ActionListener{
    private JMenuBar menuBar;
    private JMenu fileMenu;
    private JMenu editMenu;
   
    private JMenuItem addNewRecordMenuItem;
    private JMenuItem openRecordMenuItem;
    private JMenuItem exitMenuItem;
   
    private JMenuItem updateMenuItem;
    private JMenuItem deleteMenuItem;
   
    private JButton pressButton;
   
    public DSAP (){
        initComponents();
    }
   
    private void initComponents() {   
        menuBar = new JMenuBar();
        
        fileMenu = new JMenu("File");
        addNewRecordMenuItem = new JMenuItem("Add New Record");
        openRecordMenuItem = new JMenuItem("Open Record");
        
        exitMenuItem = new JMenuItem("Exit");
        exitMenuItem.addActionListener(this);
        
        fileMenu.add(addNewRecordMenuItem);
        fileMenu.add(openRecordMenuItem);
        fileMenu.addSeparator();
        fileMenu.add(exitMenuItem);
        
        editMenu =  new JMenu("Edit");

        updateMenuItem = new JMenuItem("Update Record");
        deleteMenuItem = new JMenuItem("Delete Record");
        
        editMenu.add(updateMenuItem);
        editMenu.add(deleteMenuItem);
        
        menuBar.add(fileMenu);
        menuBar.add(editMenu);
        
        pressButton = new JButton("HELOOOOOOOoo");
        
        this.setLayout(new BorderLayout(5,10));
        
        this.add(pressButton,BorderLayout.SOUTH);
      
        this.setTitle("DSAP - Asssignment");
        this.setSize(800,600); //x,y      
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
        this.setJMenuBar(menuBar);
    }

    public void actionPerformed(ActionEvent e){
        String actionCommand = e.getActionCommand();
        
        if(e.getSource() instanceof JMenuItem){
            if("Exit".equals(actionCommand)){
                System.exit(0);
            }
        }
    }
   
    public static void main(String[] args){
        DSAP g = new DSAP();
        g.setVisible(true);
    }
}
回复

使用道具 举报

 楼主| 发表于 21-2-2006 09:07 PM | 显示全部楼层
谢谢。。。。不过我试用你的code时。。。下面的error出现。。。
能解释为什么吗??

还有"this"的用意是什么??

java.lang.Error: Do not use DSAP.setLayout() use DSAP.getContentPane().setLayout() instead
        at javax.swing.JFrame.createRootPaneException(JFrame.java:465)
        at javax.swing.JFrame.setLayout(JFrame.java:531)
        at DSAP.initComponents(DSAP.java:57)
        at DSAP.<init>(DSAP.java:26)
        at DSAP.main(DSAP.java:79)
Exception in thread "main"
Java Result: 1
回复

使用道具 举报

发表于 21-2-2006 09:42 PM | 显示全部楼层
原帖由 lawTY 于 21-2-2006 09:07 PM 发表
谢谢。。。。不过我试用你的code时。。。下面的error出现。。。
能解释为什么吗??

还有"this"的用意是什么??

java.lang.Error: Do not use DSAP.setLayout() use DSAP.getContentPane().setLayo ...


那是因为我用jdk1.5(也叫 jdk 5.0)而你用的是之前的版本(e.g jdk1.4).

把这俩东东改改就行!

    getContentPane().setLayout(new BorderLayout(5,10));
        
    getContentPane().add(pressButton,BorderLayout.SOUTH);

this 是用来refer to current instance, 还有一个叫super, super 是用来refer to super class.如果你没
override super class 的 method, 这个this 可以有也可无.

例如:
     this.getContentPane().setLayout(new BorderLayout(5,10));
或者
     getContentPane().setLayout(new BorderLayout(5,10));

那this 和super有什么分别呢?我们来个例子吧..

public class SuperClass {
        public void aMethod(){
                System.out.println("SuperClass' aMethod is called");
        }       
}

public class SubClass extends SuperClass{
        public void callAllMethods(){
                this.aMethod(); //SubClass's aMethod is called
                aMethod();//SubClass's aMethod is called
                super.aMethod(); //SuperClass' aMethod is called
        }
       
        //overriden method
        public void aMethod(){
                System.out.println("SubClass's aMethod is called");
        }

       
        public static void main(String[] args) {
                SubClass subClass = new SubClass();
               
                subClass.callAllMethods();
        }
}

[ 本帖最后由 黑木头 于 21-2-2006 09:51 PM 编辑 ]
回复

使用道具 举报

 楼主| 发表于 21-2-2006 11:16 PM | 显示全部楼层
谢谢黑木头。。。让我明白很多了。。。。。。
多谢。。。。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 22-9-2024 01:16 AM , Processed in 0.103600 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表