我搜索了许多网站和java文档,看看我必须如何使用窗口监听器。
这是我的问题:我想用一个按钮打开一个新框架(这是成功的)并关闭前一个框架。我已经看过如何编写一个窗口监听器,但是我将它放在我的代码中,如何在按钮处理程序中调用它? (开始处理程序)任何人都可以告诉我如何?我也在这里搜索过并尝试不同的东西。
我很缺乏经验,但仍然必须学到很多东西。
提前致谢。
import javax.swing.*;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
public class Startvenster extends JFrame {
public static void main(String[] args) {
JFrame frame = new Startvenster();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setTitle("Rekentrainer - Startvenster");
frame.setSize(450, 400);
frame.setLocationRelativeTo(null);
frame.setContentPane( new Startpaneel());
frame.pack();
}
}
class Startpaneel extends JPanel {
private JLabel welkom_Lbl, naamLeerling_Lbl;
private JTextField naam;
private JButton start;
private Font font;
private JPanel centrumStart, begin;
public Startpaneel() {
setBackground(Color.WHITE);
begin = new JPanel();
begin.setVisible(false);
begin.setLayout(new GridLayout(8, 2));
begin.setBackground(Color.white);
centrumStart = new JPanel();
centrumStart.setBackground(Color.white);
centrumStart.setLayout(new GridLayout(3,1));
Component bovenStrut = Box.createVerticalStrut(200);
font = new Font("Dialog", Font.PLAIN, 14);
//onderdelen van het venster
welkom_Lbl = new JLabel("Vul hieronder je naam in om de rekentrainer te starten. ");
welkom_Lbl.setFont(font);
//invoer leerling
naam = new JTextField(10);
naam.setHorizontalAlignment(JTextField.CENTER);
naam.addActionListener(new StartHandler());
start = new JButton("Start");
start.addActionListener(new StartHandler());
start.setFont(font);
start.setMnemonic(KeyEvent.VK_S);
naamLeerling_Lbl = new JLabel();
centrumStart.add(welkom_Lbl);
centrumStart.add(naam);
centrumStart.add(start);
begin.add(centrumStart);
begin.add(naamLeerling_Lbl);
add(bovenStrut);
add(bovenStrut);
add(begin);
add(centrumStart);
}
class StartHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
if(e.getSource() == start || e.getSource() == naam) {
if(naam.getText().isEmpty()) {
JOptionPane.showMessageDialog(Startpaneel.this,
"Vul je naam in alsjeblieft.",
"Fout", JOptionPane.ERROR_MESSAGE);
} else {
String leerling = naam.getText();
Leerling.setLeerling(leerling);
Keuzevenster kz = new Keuzevenster();
kz.setVisible(true);
kz.setSize(400, 300);
kz.setLocationRelativeTo(null);
kz.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
kz.setTitle("Rekentrainer - Keuze scherm");
kz.setContentPane(new Keuzepaneel());
kz.pack();
setVisible(false);
}
}
} catch(NumberFormatException nfe) {
JOptionPane.showMessageDialog(Startpaneel.this,
"Geen getallen invullen alsjeblieft.",
"Fout", JOptionPane.ERROR_MESSAGE);
}
}
}
}
答案 0 :(得分:0)
这就是我在代码中管理监听器的方式。此示例适用于两个按钮。我遵循模型 - 视图 - 控制器结构。 对于主要:
public class Main {
public static void main(String[] args) {
//create the model
Model model = new Model();
//create the view
MainView view = new MainView();
//create the controller
MainController controller = new MainController();
//add controller to view
view.listener(controller);
//show the view
view.setVisible(true);
}
}
对于视图:
//create buttons
private JButton firstButton;
private JButton secondButton;
//...
firstButton = new JButton("first button");
secondButton = new JButton("second button");
//add them where you wish in your view.
//Then link the controller you added previously on them main to this two buttons:
public void listener(MainController controller) {
firstButton.addActionListener(controller);
secondButton.addActionListener(controller);
}
最后在你的控制器中,为每个按钮执行你想要的代码:
public class MainController implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//the following line tells you which button has been pressed
String pressedButton = e.getActionCommand();
switch (pressedButton){
case "first button":
System.out.println("First button pressed!");
break;
case "second button":
System.out.println("Second button pressed!");
break;
}
}
}