简体   繁体   English

单击按钮以打开新的JFrame

[英]Button clicked to open a new JFrame

Im currently working on an aplication on Eclipse and what I need is to press a button and then a JFrame to open, containing values from MySQL DB on a JTable. 我目前正在Eclipse上进行复制,我需要按下一个按钮,然后打开一个JFrame,其中包含来自JTable上MySQL数据库的值。 I had recently asked a question here about an error I had in trying to show columns using absolute layout(null layout) and I was told that it would be preferable to use layouts. 我最近在这里问了一个有关我尝试使用绝对布局(空布局)显示列时遇到的错误的问题,并被告知使用布局会更好。 The thing is that i have multiple buttons and stuff on my existing form and changing the layout would hide some of the features and I couldn't find a solution.So I thought of something. 问题是我在现有表单上有多个按钮和内容,更改布局会隐藏某些功能,而我找不到解决方案,所以我想到了一些东西。 Since the JTable has issues with absolute layout and Layout Managers dont help me with my multiple buttons, I could delete the JTable from my existing form(null layout) and keep the buttons. 由于JTable的绝对布局存在问题,并且Layout Manager不能帮助我解决多个按钮,因此可以从现有表单(空布局)中删除JTable并保留按钮。 As a result, when a user presses the "Sort by Name" or "Add Console" buttons I have for mySQL app on the existing form, then a new JFrame should pop up and show the correct JTable. 结果,当用户在现有表单上按下mySQL应用程序的“按名称排序”或“添加控制台”按钮时,应弹出一个新的JFrame并显示正确的JTable。 Is there a possible way to do that? 有没有办法做到这一点?

JTable Creation Process : 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();}               
    }
  });
 }
}

Sort by Name Button : 按名称排序按钮:

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 ...  

Make a class which takes the data you want transfered as a parameter. 创建一个类,将要传输的数据作为参数。 Do your magic setup the table and set visible true and replace blala with your logic 做你的魔术设置表,并将可见设置为true并用逻辑替换blala

public class NewJFrame extends JFrame{

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

and then on the button you have the ActionListner or something simmilar in that 然后在按钮上有ActionListner或类似的内容

new NewJFrame(data);

ANSWERED 回答

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