2013-03-28 99 views
1

打开新窗口/ Jframe(即在另一个类... Table_Edit中),我有另一类包含单个按钮的图像。 我想要的东西就像当我点击应该弹出一个窗口Table_Edit之一的按钮。 包构建;JAVA Swing onclick打开一个新窗口/ JFrame

import java.awt.*; 

import javax.swing.*; 
import com.jgoodies.forms.layout.FormLayout; 
import com.jgoodies.forms.layout.ColumnSpec; 
import com.jgoodies.forms.layout.RowSpec; 
import com.jgoodies.forms.factories.FormFactory; 
import javax.swing.table.DefaultTableModel; 
import javax.swing.border.TitledBorder; 

public class Table_Edit { 
private JTextField textField; 
private JTable table; 

public Table_Edit() 
{ 
    JFrame frame = new JFrame("Table"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    frame.setSize(600,400); 
    frame.getContentPane().setLayout(new FormLayout(new ColumnSpec[] { 
      FormFactory.RELATED_GAP_COLSPEC, 
      FormFactory.DEFAULT_COLSPEC, 
      FormFactory.RELATED_GAP_COLSPEC, 
      FormFactory.DEFAULT_COLSPEC, 
      FormFactory.RELATED_GAP_COLSPEC, 
      FormFactory.DEFAULT_COLSPEC, 
      FormFactory.RELATED_GAP_COLSPEC, 
      FormFactory.DEFAULT_COLSPEC, 
      FormFactory.RELATED_GAP_COLSPEC, 
      FormFactory.DEFAULT_COLSPEC, 
      FormFactory.RELATED_GAP_COLSPEC, 
      FormFactory.DEFAULT_COLSPEC, 
      FormFactory.RELATED_GAP_COLSPEC, 
      FormFactory.DEFAULT_COLSPEC, 
      FormFactory.RELATED_GAP_COLSPEC, 
      FormFactory.DEFAULT_COLSPEC, 
      FormFactory.RELATED_GAP_COLSPEC, 
      FormFactory.DEFAULT_COLSPEC, 
      FormFactory.RELATED_GAP_COLSPEC, 
      ColumnSpec.decode("default:grow"), 
      FormFactory.RELATED_GAP_COLSPEC, 
      ColumnSpec.decode("default:grow"),}, 
     new RowSpec[] { 
      FormFactory.RELATED_GAP_ROWSPEC, 
      FormFactory.DEFAULT_ROWSPEC, 
      FormFactory.RELATED_GAP_ROWSPEC, 
      RowSpec.decode("default:grow"), 
      FormFactory.RELATED_GAP_ROWSPEC, 
      RowSpec.decode("default:grow"),})); 

    JLabel lblTableName = new JLabel("Table Name :"); 
    frame.getContentPane().add(lblTableName, "2, 2, right, default"); 

    textField = new JTextField(40); 
    textField.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); 
    frame.getContentPane().add(textField, "4, 2, left, center"); 
    textField.setColumns(10); 

    JScrollPane scrollPane = new JScrollPane(); 
    frame.getContentPane().add(scrollPane, "5, 4, 17, 1, fill, fill"); 

    table = new JTable(); 
    table.setFillsViewportHeight(true); 
    scrollPane.setViewportView(table); 
    table.setBorder(new TitledBorder(null,, TitledBorder.CENTER,TitledBorder.TOP,null,   

    null)); 
    table.setColumnSelectionAllowed(true); 
    table.setCellSelectionEnabled(true); 
    table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); 
    table.setModel(new DefaultTableModel(
     new Object[][] { 
      {null, null, null, null}, 
      {null, null, null, null}, 
      {null, null, null, null}, 
      {null, null, null, null}, 
      {null, null, null, null}, 
      {null, null, null, null}, 
      {null, null, null, null}, 
      {null, null, null, null}, 
      {null, null, null, null}, 
      {null, null, null, null}, 
      {null, null, null, null}, 
      {null, null, null, null}, 
      {null, null, null, null}, 
      {null, null, null, null}, 
     }, 
     new String[] { 
      "Column Name", "Data Type", "NN", "PK" 
     } 
    ) { 
     Class[] columnTypes = new Class[] { 
      String.class, Object.class, Boolean.class, Boolean.class 
     }; 
     public Class getColumnClass(int columnIndex) { 
      return columnTypes[columnIndex]; 
     } 
    }); 
    table.getColumnModel().getColumn(0).setResizable(false); 
    table.getColumnModel().getColumn(0).setPreferredWidth(106); 
    table.getColumnModel().getColumn(1).setResizable(false); 
    table.getColumnModel().getColumn(1).setPreferredWidth(92); 
    table.getColumnModel().getColumn(2).setResizable(false); 
    table.getColumnModel().getColumn(2).setPreferredWidth(26); 
    table.getColumnModel().getColumn(3).setResizable(false); 
    table.getColumnModel().getColumn(3).setPreferredWidth(30); 

    JPanel panel = new JPanel(); 
    frame.getContentPane().add(panel, "22, 6, right, fill"); 

    JButton btnNewButton = new JButton("Save"); 
    btnNewButton.setMaximumSize(new Dimension(33, 19)); 
btnNewButton.setIcon(newImageIcon(Table_Edit.class.getResource("/com/sun/java/swing/ 

plaf/windows/icons/TreeOpen.gif 

"))); 
    panel.add(btnNewButton); 

    JButton btnNewButton_1 = new JButton("Exit"); 
btnNewButton_1.setIcon(newImageIcon(Table_Edit.class.getResource("/javax/swing/plaf/ 
metal/icons/ocean/close.gif"))); 
    panel.add(btnNewButton_1); 
    frame.setVisible(true);  
} 
public static void main(String[] args) 
{ 
    new Table_Edit(); 
} 
} 
+0

//当我点击其他窗口中的按钮时,该窗口应该弹出! // – Asdakamessoy 2013-03-28 10:12:20

回答

2

尝试添加一个ActionListener到按钮。

button.addActionListener(new ActionListener() { 
    //This method will be called whenever you click the button. 
    public void actionPerformed(ActionEvent e) { 
     new Table_Edit(); 
    } 
}); 
+0

是的,但是你能告诉当前打开的旧窗口是什么吗? – 2014-04-25 07:06:37