当前位置: 代码迷 >> J2SE >> 请问一个java GUI的有关问题,十分感谢
  详细解决方案

请问一个java GUI的有关问题,十分感谢

热度:6431   发布时间:2013-02-25 00:00:00.0
请教一个java GUI的问题,十分感谢!
Java 达人,

我写了一个 Java程序。

Demo10 启动后,跟踪用户按键事件,如果有按下F3,则调用 GlobalVari_5.

问题是:在 Demo10启动的时候,GlobalVari_5 对应的 JFrame会闪现一下,然后消失。

请问,应该怎么修改下,可以使得在 Demo10启动的时候, GlobalVari_5 对应的 JFrame不要闪现。

PS:我试着把 GlobalVari_5.java里的 this.setVisible(true); 改成 this.setVisible(false);

这样后,在 Demo10启动的时候, GlobalVari_5 对应的 JFrame确实没有闪现,但是用户按下 F3后,

GlobalVari_5 对应的 JFrame里的右边的 JPane不见了...

请达人帮我看看是啥情况。十分感谢!


Demo10.java

Java code
import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import javax.swing.*; public class Demo10 extends JFrame implements KeyListener{    /**     *      */    private static final long serialVersionUID = 1L;    GlobalVari_5 gv = new GlobalVari_5();    public Demo10(){        this.setTitle("键盘事件处理");        this.setSize(300, 200);        this.setLocationRelativeTo(this);// 窗口居中        this.setVisible(true);        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 窗口关闭动作处理        this.addKeyListener(this);// 添加注册键盘事件监听器        this.gv.setVisible(false);    }    @Override    public void keyPressed(KeyEvent arg0){// 敲击    }    @Override    public void keyReleased(KeyEvent arg0) {// 按下        if (arg0.getKeyCode() == 114) {// 114 F3对应的编码            Demo10.print();            gv.setVisible(true);        }    }    @Override    public void keyTyped(KeyEvent arg0) {// 释放    }    public static void main(String[] args) {        new Demo10();    }    public static void print() {        System.out.println("Running!");    }}



GlobalVari_5.java
Java code
import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Vector;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.ListSelectionModel;import javax.swing.event.ListSelectionEvent;import javax.swing.event.ListSelectionListener;import javax.swing.table.DefaultTableCellRenderer;import javax.swing.table.DefaultTableModel;public class GlobalVari_5 extends JFrame{    private static final long serialVersionUID = 1L;        private static double select = 0;    private static int rowIndex = 0;    //  /表格方法使用      public GlobalVari_5(){        init();        this.setTitle("Global Variables");        this.setSize(new Dimension(650, 400));        this.setLocationRelativeTo(null);        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);        this.setVisible(true);        this.setLayout(null);        this.setResizable(false);    }    private void init(){        Vector<String> colHeader = new Vector<String>();        colHeader.add("Variable");        colHeader.add("Value");        colHeader.add("Time");        Vector<Vector<String>> dataVec = new Vector<Vector<String>>();        final JTable table = new JTable(dataVec,colHeader){            private static final long serialVersionUID = 1L;            public boolean isCellEditable(int row, int column){                if(column > 1){                    return false;                } else {                    return true;                }            }        };        //2设置表头行高          table.getTableHeader().setPreferredSize(new Dimension(0,30));          //3设置表内容行高          table.setRowHeight(25);          //4设置单选模式          table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);          //5设置单元格不可拖动          table.getTableHeader().setReorderingAllowed(false);          //6设置不可改变列宽          table.getTableHeader().setResizingAllowed(false);        //7设置列宽          table.getColumnModel().getColumn(0).setPreferredWidth(95);        table.getColumnModel().getColumn(1).setPreferredWidth(55);          table.getColumnModel().getColumn(2).setPreferredWidth(30);            //注意索引越界        DefaultTableCellRenderer right = new DefaultTableCellRenderer();        right.setHorizontalAlignment(JLabel.RIGHT);        table.getColumnModel().getColumn(1).setCellRenderer(right);         table.getColumnModel().getColumn(2).setCellRenderer(right);                final JButton buttonAdd = new JButton("Add");        final JButton buttonDel = new JButton("Delete");        final JButton buttonCls = new JButton("Close");                rowIndex = table.getSelectedRow();          System.out.println("index:"+rowIndex);          //监听事件          table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){              public void valueChanged(ListSelectionEvent e){                if(e.getValueIsAdjusting()){//连续操作                      rowIndex = table.getSelectedRow();                      if(rowIndex != -1){                          System.out.println("表格行被选中"+rowIndex);                         buttonDel.setEnabled(true);                         select = 1;                    }                                     }            }        });        JScrollPane scrollPane = new JScrollPane();         scrollPane.setViewportView(table);        this.add(scrollPane);        scrollPane.setSize(515, 350);        scrollPane.setLocation(5, 5);        buttonAdd.setEnabled(true);        buttonDel.setEnabled(false);        buttonCls.setEnabled(true);        JPanel panel = new JPanel();        panel.setSize(10, 350);        panel.setLocation(520, 5);        this.add(panel);                panel.setLayout(null);        panel.add(buttonAdd);        buttonAdd.setSize(100, 30);        buttonAdd.setLocation(530, 6);        panel.add(buttonDel);        buttonDel.setSize(100, 30);        buttonDel.setLocation(530, 50);        panel.add(buttonCls);        buttonCls.setSize(100, 30);        buttonCls.setLocation(530, 322);                buttonAdd.addMouseListener(new MouseListener(){            public void mouseClicked(MouseEvent e){                DefaultTableModel model = (DefaultTableModel)table.getModel();                int rownum = table.getRowCount() + 1;                model.addRow(new Object[]{"gvar" + rownum, "", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())});            }            public void mousePressed(MouseEvent e){            }            public void mouseReleased(MouseEvent e){            }            public void mouseEntered(MouseEvent e){            }            public void mouseExited(MouseEvent e){            }        });        buttonDel.addMouseListener(new MouseListener(){            public void mouseClicked(MouseEvent e){                if(select == 1){                    DefaultTableModel model = (DefaultTableModel)table.getModel();                    model.removeRow(rowIndex);                    select = 0;                                        buttonDel.setEnabled(false);                }            }            public void mousePressed(MouseEvent e){            }            public void mouseReleased(MouseEvent e){            }            public void mouseEntered(MouseEvent e){            }            public void mouseExited(MouseEvent e){            }        });                buttonCls.addMouseListener(new MouseListener(){            public void mouseClicked(MouseEvent e){                setVisible(false);            }            public void mousePressed(MouseEvent e){            }            public void mouseReleased(MouseEvent e){            }            public void mouseEntered(MouseEvent e){            }            public void mouseExited(MouseEvent e){            }        });    }      public static void main(String[] args){        new GlobalVari_5();    }}
  相关解决方案