java jdbc和jframe

Posted

技术标签:

【中文标题】java jdbc和jframe【英文标题】:java jdbc and jframe 【发布时间】:2014-03-30 07:17:53 【问题描述】:

当我执行应用程序时,我得到“构建成功”但jframe 未显示在屏幕上。该应用程序是关于从 oracle 数据库中检索数据和 从数据库中找到特定的人(记录)。 jframe 可能有人员的详细信息 例如名字,姓氏...等。我可以浏览数据库中的记录。

class Addressbks extends javax.swing.JFrame

private JButton browseButton;

  private JLabel emailLabel;
  private JTextField emailTextField;
  private JLabel firstNameLabel;
  private JTextField firstNameTextField;
  private JLabel lastNameLabel;
  private JTextField lastNameTextField;
  private JLabel idLabel;
  private JTextField idTextField;  
  private JLabel phoneLabel;
  private JTextField phoneTextField;  
  private JTextField indexTextField;


  private JPanel navigatePanel; 
  private JPanel displayPanel;
  private JPanel queryPanel;

  private JButton  previousButton;
  private JButton  nextButton;

  private JLabel ofLabel;
  private JTextField maxTextField;  
  private JTextField queryTextField;  

  private Person currentEntry;
  private PersonQueries personQueries;
  private List<Person> results;
  private int numberOfEntries = 0;
  private int currentEntryIndex;
  private JLabel queryLabel;

  private JButton queryButton;
  private JButton insertButton;


    /**
     * Creates new form Address book
     */
    public Addressbks() 
        super("The Addressbook");
       initComponents();     

     setLayout(new FlowLayout(FlowLayout.CENTER,10,10));


     personQueries = new PersonQueries();

     navigatePanel = new JPanel();
     previousButton = new JButton();
     indexTextField = new JTextField(2);
     ofLabel = new JLabel();
     maxTextField = new JTextField(2);
     nextButton = new JButton();

     displayPanel  = new JPanel();
     idLabel = new JLabel();
     idTextField = new JTextField(10);
     firstNameLabel = new JLabel();
     firstNameTextField = new JTextField(10);
     lastNameLabel = new JLabel();
     lastNameTextField = new JTextField(10);
     emailLabel = new JLabel();
     emailTextField = new JTextField(10);
     phoneLabel = new JLabel();
     phoneTextField = new JTextField(10);   



     queryPanel = new JPanel();
     queryLabel = new JLabel();
     queryTextField =new JTextField(10);
     queryButton= new JButton();
     browseButton = new JButton();
     insertButton = new JButton();



     navigatePanel.setLayout(new BoxLayout(navigatePanel,BoxLayout.X_AXIS));
     previousButton.setText("Previous");

     previousButton.setEnabled(false);

     previousButton.addActionListener(new ActionListener()
        
                    @Override
            public void actionPerformed(ActionEvent e)
            
                previousButtonActionPerformed(e);
            
        );
    navigatePanel.add(previousButton);
    navigatePanel.add(Box.createHorizontalStrut(10));

    indexTextField.setHorizontalAlignment(JTextField.CENTER);

    indexTextField.addActionListener(new ActionListener()
        
                    @Override
            public void actionPerformed(ActionEvent e)
            
                 indexTextFieldActionPerformed(e);
            
        );

    navigatePanel.add(indexTextField);
    navigatePanel.add(Box.createHorizontalStrut(10));

    ofLabel.setText("of");
    navigatePanel.add(ofLabel);
    navigatePanel.add(Box.createHorizontalStrut(10));

    maxTextField.setHorizontalAlignment(JTextField.CENTER);
    maxTextField.setEditable(false);   
    navigatePanel.add(maxTextField);
    navigatePanel.add(Box.createHorizontalStrut(10));

     nextButton.setText("Next");     
     nextButton.setEnabled(false);
     nextButton.addActionListener(new ActionListener()
        
                    @Override
            public void actionPerformed(ActionEvent e)
            
                nextButtonActionPerformed(e);
            
        );
     navigatePanel.add(nextButton);
    add(navigatePanel);

     displayPanel.setLayout(new GridLayout(5,2,4,4));

     idLabel.setText("Address ID");
     displayPanel.add(idLabel);


     idTextField.setEditable(false);
     displayPanel.add(idTextField);


     firstNameLabel.setText("First Name :");
     displayPanel.add(firstNameLabel);
     displayPanel.add(firstNameTextField);

      lastNameLabel.setText("Last Name :");
     displayPanel.add(lastNameLabel);
     displayPanel.add(lastNameTextField);

      emailLabel.setText("Email :");
     displayPanel.add(emailLabel);
     displayPanel.add(emailTextField);

       phoneLabel.setText("Phone Number :");
     displayPanel.add(phoneLabel);
     displayPanel.add(phoneTextField);
     add(displayPanel);

     queryPanel.setLayout(new BoxLayout(queryPanel,BoxLayout.X_AXIS));
     queryPanel.setBorder(BorderFactory.createTitledBorder("Find an entry by lastname"));

    queryLabel.setText("Last Name");
    queryPanel.add(Box.createHorizontalStrut(5));

    queryPanel.add(queryLabel);
    queryPanel.add(Box.createHorizontalStrut(10));
    queryPanel.add(queryTextField);
    queryPanel.add(Box.createHorizontalStrut(10));

    queryButton.setText("Find");
    queryButton.addActionListener(new ActionListener()
                

                    @Override
            public void actionPerformed(ActionEvent e)
            
                 queryButtonActionPerformed(e);
            
        );
    queryPanel.add(queryButton);
    queryPanel.add(Box.createHorizontalStrut(5)); 
    add(queryPanel);

    browseButton.setText("Browse All Entries");
    browseButton.addActionListener(new ActionListener()
                

                    @Override
            public void actionPerformed(ActionEvent e)
            
                 browseButtonActionPerformed(e);
            
        );

    add(browseButton);

     insertButton.setText("Insert New Entry");
     insertButton.addActionListener(new ActionListener()
                

                    @Override
            public void actionPerformed(ActionEvent e)
            
                 insertButtonActionPerformed(e);
            
        );
     add(insertButton);

  /*  addWindowListener (new WindowAdapter()
    
            @Override
        public void windowClosing(WindowEvent e)
                 
            personQueries.close();
            //System.exit(0);
        
    );*/

      // setSize(400,400);
      // setResizable(false);
    // setLocationRelativeTo(null);
    // setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     //  setVisible(true); 

      
public class Addressbook 
 
    /**
     * @param args the command line arguments
     */

    public static void main(String args[]) throws InterruptedException
    

        try 
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) 
                if ("Nimbus".equals(info.getName())) 
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                
            
         catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) 
            java.util.logging.Logger.getLogger(Addressbook.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        
        //</editor-fold>

        /* Create and display the form */
        /*context: a.setVisible(true) still the jframe is not displayed */

      java.awt.EventQueue.invokeLater(new Runnable() 
                   @Override
            public void run() 
                JFrame a = new Addressbks();
                a.setVisible(true);

                
        );
    
 

【问题讨论】:

你没有看到任何东西,或者有一个非常小的 jframe 可以调整大小? 【参考方案1】:

您还没有为a JFrame 设置大小,请尝试将setSize() 方法添加到您的代码中,例如:

JFrame a = new Addressbks();
a.setSize(400, 400);
a.setVisible(true);

或者您可以使用pack(),它会调整框架的大小,使其所有内容都等于或大于它们的首选大小。

JFrame a = new Addressbks();
a.pack();
a.setVisible(true);

【讨论】:

pack 会是更好的解决方案,可能在缺少的 initComponents 方法中... @MadProgrammer 是的,它更好,但有时使用 setSize 很好,谢谢提示。

以上是关于java jdbc和jframe的主要内容,如果未能解决你的问题,请参考以下文章

14.2-全栈Java笔记: Java Swing创建窗口,原来这么简单!!!

Java贪吃蛇小游戏

JavaGUI之Swing简单入门示例

Java Swing界面编程(31)---菜单条:JMenu

什么是JDBC?

Java——JDBC