2017-12-15 30 views
1

を最大化されるまでのJFrameが更新されません。私はウィンドウサイズは、私は、MySQL</p> <p>を使用してデータベースに接続されているJavaプログラムのためのJFrameを使用してGUIを作成してい

のためのGUIを作成したい12回のSQL操作を持っています

最初は表示されません1から12まで番号を入力すると、それは私がこれを実行するときに12スイッチケース

に移動して1つの[OK]を 更新ボタンやテキストフィールドを入力します聞いてきますウィンドウサイズを最大化または変更しない限り

私は他の同様の質問を見ましたが、もし皆さんが例のコードを提供できたら、私はそれを得られませんでした!

import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.border.EmptyBorder; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
//import javax.swing.SwingConstants; 
import javax.swing.JTextField; 
import javax.swing.JButton; 
import java.awt.event.ActionListener; 
import java.sql.DriverManager; 
import java.awt.event.ActionEvent; 
import java.sql.*; 
import java.awt.Color; 
//import java.awt.FlowLayout; 
import java.util.*; 
import javax.swing.JToggleButton; 
import javax.swing.JTextArea; 
//import java.util.Date; 

public class ProjectInterface extends JFrame { 
static Scanner read = new Scanner(System.in); 
static Connection connect = null; 
static Statement statement = null; 
static PreparedStatement preparedStatement = null; 
static ResultSet resultSet = null; 
static String uname; 
static String aname; 
static String res; 
    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    private JPanel contentPane; 
    private JTextField ANtextField; 
    private JTextField c1textField; 
    private JTextField c11textField; 
    //private JLabel lblNewLabel; 
    //private JLabel lblNewLabel_1; 

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

       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the frame. 
    */ 
    public ProjectInterface() { 
     setTitle("Database"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setBounds(100, 100, 378, 495); 
     contentPane = new JPanel(); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     contentPane.setLayout(new BorderLayout(0, 0)); 
     setContentPane(contentPane); 

     JPanel panel = new JPanel(); 
     panel.setBackground(new Color(186, 85, 211)); 
     panel.setForeground(new Color(0, 0, 0)); 
     contentPane.add(panel, BorderLayout.CENTER); 
     panel.setLayout(null); 

     JLabel lblEnter = new JLabel("Enter 1-12"); 
     lblEnter.setBounds(26, 8, 116, 14); 
     panel.add(lblEnter); 
     //RestextField.setHorizontalAlignment(SwingConstants.LEFT); 
     //label.setHorizontalAlignment(SwingConstants.LEFT); 

     ANtextField = new JTextField(); 
     //panel.add(ANtextField); 
     ANtextField.setBounds(152, 5, 86, 20); 
     ANtextField.setColumns(10); 
     panel.add(ANtextField); 

     JButton btnOk = new JButton("OK"); 
     btnOk.setBounds(248, 4, 63, 23); 
     panel.add(btnOk); 


     btnOk.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       /* 
       String uname; 
       String aname; 
       String res;*/ 

       try { 
        /* 
        Connection connect = null; 
        Statement statement = null; 
        PreparedStatement preparedStatement = null; 
        ResultSet resultSet = null;*/ 
       Class.forName("com.mysql.jdbc.Driver"); 
       connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/project?useSSL=false","root","R1234"); 
       statement = connect.createStatement(); 

       String s = ANtextField.getText(); 
       int x = Integer.parseInt(s); 
       switch(x) { 
       //customer 
       case 1: 

       //update customer set Password = 999 where Username = 'Raghad'; 
       //System.out.println(" (PK)"); 
       JLabel lblc1 = new JLabel("Enter username you wish to update its password:"); 
       lblc1.setBounds(30, 104, 300, 14); 
       panel.add(lblc1); 

       c1textField = new JTextField(); 
       c1textField.setBounds(350, 104, 86, 20); 
       c1textField.setColumns(10); 
       panel.add(c1textField); 

       JLabel lblc11 = new JLabel("Enter new password:"); 
       lblc11.setBounds(30, 140, 200, 14); 
       panel.add(lblc11); 

       c11textField = new JTextField(); 
       c11textField.setBounds(350, 140, 86, 20); 
       c11textField.setColumns(10); 
       panel.add(c11textField); 

       JButton btnUpdate = new JButton("Update"); 
       btnUpdate.setBounds(200, 200, 86, 20); 
       panel.add(btnUpdate); 

       btnUpdate.addActionListener(new ActionListener() { 
        public void actionPerformed(ActionEvent arg0) { 
       try { 
       preparedStatement = connect.prepareStatement("update customer set Password = ? where Username = ?"); 
       preparedStatement.setString(1, c11textField.getText()); 
       preparedStatement.setString(2, c1textField.getText()); 
       preparedStatement.executeUpdate(); 
        }catch(Exception e) {System.out.println("Exception in case1");} 
       }}); 

       break; 

       case 2: 
       //update location 
       System.out.println("Enter username you wish to update its location (PK)"); 
       uname = read.next(); //or nextLine? 
       System.out.println("Enter new city"); 
       String city = read.nextLine(); 
       System.out.println("Enter new street"); 
       String st = read.nextLine(); 
       System.out.println("Enter new ZIP code"); 
       int zip = read.nextInt(); 
       preparedStatement = connect.prepareStatement("update customer set City = ? where Username = ?"); 
       preparedStatement.setString(1, city); 
       preparedStatement.setString(2, uname); 
       preparedStatement.executeUpdate(); 
       preparedStatement = connect.prepareStatement("update customer set Street = ? where Username = ?"); 
       preparedStatement.setString(1, st); 
       preparedStatement.setString(2, uname); 
       preparedStatement.executeUpdate(); 
       preparedStatement = connect.prepareStatement("update customer set ZIPcode = ? where Username = ?"); 
       preparedStatement.setInt(1, zip); 
       preparedStatement.setString(2, uname); 
       preparedStatement.executeUpdate(); 
       break; 

       case 3: 
       //read or retrieve? customer info 
        JTextArea textArea_1 = new JTextArea(); 
        textArea_1.setBounds(10, 78, 326, 357); 
        panel.add(textArea_1); 
       resultSet = statement.executeQuery("select * from customer "); 
       while(resultSet.next()) { 
        String username = resultSet.getString("Username"); 
        String FName = resultSet.getString("FName"); 
        String LName = resultSet.getString("LName"); 
        int PhoneNo = resultSet.getInt("PhoneNo"); 
        String Password = resultSet.getString("Password"); 
        String Email = resultSet.getString("Email"); 
        String City = resultSet.getString("City"); 
        String Street = resultSet.getString("Street"); 
        int ZIPcode = resultSet.getInt("ZIPcode"); 
        String AName = resultSet.getString("AName"); 
        //textArea_1.append("%10s %10s %10s %10s %10s %10s %10s\n","Username","FName","LName","PhoneNo","Password","Email","City","Street","ZIPcode","AName"); 
        textArea_1.append("Username: " + username + "\n"); 
        textArea_1.append("FName: " + FName + "\n"); 
        textArea_1.append("LName: " + LName + "\n"); 
        textArea_1.append("PhoneNo: " + PhoneNo + "\n"); 
        textArea_1.append("Password: " + Password + "\n"); 
        textArea_1.append("Email: " + Email + "\n"); 
        textArea_1.append("City: " + City + "\n"); 
        textArea_1.append("Street: " + Street + "\n"); 
        textArea_1.append("ZIPcode: " + ZIPcode + "\n"); 
        textArea_1.append("AName: " + AName + "\n\n");} 
        break; 

        case 4: 
        //delete 
        System.out.println("Enter the username you wish to delete its info (PK)"); 
        String an = read.nextLine(); 
        connect.prepareStatement("delete from customer where Username = " + "'" + an + "'").executeUpdate(); 
        break; 

        // admin 
        case 5: 
        //update Responsibility 
        System.out.println("Enter admin name (PK"); 
        aname = read.nextLine(); 
        System.out.println("Enter new Responsibility"); 
        res = read.nextLine(); 
        preparedStatement = connect.prepareStatement("update admin set Responsibility = ? where AName = ?"); 
        preparedStatement.setString(1, res); 
        preparedStatement.setString(2, aname); 
        preparedStatement.executeUpdate(); 
        break; 

        case 6: 
        //delete customer from admin 
        System.out.println("Enter the admin name you wish to delete their customer (PK)"); 
        aname = read.nextLine(); 
        connect.prepareStatement("delete from customer where AName = " + "'" + aname + "'").executeUpdate(); 
        break; 

        case 7: 
        //insert an admin 
        //read.next(); 
        System.out.println("Enter admin name"); 
        aname = read.nextLine(); 
        System.out.println("Enter responsibility"); 
        res = read.nextLine(); 
        preparedStatement = connect.prepareStatement("insert into admin(AName,Responsibility) values(?,?)"); 
        preparedStatement.setString(1,aname); 
        preparedStatement.setString(2,res); 
        preparedStatement.executeUpdate(); 
        break; 

        case 8: 
        //retrieve first row admin name 
        resultSet = statement.executeQuery("select * from admin "); 
        if(resultSet.next()) { 
        aname = resultSet.getString("AName"); 
        System.out.println("Username: " + aname + " ");} 
        break; 

        case 9: 

        //supplier 
        System.out.println(""); 

        case 10: 


       }//switch 

       //JOptionPane.showMessageDialog(null, ""); 
       }catch(Exception e) { 
        e.printStackTrace(); 
       } 

      } 
     }); 
    } 



} 
+0

境界線を自分で設定しないでください。レイアウトマネージャを使用してください。 1件以内に問題が発生した場合は、[mcve]を送信してください。 – user1803551

答えて

1

ヌルレイアウトを使用しないでください。 setBounds(...)を使わないでください!

スイングはレイアウトマネージャで使用するように設計されています。あなたが動的に見えるGUIにコンポーネントを追加すると、基本的なコードは次のとおり

panel.add(....); 
panel.revalidate(); 
panel.repaint(); 

再検証()パネルのレイアウトマネージャを起動し、レイアウトマネージャは、ルールに基づいて、各コンポーネントをサイズ/位置を与えますレイアウトマネージャ。

スイングは、使用するように設計されています。

関連する問題