簡體   English   中英

單擊按鈕以打開新的JFrame

[英]Button clicked to open a new JFrame

我目前正在Eclipse上進行復制,我需要按下一個按鈕,然后打開一個JFrame,其中包含來自JTable上MySQL數據庫的值。 我最近在這里問了一個有關我嘗試使用絕對布局(空布局)顯示列時遇到的錯誤的問題,並被告知使用布局會更好。 問題是我在現有表單上有多個按鈕和內容,更改布局會隱藏某些功能,而我找不到解決方案,所以我想到了一些東西。 由於JTable的絕對布局存在問題,並且Layout Manager不能幫助我解決多個按鈕,因此可以從現有表單(空布局)中刪除JTable並保留按鈕。 結果,當用戶在現有表單上按下mySQL應用程序的“按名稱排序”或“添加控制台”按鈕時,應彈出一個新的JFrame並顯示正確的JTable。 有沒有辦法做到這一點?

JTable創建過程:

 package newWindow;

  import java.awt.*;
  import java.sql.*;
  import javax.swing.*;
  import javax.swing.table.*;

 public class newWindow {

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

 public newWindow() {
   EventQueue.invokeLater(new Runnable() {
       @Override
         public void run() {
           try {
 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
 } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {}

  try
  {
  Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test1?user=mySQL&password=7777");
  Statement stmt = conn.createStatement();
  ResultSet rs = stmt.executeQuery("SELECT * FROM consoles INNER JOIN hardware ON consoles.id=hardware.id");
  ResultSetMetaData md = rs.getMetaData();
  int columnCount = md.getColumnCount();
  String[] cols = new String[columnCount];
  int i;
  for (i=1;i<= columnCount;i++)
   {
     cols[i-1] = md.getColumnName(i);
   }
   DefaultTableModel model = new DefaultTableModel(cols,0);
     while (rs.next())
       {
         Object[] row = new Object[columnCount];
             for (i = 1 ; i <= columnCount ; i++)
                 {
                   row[i-1] = rs.getObject(i);
                 }
      model.addRow(row);
        }
   JFrame frame = new JFrame("Promitheas");
   JTable table = new JTable(model);
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.getContentPane().setLayout(new GridLayout());
   JScrollPane scrollPane = new JScrollPane(table);
   frame.getContentPane().add(scrollPane);
   frame.pack();
   frame.setLocationRelativeTo(null);
   frame.setVisible(true);
   conn.close();
   stmt.close();
   rs.close();
  } catch (SQLException case1) { case1.printStackTrace();
  } catch (Exception case2) { case2.printStackTrace();}               
    }
  });
 }
}

按名稱排序按鈕:

try{
   conn = DriverManager.getConnection("jdbc:mysql://localhost/test1?user=mySQL&password=7777");
   String[] gender = { "ASC", "DESC"};
   int response = JOptionPane.showOptionDialog(null,"Ascending or Descending Order?","Make a choice...",0,JOptionPane.INFORMATION_MESSAGE,null,gender,gender[0]);
   stmtsortname = conn.createStatement();
   String sql = null;
   if (response == JOptionPane.YES_OPTION)
        sql = "ASC";
   if (response == JOptionPane.NO_OPTION)
        sql = "DESC";
  rssortname = stmtsortname.executeQuery("SELECT * FROM consoles INNER JOIN hardware ON consoles.id=hardware.id ORDER BY consoles.name "+sql);
                              .
                              .
                              . 
             create an newJTable overwriting the old
                              .
                              .
 } catch ...  

創建一個類,將要傳輸的數據作為參數。 做你的魔術設置表,並將可見設置為true並用邏輯替換blala

public class NewJFrame extends JFrame{

   public NewJFrame(Data data){
      blala
      this.setVisible(true);
   }
}

然后在按鈕上有ActionListner或類似的內容

new NewJFrame(data);

回答

JButton btnStartTable = new JButton("Start Table");
    btnStartTable.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent arg0){
        try
    {

        conn = DriverManager.getConnection("jdbc:mysql://localhost/test1?user=mySQL&password=7777");
        stmt = conn.createStatement();
        rs = stmt.executeQuery("SELECT * FROM consoles INNER JOIN hardware ON consoles.id=hardware.id");
        md = rs.getMetaData();
        columnCount = md.getColumnCount();
        String[] cols = new String[columnCount];
        for (i=1;i<= columnCount;i++)
         {
        cols[i-1] = md.getColumnName(i);
         }
    model = new DefaultTableModel(cols,0);
        while (rs.next())
        {
              Object[] row = new Object[columnCount];
                 for (i = 1 ; i <= columnCount ; i++)
                 {
                    row[i-1] = rs.getObject(i);
                 }
        model.addRow(row);
        }

    frame2 = new JFrame();
    frame2.setVisible(true);
    JTable table = new JTable(model);
    f2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f2.getContentPane().setLayout(new GridLayout());
    JScrollPane scrollPane = new JScrollPane(table);
    f2.getContentPane().add(scrollPane);
    f2.pack();
    f2.setLocationRelativeTo(null);
    f2.setVisible(true);
    conn.close();
    stmt.close();
    rs.close();
    } catch (SQLException case1) { case1.printStackTrace();
    } catch (Exception case2) { case2.printStackTrace();}}
    });
    btnStartTable.setBounds(10, 11, 126, 23);
    frame.getContentPane().add(btnStartTable);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM