从jpanel调用函数

juud5qan  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(252)

我有这段代码。我想添加一些命令,以便从 drawgrid 与“konichiwa”一起出现在 JPanel 事情我试过使用谷歌上的一些命令,比如。 JTable table = new JTable(rec, header); . 我还没有找到其他方法将表实现到面板中,所以如果有人能建议一个可能有效的命令。

  1. import java.util.Random;
  2. import javax.swing.JFrame;
  3. import javax.swing.JLabel;
  4. import javax.swing.JOptionPane;
  5. import javax.swing.JPanel;
  6. import javax.swing.JScrollPane;
  7. import javax.swing.JTable;
  8. import javax.swing.table.TableModel;
  9. @SuppressWarnings({"serial", "unused"})
  10. public class OnO extends JPanel {
  11. public static void main(String[] argv) throws Exception {
  12. int i = yesno("Play game? D:");
  13. if (i == JOptionPane.YES_OPTION) {
  14. JFrame f = new JFrame("WELCOME!"); // initializes objects from class JFrame
  15. JLabel label1 = new JLabel("konichiwa"); // initializes objects from class JLabel
  16. f.add(label1); // f calls the add function
  17. f.pack(); // f calls the pack function
  18. JTable table = new JTable(d);
  19. f.add(new JScrollPane(table));
  20. f.add(table);
  21. f.setSize(550, 400);
  22. f.setVisible(true); // f calls the set visible function
  23. // when program gets big, need a way to route
  24. } else {
  25. System.exit(0);
  26. }
  27. }
  28. public static int yesno(String theMessage) {
  29. int result = JOptionPane.showConfirmDialog(null, theMessage,
  30. "WELCOME", JOptionPane.YES_NO_OPTION);
  31. return result;
  32. }
  33. public static String drawgrid() {
  34. String s = null;
  35. int[][] board = new int[16][16];
  36. int i;
  37. int j;
  38. Random r = new Random();
  39. int low = 0;
  40. int high = 2;
  41. for (i = 0; i < 16; i++) {
  42. for (j = 0; j < 16; j++) {
  43. int result = r.nextInt(high - low) + low;
  44. board[i][j] = result;
  45. }
  46. }
  47. for (int[] row : board) {
  48. printRow(row);
  49. }
  50. return s;
  51. }
  52. public static void printRow(int[] row) {
  53. for (int i : row) {
  54. System.out.print(i);
  55. System.out.print("\t");
  56. }
  57. System.out.println();
  58. }
  59. }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题