JFrame在Java Swing GUI

时间:2015-06-04 23:37:58

标签: java eclipse swing

我正在使用的书中说这段代码应该显示框架,并在框架内显示一个列表和至少OK按钮。
当我编译时,我只看到框架名称为'Event Object'的框架 框架内部是空白的。

import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;

public class EventObject extends JFrame
{
    private JList list;
    private DefaultListModel model;

    public EventObject()
    {
        setTitle("Event Object");
        JPanel panel = new JPanel();
        panel.setLayout(null);

        model = new DefaultListModel();
        list = new JList(model);
        list.setBounds(150, 30, 220, 150);
        JButton ok = new JButton("Ok");
        ok.setBounds(30, 35, 80, 25);
        ok.addActionListener( new ActionListener () 
        {
            public void actionPerformed(ActionEvent event)
            {
                Locale locale = Locale.getDefault();
                Date date = new Date();
                String s = DateFormat.getTimeInstance(DateFormat.SHORT, locale).format(date);
                if ( !model.isEmpty() )
                    model.clear();
                    if (event.getID() == ActionEvent.ACTION_PERFORMED)
                        model.addElement(" Event Id: ACTION_PERFORMED");
                    model.addElement(" Time: " + s);
                    String source = event.getSource().getClass().getName();
                    model.addElement(" Source: " + source);
                    int mod = event.getModifiers();
                    StringBuffer buffer = new StringBuffer(" Modifiers: ");
                    if ((mod & ActionEvent.ALT_MASK) > 0)
                        buffer.append("Alt ");
                    if ((mod & ActionEvent.SHIFT_MASK) > 0)
                        buffer.append("Shift ");
                    if ((mod & ActionEvent.META_MASK) > 0)
                        buffer.append("Meta ");
                    if ((mod & ActionEvent.CTRL_MASK) > 0)
                        buffer.append("Ctrl ");
                    model.addElement(buffer);
                }
        });
        panel.add(ok);
        panel.add(list);

        setSize(500, 500);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main (String[] args)
    {
        new EventObject();
    }
}

2 个答案:

答案 0 :(得分:3)

它是因为您只创建了一些组件,但没有向EventObject添加任何内容(扩展了JFrame)。尝试添加:

add(panel);
例如,在.setSize()之前

,它应该有用。

答案 1 :(得分:1)

尝试使用此代码。

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;

public class EventObject extends JFrame
{
private JList list;
private DefaultListModel model;

public EventObject()
{
    setTitle("Event Object");
    JPanel panel = new JPanel();
    panel.setLayout(null);
    add(panel);

    model = new DefaultListModel();
    list = new JList(model);
    list.setBounds(150, 30, 220, 150);
    JButton ok = new JButton("Ok");
    ok.setBounds(30, 35, 80, 25);
    ok.addActionListener( new ActionListener () 
    {
        public void actionPerformed(ActionEvent event)
        {
            Locale locale = Locale.getDefault();
            Date date = new Date();
            String s = DateFormat.getTimeInstance(DateFormat.SHORT, locale).format(date);
            if ( !model.isEmpty() )
                model.clear();
                if (event.getID() == ActionEvent.ACTION_PERFORMED)
                    model.addElement(" Event Id: ACTION_PERFORMED");
                model.addElement(" Time: " + s);
                String source = event.getSource().getClass().getName();
                model.addElement(" Source: " + source);
                int mod = event.getModifiers();
                StringBuffer buffer = new StringBuffer(" Modifiers: ");
                if ((mod & ActionEvent.ALT_MASK) > 0)
                    buffer.append("Alt ");
                if ((mod & ActionEvent.SHIFT_MASK) > 0)
                    buffer.append("Shift ");
                if ((mod & ActionEvent.META_MASK) > 0)
                    buffer.append("Meta ");
                if ((mod & ActionEvent.CTRL_MASK) > 0)
                    buffer.append("Ctrl ");
                model.addElement(buffer);
            }
    });
    panel.add(ok);
    panel.add(list);

    setSize(500, 500);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
}

public static void main (String[] args)
{
    new EventObject();
}
}