英文:
I want to use a variable outside of "public void actionPerformed(ActionEvent e) {}"
问题
需要创建一个图形用户界面(GUI),在那里输入高度和宽度,点击按钮后会启动一个新窗口,其高度和宽度将与输入的值相对应。目前,我有一个名为width和height的变量值,它们在public void actionPerformed(ActionEvent e) {}中设置,我希望使用它们来传递新窗口的值。我得出结论,要么从JTextField本身中提取值,要么使用我想要实现的方式。
public class Form extends JFrame implements Runnable {
public int wt;
public int ht;
public void Okno() {
JFrame win1 = new JFrame("Evolution.IO");
win1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win1.setLayout(new BorderLayout());
win1.setSize(470, 400);
JPanel pane = new JPanel();
pane.setLayout(new FlowLayout());
JTextField txtField = new JTextField(5);
JTextField txtField2 = new JTextField(5);
JLabel weightL = new JLabel("Weight: ");
JLabel highL = new JLabel("High: ");
pane.add(weightL);
pane.add(txtField);
pane.add(highL);
pane.add(txtField2);
JButton btn = new JButton("Начать симуляцию");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
wt = Integer.parseInt(txtField.getText());
ht = Integer.parseInt(txtField2.getText());
if (!(txtField == null)) {
Form f = new Form();
new Thread(f).start();
win1.getDefaultCloseOperation();
}
}
});
pane.add(btn);
win1.add(pane);
win1.setLocationRelativeTo(null);
win1.setVisible(true);
}
private final int FRAMES_TOTAL = 100000;
private final int SKIP_FRAMES = 10;
private final Color BG = new Color(200, 200, 200, 255);
private final Color BLUE = new Color(150, 160, 255, 255);
private final Color RED = new Color(255, 100, 120, 255);
private BufferedImage buf = new BufferedImage(w1(), h1(), BufferedImage.TYPE_INT_RGB);
private BufferedImage img = new BufferedImage(w1(), h1(), BufferedImage.TYPE_INT_RGB);
private BufferedImage graph = new BufferedImage(w1(), h1(), BufferedImage.TYPE_INT_ARGB);
}
不再需要以下部分:
英文:
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
public class Form extends JFrame implements Runnable {
//private int w= 1280;
//private int h=768;
public int wt;
public int ht;
public void Okno() {
JFrame win1 = new JFrame("Evolution.IO");
win1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win1.setLayout(new BorderLayout());
win1.setSize(470, 400);
JPanel pane = new JPanel();
pane.setLayout(new FlowLayout());
JTextField txtField = new JTextField(5);
JTextField txtField2 = new JTextField(5);
JLabel weightL = new JLabel("Weight: ");
JLabel highL = new JLabel("High: ");
pane.add(weightL);
pane.add(txtField);
pane.add(highL);
pane.add(txtField2);
JButton btn = new JButton("Начать симуляцию");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
wt = Integer.parseInt(txtField.getText());
ht = Integer.parseInt(txtField2.getText());
if (!(txtField==null)) {
Form f = new Form();
new Thread(f).start();
win1.getDefaultCloseOperation();
}
}
});
pane.add(btn);
win1.add(pane);
win1.setLocationRelativeTo(null);
win1.setVisible(true);
}
//THIS DOESNT WORK#
// public int w1() {
// return this.wt;
// }
// public int h1() {
// return this.ht;
// }
private final int FRAMES_TOTAL = 100000;
private final int SKIP_FRAMES = 10;
private final Color BG = new Color(200, 200, 200, 255);
private final Color BLUE = new Color(150, 160, 255, 255);
private final Color RED = new Color(255, 100, 120, 255);
private BufferedImage buf = new BufferedImage(w1(), h1(), BufferedImage.TYPE_INT_RGB);
private BufferedImage img = new BufferedImage(w1(), h1(), BufferedImage.TYPE_INT_RGB);
private BufferedImage graph = new BufferedImage(w1(), h1(), BufferedImage.TYPE_INT_ARGB);
//no longer needed
专注分享java语言的经验与见解,让所有开发者获益!
评论