Я делаю очень плохую попытку в ActionListeners здесь.ActionListeners JMenuItems - Перекрестные классы
Я пытаюсь использовать ActionListeners для запуска кода из другого класса (AddForm.java) после нажатия JMenuItem (в MainMenu.java).
Во-первых, вот код: MainMenu.java
package carparksystem;
import javax.swing.JFrame;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
import java.awt.event.*;
public class MainMenu extends JFrame
{
public MainMenu()
{
JMenuBar mainMenu = new JMenuBar();
JMenu main = new JMenu("Menu");
mainMenu.add(main);
JMenuItem addCar = new JMenuItem("Add Car");
addCar.setActionCommand("Add");
main.add(addCar);
//addCar.addActionListener();
JMenuItem removeCar = new JMenuItem("Remove Car");
removeCar.setActionCommand("Remove");
main.add(removeCar);
JMenuItem searchCars = new JMenuItem("Search Cars");
searchCars.setActionCommand("Search");
main.add(searchCars);
setJMenuBar(mainMenu);
/*
//Add action listener for the Add Car button
addCar.addActionListener
(
new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
MainMenu.windowClosed();
}
}
);
*/
}
}
AddForm.java:
package carparksystem;
import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.JFrame;
public class AddForm extends JFrame
{
public AddForm()
{
JLabel regNumLabel = new JLabel("Registration Number:");
JLabel highValLabel = new JLabel("High Value?");
JLabel largeLabel = new JLabel("Large Vehicle?");
JRadioButton btnYesHighVal = new JRadioButton("Yes", false);
JRadioButton btnNoHighVal = new JRadioButton("No", true);
JRadioButton btnYesLarge = new JRadioButton("Yes", false);
JRadioButton btnNoLarge = new JRadioButton("No", true);
ButtonGroup highVal = new ButtonGroup(); //allows just one radio button from the group to be selected
highVal.add(btnYesHighVal);
highVal.add(btnNoHighVal);
ButtonGroup largeCar = new ButtonGroup(); //allows just one radio button from the group to be selected
largeCar.add(btnYesLarge);
largeCar.add(btnNoLarge);
JTextField regNumField = new JTextField();
JButton addCar = new JButton(" Add ");
JButton addCancel = new JButton("Cancel");
GroupLayout addLayout = new GroupLayout(getContentPane()); //chosen to display components in group layout
getContentPane().setLayout(addLayout);
addLayout.setAutoCreateGaps(true);
addLayout.setAutoCreateContainerGaps(true);
addLayout.setHorizontalGroup(addLayout.createSequentialGroup()
.addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(regNumLabel)
.addComponent(highValLabel)
.addComponent(largeLabel))
.addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(regNumField)
.addGroup(addLayout.createSequentialGroup()
.addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(btnYesHighVal)
.addComponent(btnYesLarge))
.addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(btnNoHighVal)
.addComponent(btnNoLarge))))
.addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(addCar)
.addComponent(addCancel))
);
addLayout.setVerticalGroup(addLayout.createSequentialGroup()
.addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(regNumLabel)
.addComponent(regNumField)
.addComponent(addCar))
.addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(addLayout.createSequentialGroup()
.addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(highValLabel)
.addComponent(btnYesHighVal)
.addComponent(btnNoHighVal))
.addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(largeLabel)
.addComponent(btnYesLarge)
.addComponent(btnNoLarge)))
.addComponent(addCancel))
);
setSize(375, 150);
setTitle("Add Car");
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
На основной раме я нарисовал ряд форм, которые будут появляться на раме в первом , И над ним будет JMenu с JMenuItems Добавить, удалить и найти автомобили в меню.
После нажатия этих кнопок «Удалить» и «Поиск» они откроют соответствующую форму, которая позволит пользователю вводить данные.
Когда я запускаю свой код с и без прослушивателей действий, он работает как обычно, но меню вообще не ссылаются. Как будто они не имеют никакого значения?
Я настоятельно рекомендую вам пересмотреть заголовок к чему-то более содержательному. –