首页 > 解决方案 > 我想通过单击按钮将面板插入到 jframe

问题描述

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

class Button extends JButton{
    Button(){}
    Button(String text,ImageIcon icon,Dimension dm,Point point){
    this.setText(text);
    this.setIcon(icon);
    this.setSize(dm);
    this.setBorder(BorderFactory.createLineBorder(Color.WHITE));
    this.setHorizontalTextPosition(SwingConstants.RIGHT);
    this.setIconTextGap(50);
    this.setFont(new Font(null, Font.PLAIN, 14));
    this.setFocusable(false);
    this.setLocation(point);
    this.setBackground(null);
    }
}

class MenuPanel extends JPanel implements ActionListener{

    public MenuPanel(int width,int height,Color color){
        bDimension = new Dimension(196,40);
        addIcon =new ImageIcon(getClass().getResource("addUser.png"));
        editIcon =new ImageIcon(getClass().getResource("editUser.png"));

        this.setLayout(null);
        this.setPreferredSize(new Dimension(width,height));
        this.setBackground(color);

        //THE LOGO
        this.add(new LogoPanel(new Dimension(196, 150), this.getBackground(),new Point(2, 2)));

        //THE ADD BUTTON
        btnAdd =new Button("Add User",addIcon,bDimension,new Point(2,this.getComponent(0).getHeight()+4));
        btnAdd.addActionListener`enter code here`(this);
        this.add(btnAdd);


        //THE EDIT BUTTON
        btnEdit =new Button("Edit User",editIcon,bDimension,new Point(2,this.getComponent(0).getHeight()+this.getComponent(1).getHeight()+8));
        btnEdit.addActionListener(this);
        this.add(btnEdit);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==btnAdd) {
            JPanel addUser =new JPanel();
            addUser.setBackground(Color.BLUE);
            addUser.setPreferredSize(new Dimension(400,100));
            addUser.add(new JButton("Button"));

            //NOT SURE WHAT I'M DOING HERE. DOESN'T WORK
            this.getParent().add(addUser,BorderLayout.CENTER);


        }else if(e.getSource()==btnEdit) {
            JPanel editUser =new JPanel();
            editUser.setBackground(Color.RED);
            editUser.setPreferredSize(new Dimension(400,100));
            editUser.add(new JButton("Button"));

            //NOT SURE WHAT I'M DOING HERE. DOESN'T WORK
            this.getParent().add(editUser,BorderLayout.CENTER);
        }
    }

    private Button btnAdd;
    private Button btnEdit;
    private ImageIcon addIcon;
    private ImageIcon editIcon;
    private Dimension bDimension;
}


class LogoPanel extends JPanel{
    public LogoPanel(Dimension dimension,Color color,Point point){
        btnLogo =new JLabel();
        btnLogo.setText("Your Name");
        btnLogo.setIcon(new ImageIcon(getClass().getResource("user.png")));
        btnLogo.setOpaque(true);
        btnLogo.setIconTextGap(10);
        btnLogo.setHorizontalTextPosition(0);
        btnLogo.setVerticalTextPosition(3);
        btnLogo.setHorizontalAlignment(0);
        btnLogo.setVerticalAlignment(0);

        this.setLayout(new BorderLayout());
        this.setBorder(BorderFactory.createLineBorder(Color.WHITE));
        this.setSize(dimension);
        this.setBackground(color);
        this.add(btnLogo,BorderLayout.CENTER);
        this.getComponent(0).setFont(new Font(null, Font.PLAIN, 14));
        this.getComponent(0).setFocusable(false);
        this.setLocation(point);
    }
    JLabel btnLogo;
}

public class Window extends JFrame {
    public Window(){
        this.setTitle(null);
        this.setIconImage(null);
        this.setLayout(new BorderLayout(0,2));
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setPreferredSize(new Dimension(new Dimension(960,500)));
        this.setMinimumSize(new Dimension(960,500));
        this.getContentPane().setBackground(new Color(250, 250, 250));

        //ADD PANELS
        this.add(new MenuPanel(200, 0, new Color(238, 238, 238)),BorderLayout.WEST);

        this.setLocationRelativeTo(null);
    }
} 

上面的代码创建了一个带有边框布局和一个向西对齐的菜单面板的 jframe。菜单面板有两个 jbuttons。我想要实现的是,单击任一按钮,从而将面板插入到 jframe 的另一侧。有人可以帮助/教我如何实现这一目标。我对编程非常陌生,尤其是在 Java 方面。

标签: java

解决方案


推荐阅读