[英]ActionListeners JMenuItems - Cross classes
我在這里對ActionListeners進行了非常糟糕的嘗試。
一旦嘗試單擊JMenuItem(位於MainMenu.java中),我試圖使用ActionListeners從另一個類(AddForm.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添加,刪除和搜索汽車。
單擊這些添加刪除和搜索“菜單按鈕”后,它們將打開相應的表單,該表單將允許用戶輸入數據。
當我在有和沒有動作監聽器的情況下運行代碼時,它會正常運行,但菜單根本不會鏈接。 好像他們沒有意義?
一些基本問題:
對於一個簡單的非MVC示例,您可以使用一個第二個類的調用方法。 假設一個JPanel擁有一個名為View1的JList,該JList具有一個公共方法addItem(String item)
,該方法將項添加到JList的模型中:
public class View1 extends JPanel {
private static final String PROTOTYPE = String.format("%50s", " ");
private DefaultListModel<String> listModel = new DefaultListModel<>();
private JList<String> list = new JList<>(listModel);
public View1() {
list.setPrototypeCellValue(PROTOTYPE);
list.setVisibleRowCount(8);
JScrollPane scrollPane = new JScrollPane(list);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
add(scrollPane);
}
public void addItem(String item) {
listModel.addElement(item);
}
}
考慮第二個JPanel類View2,該類包含JTextArea和JButton以及View1的實例。 然后,第二個類可以調用View1的addItem(...)
方法:
public class View2 extends JPanel {
private View1 view1;
private JTextField textField = new JTextField(10);
public View2(View1 view1) {
Action addItemAction = new AddItemAction();
this.view1 = view1;
add(textField);
add(new JButton(addItemAction));
textField.setAction(addItemAction);
}
private class AddItemAction extends AbstractAction {
public AddItemAction() {
super("Add Item");
putValue(MNEMONIC_KEY, KeyEvent.VK_A);
}
@Override
public void actionPerformed(ActionEvent e) {
view1.addItem(textField.getText()); // *** calls view1's method here
textField.selectAll();
}
}
}
然后,您可以創建兩個類,將View1傳遞給View2的構造函數
View1 view1 = new View1();
View2 view2 = new View2(view1);
然后將一個放入JFrame,將另一個放入非模式JDialog並顯示。 例如:
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class SimpleGuis {
private static void createAndShowGui() {
View1 view1 = new View1();
View2 view2 = new View2(view1);
JFrame frame = new JFrame("SimpleGuis");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(view1);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
JDialog dialog = new JDialog(frame, "View2", ModalityType.MODELESS);
dialog.add(view2);
dialog.pack();
dialog.setLocationByPlatform(true);
dialog.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class View1 extends JPanel {
private static final String PROTOTYPE = String.format("%50s", " ");
private DefaultListModel<String> listModel = new DefaultListModel<>();
private JList<String> list = new JList<>(listModel);
public View1() {
list.setPrototypeCellValue(PROTOTYPE);
list.setVisibleRowCount(8);
JScrollPane scrollPane = new JScrollPane(list);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
add(scrollPane);
}
public void addItem(String item) {
listModel.addElement(item);
}
}
class View2 extends JPanel {
private View1 view1;
private JTextField textField = new JTextField(10);
public View2(View1 view1) {
Action addItemAction = new AddItemAction();
this.view1 = view1;
add(textField);
add(new JButton(addItemAction));
textField.setAction(addItemAction);
}
private class AddItemAction extends AbstractAction {
public AddItemAction() {
super("Add Item");
putValue(MNEMONIC_KEY, KeyEvent.VK_A);
}
@Override
public void actionPerformed(ActionEvent e) {
view1.addItem(textField.getText());
textField.selectAll();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.