在两个JFrame之间进行通信

时间:2015-04-25 12:16:00

标签: java swing graphics actionlistener paintcomponent

很抱歉,如果此问题已被提出,但我无法理解现有答案,并且需要查看与我的代码相关的问题。 (我是java的新手)

也很抱歉,如果这个类似的代码已经发布了 - 我尝试在一个框架上使用两个面板进行此操作并且无法使其正常工作,所以即时尝试此方法。

当我在第2帧按下我的按钮blackTrail时,它会将我在第1帧上的射弹颜色改为黑色。

到目前为止,这是我的代码:

GUI表单类(带按钮的菜单等)

 public class GUIForm1 extends JFrame{

//Initialise components
private JPanel MainPanel;
private JPanel MenuPanel;
private JButton whiteTrail;
private JButton blackTrail;
private JButton redTrail;
private JButton blueTrail;
private JButton greenTrail;
private JButton purpleTrail;
//irrelevant components  


public GUIForm1() {



    JFrame frame2 = new JFrame("Projectile Motion - Menu");
    frame2.setContentPane(MainPanel);
    frame2.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
    frame2.pack();
    frame2.setVisible(true);

}

}

物理模拟课程(主图和图形绘制)

public class PhysicsSim extends JComponent {




public static void main(String[] args) {



    //building frame to fit correct screen size

    JFrame frame1 = new JFrame("Projectile Motion - Animation");
    PhysicsSim sim = new PhysicsSim();
    frame1.add(sim);
    frame1.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame1.setLocationRelativeTo(null);
    frame1.setVisible(true);
    frame1.setExtendedState(frame1.MAXIMIZED_BOTH);


    GUIForm1 guiForm1 = new GUIForm1();



}



public void paintComponent(Graphics g) {
    //background
    g.setColor(new Color(149, 179, 215));
    g.fillRect(0, 0, getWidth(), getHeight());
    super.paintComponent(g);

    //axis
    g.setColor(Color.BLACK);
    g.fillRect(50, 0, 5, getHeight());
    g.fillRect(0, getHeight() - 50, getWidth(), 5);


    int axisY = 0;
    int axisX = 0;
    int axisYCounter = 1;
    int axisXCounter = 1;

    //axis markers
    while (axisYCounter <= getHeight() / 10) {
        g.fillRect(35, axisY, 20, 5);
        axisY = axisY + 50;
        axisYCounter++;
    }

    while (axisXCounter <= getWidth() / 10) {
        g.fillRect(axisX, getHeight() - 50, 5, 20);
        axisX = axisX + 50;
        axisXCounter++;
    }
    //projectile

    g.setColor(c);
    g.fillOval(getWidth() - 1885, getHeight() - 60, 30, 30);




}
public Color c = Color.WHITE;


}

0 个答案:

没有答案