我想在“public void actionPerformed(ActionEvent e) {}”之外使用一个变量。

huangapple 未分类评论49阅读模式
英文:

I want to use a variable outside of "public void actionPerformed(ActionEvent e) {}"

问题

需要创建一个图形用户界面(GUI),在那里输入高度和宽度,点击按钮后会启动一个新窗口,其高度和宽度将与输入的值相对应。目前,我有一个名为width和height的变量值,它们在public void actionPerformed(ActionEvent e) {}中设置,我希望使用它们来传递新窗口的值。我得出结论,要么从JTextField本身中提取值,要么使用我想要实现的方式。

  1. public class Form extends JFrame implements Runnable {
  2. public int wt;
  3. public int ht;
  4. public void Okno() {
  5. JFrame win1 = new JFrame("Evolution.IO");
  6. win1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  7. win1.setLayout(new BorderLayout());
  8. win1.setSize(470, 400);
  9. JPanel pane = new JPanel();
  10. pane.setLayout(new FlowLayout());
  11. JTextField txtField = new JTextField(5);
  12. JTextField txtField2 = new JTextField(5);
  13. JLabel weightL = new JLabel("Weight: ");
  14. JLabel highL = new JLabel("High: ");
  15. pane.add(weightL);
  16. pane.add(txtField);
  17. pane.add(highL);
  18. pane.add(txtField2);
  19. JButton btn = new JButton("Начать симуляцию");
  20. btn.addActionListener(new ActionListener() {
  21. @Override
  22. public void actionPerformed(ActionEvent e) {
  23. wt = Integer.parseInt(txtField.getText());
  24. ht = Integer.parseInt(txtField2.getText());
  25. if (!(txtField == null)) {
  26. Form f = new Form();
  27. new Thread(f).start();
  28. win1.getDefaultCloseOperation();
  29. }
  30. }
  31. });
  32. pane.add(btn);
  33. win1.add(pane);
  34. win1.setLocationRelativeTo(null);
  35. win1.setVisible(true);
  36. }
  37. private final int FRAMES_TOTAL = 100000;
  38. private final int SKIP_FRAMES = 10;
  39. private final Color BG = new Color(200, 200, 200, 255);
  40. private final Color BLUE = new Color(150, 160, 255, 255);
  41. private final Color RED = new Color(255, 100, 120, 255);
  42. private BufferedImage buf = new BufferedImage(w1(), h1(), BufferedImage.TYPE_INT_RGB);
  43. private BufferedImage img = new BufferedImage(w1(), h1(), BufferedImage.TYPE_INT_RGB);
  44. private BufferedImage graph = new BufferedImage(w1(), h1(), BufferedImage.TYPE_INT_ARGB);
  45. }

不再需要以下部分:

英文:

I need to create a GUI and enter the height and width there, click on the button and a new window will start with these heights and widths. Аt the moment I have variable values width and height ​​which are set in public void actionPerformed(ActionEvent e) {} and I want to use them to transfer the values ​​for a new window. I came to the conclusion that it is necessary either to pull values ​​from the JTextField itself or the way that I want to implement

  1. public class Form extends JFrame implements Runnable {
  2. //private int w= 1280;
  3. //private int h=768;
  4. public int wt;
  5. public int ht;
  6. public void Okno() {
  7. JFrame win1 = new JFrame("Evolution.IO");
  8. win1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  9. win1.setLayout(new BorderLayout());
  10. win1.setSize(470, 400);
  11. JPanel pane = new JPanel();
  12. pane.setLayout(new FlowLayout());
  13. JTextField txtField = new JTextField(5);
  14. JTextField txtField2 = new JTextField(5);
  15. JLabel weightL = new JLabel("Weight: ");
  16. JLabel highL = new JLabel("High: ");
  17. pane.add(weightL);
  18. pane.add(txtField);
  19. pane.add(highL);
  20. pane.add(txtField2);
  21. JButton btn = new JButton("Начать симуляцию");
  22. btn.addActionListener(new ActionListener() {
  23. @Override
  24. public void actionPerformed(ActionEvent e) {
  25. wt = Integer.parseInt(txtField.getText());
  26. ht = Integer.parseInt(txtField2.getText());
  27. if (!(txtField==null)) {
  28. Form f = new Form();
  29. new Thread(f).start();
  30. win1.getDefaultCloseOperation();
  31. }
  32. }
  33. });
  34. pane.add(btn);
  35. win1.add(pane);
  36. win1.setLocationRelativeTo(null);
  37. win1.setVisible(true);
  38. }
  39. //THIS DOESNT WORK#
  40. // public int w1() {
  41. // return this.wt;
  42. // }
  43. // public int h1() {
  44. // return this.ht;
  45. // }
  46. private final int FRAMES_TOTAL = 100000;
  47. private final int SKIP_FRAMES = 10;
  48. private final Color BG = new Color(200, 200, 200, 255);
  49. private final Color BLUE = new Color(150, 160, 255, 255);
  50. private final Color RED = new Color(255, 100, 120, 255);
  51. private BufferedImage buf = new BufferedImage(w1(), h1(), BufferedImage.TYPE_INT_RGB);
  52. private BufferedImage img = new BufferedImage(w1(), h1(), BufferedImage.TYPE_INT_RGB);
  53. private BufferedImage graph = new BufferedImage(w1(), h1(), BufferedImage.TYPE_INT_ARGB);
  54. //no longer needed

huangapple
  • 本文由 发表于 2020年5月3日 12:54:42
  • 转载请务必保留本文链接:https://java.coder-hub.com/61569793.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定