我想避免其余代码被执行,直到用户响应jtable(等到他取消它以显示JOptionPane为止
import java.awt.*;
import javax.swing.*;
public class test {
public static void main(String args[]) {
String[][] rows = { { "Sameendra", "MCA","sam", "Old" }, { "vinod", "M.ED","sam", "Old" }, { "Suman", "BIT","sam", "Old" }, { "Amit", "Msc","sam", "Old" }, {"Deepak", "BIT","sam", "Old"}, { "Ravi", "MCA","sam", "Old" },// { "Sameendra", "MCA","sam", "Old" }, { "vinod", "M.ED","sam", "Old" }, };
};
String headers[] = { "Upper", "Lower", "New", "Old" };
JTable table = new JTable(rows, headers);
table.setEnabled(true);
JScrollPane scrollPane = new JScrollPane(table);
JFrame frame = new JFrame("New Table");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setLocationRelativeTo(frame);
frame.setLocation(500,325);
frame.setSize(300, 168);
frame.setVisible(true);
frame.setFocusable(true);
JOptionPane.showMessageDialog(null, "Goback?", "return",
JOptionPane.INFORMATION_MESSAGE);
最佳答案
请记住,大多数GUI都是事件驱动的环境,这意味着可以一次在屏幕上具有多个窗口和控件,并且用户可以选择要执行的操作。
如果要在用户关闭窗口之前停止执行代码,则需要使用某种模式对话框。这将在对话框可见时停止执行代码,并在关闭对话框后继续执行。
除了使用JFrame
,还可以使用JDialog
或更简单的方法,使用JOptionPane
,并将JTable
作为消息对象传递。
请查看How to use dialogs了解更多详细信息,并查看How do i make the output come in different columns?例如。