Java Swing 如何以编程方式关闭 JPanel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26762324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swing how to close JPanel programmatically
提问by Harun ERGUL
My main class extends JPanel and I create a table and a button on this panel.Now I want to close this panel when the user press it.On the internet closing examples are about JFrame.Is there solution for JPanel?
我的主类扩展了 JPanel,我在这个面板上创建了一个表格和一个按钮。现在我想在用户按下它时关闭这个面板。在互联网上关闭示例是关于 JFrame.JPanel 有解决方案吗?
- There is a panel
- On Panel there is a table and a button
- I add an action listener to the button
- I want to close panel if user press button
This is my code now I want when the user press the btnDelete then it close the panel
public class ListUsers extends JPanel {
ResultSet rs; ClientDAO dao; JScrollPane scrollPane; JTable table; Object columnId;
public ListUsers() throws SQLException {
dao = new ClientDAO(); rs=dao.getUsers(); ResultSetMetaData md = rs.getMetaData(); int columnCount = md.getColumnCount(); Vector<String> columns = new Vector(columnCount); //store column names for(int i=1; i<=columnCount; i++) columns.add(md.getColumnName(i)); Vector data = new Vector(); Vector row; //store row data while(rs.next()) { row = new Vector(columnCount); for(int i=1; i<=columnCount; i++) { row.add(rs.getString(i)); } data.add(row); } table = new JTable(data, columns); table.setPreferredScrollableViewportSize(new Dimension(500, 70)); table.setFillsViewportHeight(true); table.setVisible(true); table.validate(); table.setEnabled(true); add(table); table.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { final JDialog dialog = new JDialog(); dialog.setSize(300, 200); dialog.setLayout(null); columnId = table.getValueAt(table.getSelectedRow(),0); Integer no = new Integer(columnId.toString()); final int i =no.intValue(); String columnNo =columnId.toString(); String name = table.getValueAt(table.getSelectedRow(),1).toString(); String surname = table.getValueAt(table.getSelectedRow(),2).toString(); String gender = table.getValueAt(table.getSelectedRow(),3).toString(); String labelText ="<html>Name :<b>"+name+"</b><br>Surname :<b>"+surname+"</b><br>Gender :<b>"+gender+"</b></html>"; JLabel label=new JLabel(labelText); label.setVisible(true); label.setBounds(10, 10,300, 100); dialog.add(label); JButton btnUpdate= new JButton("Update"); btnUpdate.setVisible(true); btnUpdate.setBounds(10,100,100,35); JButton btnDelete= new JButton("Delete"); btnDelete.setVisible(true); btnDelete.setBounds(150,100,100,35); dialog.add(btnDelete); dialog.add(btnUpdate); btnUpdate.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { dialog.setVisible(false); } }); btnDelete.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { dao.deleteUser(i); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } dialog.setVisible(false); setVisible(false); System.exit(0); } }); dialog.setVisible(true);
- 有一个面板
- 在面板上有一张桌子和一个按钮
- 我向按钮添加了一个动作侦听器
- 如果用户按下按钮,我想关闭面板
这是我现在想要的代码,当用户按下 btnDelete 然后关闭面板
公共类 ListUsers 扩展 JPanel {
结果集rs;ClientDAO dao; JScrollPane scrollPane; JTable 表;对象列 ID;
公共 ListUsers() 抛出 SQLException {
dao = new ClientDAO(); rs=dao.getUsers(); ResultSetMetaData md = rs.getMetaData(); int columnCount = md.getColumnCount(); Vector<String> columns = new Vector(columnCount); //store column names for(int i=1; i<=columnCount; i++) columns.add(md.getColumnName(i)); Vector data = new Vector(); Vector row; //store row data while(rs.next()) { row = new Vector(columnCount); for(int i=1; i<=columnCount; i++) { row.add(rs.getString(i)); } data.add(row); } table = new JTable(data, columns); table.setPreferredScrollableViewportSize(new Dimension(500, 70)); table.setFillsViewportHeight(true); table.setVisible(true); table.validate(); table.setEnabled(true); add(table); table.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { final JDialog dialog = new JDialog(); dialog.setSize(300, 200); dialog.setLayout(null); columnId = table.getValueAt(table.getSelectedRow(),0); Integer no = new Integer(columnId.toString()); final int i =no.intValue(); String columnNo =columnId.toString(); String name = table.getValueAt(table.getSelectedRow(),1).toString(); String surname = table.getValueAt(table.getSelectedRow(),2).toString(); String gender = table.getValueAt(table.getSelectedRow(),3).toString(); String labelText ="<html>Name :<b>"+name+"</b><br>Surname :<b>"+surname+"</b><br>Gender :<b>"+gender+"</b></html>"; JLabel label=new JLabel(labelText); label.setVisible(true); label.setBounds(10, 10,300, 100); dialog.add(label); JButton btnUpdate= new JButton("Update"); btnUpdate.setVisible(true); btnUpdate.setBounds(10,100,100,35); JButton btnDelete= new JButton("Delete"); btnDelete.setVisible(true); btnDelete.setBounds(150,100,100,35); dialog.add(btnDelete); dialog.add(btnUpdate); btnUpdate.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { dialog.setVisible(false); } }); btnDelete.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { dao.deleteUser(i); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } dialog.setVisible(false); setVisible(false); System.exit(0); } }); dialog.setVisible(true);
回答by forgivenson
If you want to 'close' a JPanel, you can hide it.
如果你想“关闭”一个 JPanel,你可以隐藏它。
myPanel.setVisible(false);
And if/when you want to 'open' it again:
如果/当您想再次“打开”它时:
myPanel.setVisible(true);
回答by HellishHeat
Assuming you want to close your swing application when you press the button. You can just use:
假设您想在按下按钮时关闭 Swing 应用程序。你可以只使用:
System.exit(0);
System.exit(0);
回答by Alex
You may try using Frame.pack()
again it worked for me.
您可以尝试Frame.pack()
再次使用它对我有用。
回答by Lefteris Bab
public void actionPerformed(ActionEvent e) {
JComponent comp = (JComponent) e.getSource();
Window win = SwingUtilities.getWindowAncestor(comp);
win.dispose();
}
e.getSource(): Get Java Component
e.getSource():获取 Java Component
getWindowAncestor: Returns the first Window
ancestor of Component
getWindowAncestor:返回第一个Window
祖先Component
win.dispose(): Releases all of the native screen resources used by this its subcomponents, and all of its owned children.
win.dispose():释放其子组件及其所有子组件使用的所有本机屏幕资源。