问题1:第一个JPanel将显示,但似乎无法访问ControlPanel类中的actionPerformed()方法。我设想使用CardLayout在firstPanel和SecondPanel之间进行转换(在我的实际项目中,这些将包含两个截然不同的游戏环境,每个面板中都包含游戏逻辑)。
我希望能够让每个JPanel(firstPanel和SecondPanel)响应鼠标点击并切换到另一个面板。如果您需要任何其他信息,请与我们联系。以下所有代码都应该能够直接粘贴eclipse luna并编译。这是我到目前为止所做的。
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class CardLayoutMCVE
{
Panel1 firstPanel = new Panel1();
Panel2 secondPanel = new Panel2();
ControlPanel controlPanel = new ControlPanel();
CardLayout card = new CardLayout();
String State, panel1 = "1", panel2 = "2";
public CardLayoutMCVE()
{
firstPanel.setBackground(Color.BLACK);
secondPanel.setBackground(Color.CYAN);
JFrame frame = new JFrame("CardLayout MCVE");
frame.setSize(1000, 1000);
frame.setVisible(true);
frame.setContentPane(controlPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
controlPanel.setLayout(card);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run()
{
new CardLayoutMCVE();
}
});
}
private class ControlPanel extends JPanel implements ActionListener
{
public ControlPanel()
{
this.setLayout(card);
/*Which ever is added first is drawn first, regardless of the
* actionPerformed method.*/
this.add("Panel1", firstPanel);
this.add("Panel2", secondPanel);
Timer timer = new Timer(14, this);
timer.start();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(State == panel1)
{
card.show(controlPanel, "Panel1");
}
if(State == panel2)
{
card.show(controlPanel, "Panel2");
}
}
}
private class Panel1 extends JPanel implements ActionListener, MouseListener
{
/*Because there is no image the rectangle is set to cover the entire
* screen, so window should change to Cyan on mouse click*/
Rectangle rec1 = new Rectangle(0, 0, 1000 , 1000);
public Panel1()
{
this.addMouseListener(this);
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
Point p = e.getPoint();
if(rec1.contains(p));
{
State = panel2;
}
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}}
private class Panel2 extends JPanel implements ActionListener, MouseListener
{
Rectangle rec1 = new Rectangle(0, 0, 1000 , 1000);
public Panel2()
{
this.addMouseListener(this);
Timer timer = new Timer(14, this);
timer.start();
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
Point p = e.getPoint();
if(rec1.contains(p));
{
State = panel1;
}
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}}
}
感谢Andrew Thompson的有用编辑!