2011-12-08 2 views
0

システム管理者、従業員、または財務担当者の場合に、ユーザーがクリックするメインウィンドウを作成しました。私の問題の1つは、画面中央に表示されません。それを行う?次に、Finance ButtonをクリックするとMainwindowが終了し、私のログイン画面に移動します。どうすればいいですか?ここに私のメインウィンドウコード新しいボタンウィンドウとUIの配置

import java.awt.BorderLayout; 
import java.awt.EventQueue; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.border.EmptyBorder; 
import java.awt.CardLayout; 
import javax.swing.JEditorPane; 
import javax.swing.SpringLayout; 
import javax.swing.JButton; 
import javax.swing.JTextField; 
import javax.swing.JLabel; 
import javax.swing.JFormattedTextField; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 


    public class MainWindow extends JFrame { 

     private JPanel contentPane; 

     /** 
     * Launch the application. 
     */ 
     public static void main(String[] args) { 
      EventQueue.invokeLater(new Runnable() { 
       public void run() { 
        try { 
         MainWindow frame = new MainWindow(); 
         frame.setVisible(true); 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } 
       } 
      }); 
     } 

     /** 
     * Create the frame. 
     */ 
     public MainWindow() { 
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      setBounds(100, 100, 333, 191); 
      contentPane = new JPanel(); 
      contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
      setContentPane(contentPane); 
      contentPane.setLayout(new BorderLayout(0, 0)); 

      JButton btnNewButton = new JButton("Employee"); 
      contentPane.add(btnNewButton, BorderLayout.WEST); 

      JButton btnNewButton_1 = new JButton("Finance"); 
      btnNewButton_1.addActionListener(new ActionListener() { 
       public void actionPerformed(ActionEvent arg0) { 
        Login login = new Login(); 
       } 
      }); 
      contentPane.add(btnNewButton_1, BorderLayout.CENTER); 

      JButton btnNewButton_2 = new JButton("System Admin"); 
      contentPane.add(btnNewButton_2, BorderLayout.EAST); 

      JLabel lblNewLabel = new JLabel("Welcome"); 
      contentPane.add(lblNewLabel, BorderLayout.NORTH); 
     } 

    } 

だここでは、あなたのニーズに合わせてcarefully choose a layout managerに必要なログインフォーム

import java.awt.BorderLayout; 
import java.awt.GridLayout; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 
import javax.swing.JFrame; 
import javax.swing.JOptionPane; 
import javax.swing.JLabel; 
import javax.swing.JTextField; 
import javax.swing.JPasswordField; 
import javax.swing.JButton; 
import javax.swing.JPanel; 
import java.sql.Connection; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 

public class Login extends JFrame { 

    private JLabel label1, label2; 
    private JButton submit; 
    private JTextField textfield1; 
    private JPasswordField passfield; 
    private JPanel panel; 

    public Login() { 

     setSize(300, 100); 
     setVisible(true); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 

     label1 = new JLabel("User ID:"); 
     textfield1 = new JTextField(15); 
     label2 = new JLabel("Password:"); 
     passfield = new JPasswordField(15); 
     submit = new JButton("Submit"); 
     panel = new JPanel(new GridLayout(3, 1)); 

     panel.add(label1); 
     panel.add(textfield1); 
     panel.add(label2); 
     panel.add(passfield); 
     panel.add(submit); 
     add(panel, BorderLayout.CENTER); 

     ButtonHandler handler = new ButtonHandler(); 
     submit.addActionListener(handler); 
    }// end login constructor 

    private class ButtonHandler implements ActionListener { 

     public void actionPerformed(ActionEvent event) { 

      String user = textfield1.getText(); 
      char[] passChars = passfield.getPassword(); 
      Connection conn = Jdbc.dbConn(); 
      PreparedStatement ps = null; 
      ResultSet rs = null; 
      String pass = new String(passChars); 

      if (passChars != null) { 
       String sql = "SELECT employee_ID, employee_password FROM user WHERE employee_ID = ? AND employee_password = ?"; 

       try { 
        ps = conn.prepareStatement(sql); 
        ps.setString(1, user); 
        ps.setString(2, pass); 
        rs = ps.executeQuery(); 
        if (rs.next()) { 
         JOptionPane.showMessageDialog(null,"Welcome! "+user); 
        } else { 
         JOptionPane.showMessageDialog(null, "Wrong Input"); 
        } 
       } catch (Exception e) { 
         e.printStackTrace(); 
       } finally { 
        try { 
         rs.close(); 
         ps.close(); 
         conn.close(); 
        } catch (Exception ee) { 
          ee.printStackTrace(); 
        } 
       } 
      }// end actionPerformed 
     }// End ButtonHandler 
    }// End of class 
} 
+1

あなたの疑問符キーは – kleopatra

答えて

3

ヒント:

  1. MainWindow(JFrameの)ためにsetBounds()を使用しないでください。いくつかのレイアウトを使用し、最後にpack()を使用してください。手動でサイズを設定する場合は、setSize()を使用することもできます。
  2. 現在のウィンドウを閉じてログイン・フレームを開くには、setVisible(false)またはdispose()を追加し、ログイン・オブジェクトを作成して可視にします。

  3. フレームを中心にするには、setLocationRelativeTo(null);を試してください。

  4. label1,textFiled2btnNewButtonなどのような変数名を使用しないでください。使用方法を反映する適切な変数には適切な名前を使用してください。ポイント2用

例:

btnNewButton_1.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      setVisible(false); 
      Login login = new Login(); 
     } 
    }); 
+0

soo私は、[Finance]ボタンをクリックした後、オブジェクトを作成してから、それをfalseに設定します。または私はdispose()を使用します?? – user962206

+0

@ user962206私の答えの編集を参照してください。 –

+0

これはまさに私が探していたものです、ありがとう! – user962206

3

のための私のコードです。あなたは現在、あなたが望むことをしていないと思われるBorderLayoutを使用しています。

JPanelに3つのボタンを追加し、そのパネルをフレームのコンテンツペインとして設定してみてください。 JPanelはデフォルトでFlowLayoutを使用しています。

+0

EDIT :-)少し立ち往生しているようだ:どのように私はアプローチのようなもので、私のログインフォームを表示されるでしょう? – user962206

+0

このように?? :http://pastebin.com/bZqB3iuE – user962206

+0

コメントにコードを入れないでください。それは判読不能になります。それを更新として質問に編集して、質問が更新されたことを人に知らせます。 –

関連する問題