从其他班级创建的gui不工作

时间:2015-04-19 08:29:20

标签: java swing user-interface

我有两个gui课程,Mall和Store。通过运行Mall类,创建了一个gui,其中包含一些其他按钮,包括" store"按钮。当"存储"单击按钮,将为商店gui创建一个单独的窗口。基本上,当"存储"时,Mall类会调用Store构造函数。单击按钮。

从Mall类调用Store构造函数时,会显示Store中的gui。但是当我尝试按下箭头按钮时没有任何反应。这是我的商城类

import javax.swing.*;

public class Mall extends JFrame implements KeyListener, ActionListener {
    public static Customer c = new Customer("James");
    private JFrame frame = new JFrame(); // only used to show dialog boxes
    private JPanel panel;    
    //and other private JLabels, ImageIcons, and JButtons

    public static void main(String[] args) {
        new Mall(); 
    }

    public Mall() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.addKeyListener(this);
        // and initialization of gui components
        store.addActionListener(this);
        this.add(panel);
        // panel.add(....); etc.
    }
    @Override // and other method implementations
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_ENTER) {
            if(menuSelection == 1) {
                new Store(c);   
            }
            // other conditions
        }
        // and other conditions
    }
}

这是我的Store类:

import javax.swing.*;

public class Store implements KeyListener, ActionListener {
    JFrame frame1 = new JFrame(); 
    private JPanel panel1;    
    //and other private JLabels, ImageIcons, and JButtons

    // works fine if I have this method, but I want it to be called from the Mall class
    public static void main(String[] args) {
        new Store(new Customer("Bon")); }

    public Store(Customer c1) {
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        frame1.setTitle("Store");
        frame1.setVisible(true);
        // and initialization of gui components
        frame1.addKeyListener(this);
        frame1.add(panel1);
        // panel1.add(....); etc.
    }
    @Override // and other method implementations
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_UP) {
            if(storeSelection==1) {
                highlighter.setBounds(100,50,50, 50); // just for highlighting a button
            // other conditions 
        }
        // and other conditions
    }
}

Mall类在我的代码中运行得非常好。如果调用Store类中的main方法,Store类也可以正常工作。从Mall类调用它时只到达Store构造函数(即只有图形)。我想知道如何从Mall类调用Store gui。

编辑:

行为:

如果Store类中有一个main方法,那么Store gui就可以工作(public static void main(String [] args){new Store(new Customer(" James"));})

但商店gui只显示gui并且当Mall类调用它时不起作用(例如按下按钮)

问题:从Mall类调用时,如何使Store类工作?

1 个答案:

答案 0 :(得分:0)

根据您向我们展示的内容,似乎没有任何不一致之处。

我可以推荐的,只是为了测试它是否有效:

  

创建一个JButton,并指定一个Action Performed,以便它调用Store Class

JButton button = new JButton("Test");
button.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        new Store(c);
    }
}

如果这样做,我们可以确定您的KeyPressed事件处理程序存在问题,然后我们可以从那里开始。

如果按钮确实有效,您可以使用按钮而不是RadioButton组上的按键事件。例如:

JButton button = new JButton("Test");
button.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        if(menuSelection == 1) {
            new Store(c);
        }
    }
}

希望这能回答你的问题!

让我知道结果。

相关问题