我正在設計一個包含多個組件的軟件 - 每個組件都有自己的actionlistener。 舉個例子,我有一個帶有cardlayout的JPanel,它擁有10張卡片 - 每個卡片都有它自己的JPanel和用途。如何暫停方法,直到按下按鈕?
一方面,有多個按鈕,I.E.登錄,註銷,設置等
當我點擊登錄,它會切換到卡使用Login()方法登錄JPanel對象,我希望它等待按鈕點擊I.E.登錄,新用戶或取消,然後繼續登錄()方法並設置當前用戶。
是否有方法暫停程序,直到點擊其中一個按鈕以從中檢索數據? (有點像如何JOptionPane.showInputMessage(NULL, 「輸入字符串」)等待你)
我的代碼如下: FRAME:
/**
* Frame design
*/
public class Frame extends JFrame{
JPanel LeftSide, UpperRightSide;
EmployeeAdder employAdd;
ArrayList<ServiceView> serviceViewers;
ChartViewer viewChart;
PayByView viewPayBy;
SettingsViewer viewSettings;
LoginViewer viewLogin;
CategoryView viewCategory;
ServiceAdder serviceAdd;
Directory directory;
Employee currentEmployee;
ChargeViewer viewCharge;
JButton Login, Logout, Settings;
CardLayout LeftCard,RightCard;
String currentCard,currentRightCard;
ButtonListen listen;
public static String CARDCAT = "Category View";
public static String CARDPAY = "Pay By";
public static String CARDCHART = "Chart View";
public static String CARDLOGIN = "Log-in View";
public static String CARDSERVICEADD = "Service Adder";
Frame(){
listen = new ButtonListen();
//-------Current Card--------------------
currentCard = CARDCAT;
currentRightCard = "CHARGE";
//-----First Find Directory Folder-------
startDirectory();
//-----User Interface--------------------
//-------Left Side-----------------------
LeftSide = new JPanel();
LeftCard = new CardLayout();
LeftSide.setLayout(LeftCard);
viewPayBy = new PayByView();
viewLogin = new LoginViewer();
viewChart = new ChartViewer();
viewCategory = new CategoryView();
employAdd = new EmployeeAdder();
serviceAdd = new ServiceAdder();
LeftSide.add(viewCategory,"CAT");
LeftSide.add(viewChart, "CHA");
LeftSide.add(viewLogin,"LOG");
LeftSide.add(viewPayBy,"PAY");
LeftSide.add(employAdd,"EMA");
LeftSide.add(serviceAdd,"SEA");
LeftCard.show(LeftSide, "CAT");
viewCategory.setEnabled(false);
LeftSide.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK),currentCard));
serviceViewers = new ArrayList<ServiceView>();
//--------Right Side---------------------
JPanel RightSide = new JPanel();
RightSide.setLayout(new BorderLayout());
UpperRightSide = new JPanel();
RightCard = new CardLayout();
UpperRightSide.setLayout(RightCard);
viewSettings = new SettingsViewer();
viewCharge = new ChargeViewer();
viewCharge.setEnabled(false);
UpperRightSide.add(viewCharge,"CHARGE");
UpperRightSide.add(viewSettings,"SETTINGS");
UpperRightSide.setPreferredSize(new Dimension(350,500));
RightSide.add(UpperRightSide,BorderLayout.NORTH);
//--------Buttons at the bottom Panel---
JPanel Buttons = new JPanel();
Buttons.setLayout(new GridLayout(3,1));
Login = new JButton("LOG-IN");
Login.addActionListener(listen);
Logout = new JButton("LOG OUT");
Logout.addActionListener(listen);
Settings = new JButton("Settings");
Settings.addActionListener(listen);
Buttons.add(Login);
Buttons.add(Logout);
Buttons.add(Settings);
Buttons.setPreferredSize(new Dimension(350,150));
RightSide.add(Buttons,BorderLayout.SOUTH);
RightSide.setSize(new Dimension(400,200));
//------Other Stuff--------------------------
//-----add Panels----------------------------
setLayout(new BorderLayout());
add(LeftSide,BorderLayout.WEST);
add(RightSide,BorderLayout.EAST);
}
private void Login(){
LeftCard.show(LeftSide, "LOG");
//----I WANT IT TO WAIT HERE FOR AN ACTION-------
int clicked = viewLogin.getClicked();
if (clicked==LoginViewer.NEWUSER){
NewUser();
}else if (clicked==LoginViewer.LOGIN){
if (viewLogin.checkPassword()){
currentEmployee = directory.getEmployee(viewLogin.getSelectedName());
viewCategory.setEnabled(true);
viewCharge.setEnabled(true);
viewCharge.refreshName(currentEmployee.getName());
LeftCard.show(LeftSide, "CAT");
}
}else if (clicked==LoginViewer.CANCEL){
LeftCard.show(LeftSide, "CAT");
}
}
public class ButtonListen implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
if (!viewLogin.isWaiting()){
if (e.getSource()==Login){
if (currentCard.equals(CARDLOGIN)){
LeftCard.show(LeftSide,"CAT");
currentCard = CARDCAT;
}else{
Login();
currentCard = CARDLOGIN;
}
}else{
//Don't change the screen
}
}
}
}
}
我對LoginViewer代碼:
public class LoginViewer extends JPanel{
JComboBox User;
JPasswordField passField;
JButton NewUser, Login, Cancel;
Hashtable<String,String> namespass; //names and password
private int clicked = -1;
ButtonListen listen;
public static int NEWUSER = 1;
public static int LOGIN = 0;
public static int CANCEL = 2;
boolean waiting;
LoginViewer(){
waiting = false;
//---------------------------------------
setPreferredSize(new Dimension(100,100));
listen = new ButtonListen();
namespass = new Hashtable<String,String>();
//----------Panel Design-------------------
JPanel Center = new JPanel();
Center.setLayout(new GridLayout(3,3));
User = new JComboBox();
passField = new JPasswordField();
NewUser = new JButton("New User");
NewUser.addActionListener(listen);
Login = new JButton("Login");
Login.addActionListener(listen);
Cancel = new JButton("Cancel");
Cancel.addActionListener(listen);
Center.add(new JLabel("Choose User"));
Center.add(User);
Center.add(new JLabel(""));
Center.add(new JLabel("Type Password"));
Center.add(passField);
Center.add(new JLabel(""));
Center.add(Login);
Center.add(NewUser);
Center.add(Cancel);
Center.setPreferredSize(new Dimension(300,300));
Center.setMaximumSize(new Dimension(100,100));
Center.setAlignmentX(CENTER_ALIGNMENT);
setAlignmentX(CENTER_ALIGNMENT);
setLayout(new BoxLayout(this,BoxLayout.X_AXIS));
add(Box.createHorizontalGlue());
add(Center);
add(Box.createHorizontalGlue());
}
public void uploadUserNames(Hashtable<String,String> names){
namespass.clear();
namespass.putAll(names);
User.removeAllItems();
Enumeration<String> name = names.keys();
while (name.hasMoreElements()){
User.addItem(name.nextElement());
}
}
public boolean checkPassword(){
boolean value = false;
String key = User.getSelectedItem().toString();
if (passField.getPassword().length==4){
if (namespass.get(key).equals(String.valueOf(passField.getPassword()))){
value = true;
}
}
return value;
}
public String getSelectedName(){
return User.getSelectedItem().toString();
}
public boolean isWaiting(){
return waiting;
}
public int getClicked(){
waiting = true;
return clicked;
}
public class ButtonListen implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
waiting = false;
if (e.getSource()==NewUser){
clicked = 1;
}else if (e.getSource()==Login){
clicked = 0;
}else if (e.getSource()==Cancel){
clicked = 2;
}
}
}
}
或者僅僅使用actionlistener來聽取所有對象的按鈕更容易?
有按鈕的很多...
注意:有些方法是不完整的或測試方法,直到我知道如何使它工作...