如果在jframe中单击按钮,则更改jpanel

时间:2016-09-06 23:19:20

标签: java oop user-interface jpanel jbutton

我是GUI的初学者,我有一个程序,左边有菜单,是jpanel里面的jbuttons。如果单击菜单中的其中一个按钮,如何更改右侧的jpanel?这是我的代码:

我的菜单:

import * as Rx from "rxjs/Rx";

const asyncOp = () => Rx.Observable.interval(300).take(1);

Rx.Observable.from(["a", "b"])

    // Perform the async operation on the values emitted from the
    // observable and map the emitted value and async result into
    // an object.

    .mergeMap((key) => asyncOp().map((result) => ({ key, result })))

    // Use reduce to build an object containing the emitted values
    // (the keys) and the async results.

    .reduce((acc, value) => { acc[value.key] = value.result; return acc; }, {})
    .subscribe((value) => { console.log(value); });

为我的主框架

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Menu extends JPanel implements ActionListener{
    static JButton btnRegister, btnVote, btnResult, btnView, btnSearch, btnLogout;
    JLabel lblLogo, lblTitle;
    ImageIcon imgLogo;
    static JPanel panelLogo, panelTitle, panelButtons, panelMENU;

    Menu(){
        setLayout(new BorderLayout());
        setPreferredSize(new Dimension(320, 700));

        btnRegister=new JButton("REGISTER");
        btnRegister.setPreferredSize(new Dimension(320, 90));
        btnVote=new JButton("VOTE");
        btnVote.setPreferredSize(new Dimension(320, 90));
        btnResult=new JButton("PARTIAL RESULT");
        btnResult.setPreferredSize(new Dimension(320, 90));
        btnView=new JButton("VIEW RECORDS");
        btnView.setPreferredSize(new Dimension(320, 90));
        btnSearch=new JButton("SEARCH");
        btnSearch.setPreferredSize(new Dimension(320, 90));
        imgLogo=new ImageIcon("logo.png");
        lblLogo=new JLabel(imgLogo);
        lblTitle=new JLabel("ELECTION YEAR 2765");

        panelLogo=new JPanel();
        panelTitle=new JPanel();
        panelButtons=new JPanel(new GridLayout(5, 1));

        panelLogo.add(lblLogo);
        panelTitle.add(lblTitle);

        panelButtons.add(btnRegister);
        panelButtons.add(btnVote);
        panelButtons.add(btnResult);
        panelButtons.add(btnView);
        panelButtons.add(btnSearch);
        panelButtons.setBackground(Color.WHITE);

        add(panelLogo, BorderLayout.NORTH);
        add(panelTitle, BorderLayout.CENTER);
        add(panelButtons, BorderLayout.SOUTH);

        btnRegister.addActionListener(this);
        btnView.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {
        if(e.getSource().equals(btnRegister)){
            Main.frameMain.setSize(1200, 700);
            Main.frameMain.add(new PanelRegister(), BorderLayout.EAST);
            Main.frameMain.setLocationRelativeTo(null);
        }
        if(e.getSource().equals(btnVote)){}
        if(e.getSource().equals(btnResult)){}
        if(e.getSource().equals(btnView)){
            Main.frameMain.setSize(1200, 700);
            Main.frameMain.add(new PanelView(), BorderLayout.EAST);
            Main.frameMain.setLocationRelativeTo(null);
        }
        if(e.getSource().equals(Menu.btnSearch)){}
    }
}

编辑:我在reduce找到了这个答案,但我可以在import javax.swing.*; import java.awt.*; public class Main{ static JFrame frameMain; Main(){ frameMain=new JFrame(); frameMain.setLayout(new BorderLayout()); frameMain.setSize(320, 700); frameMain.setTitle("Election Year 2765"); frameMain.add(new Menu()); frameMain.setVisible(true); frameMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frameMain.setResizable(false); frameMain.setLocationRelativeTo(null); frameMain.getContentPane().setBackground(Color.WHITE); } public static void main(String [] args){ new Main(); } } 中使用吗?

0 个答案:

没有答案