jbuttons中的波动

时间:2015-04-15 10:17:39

标签: java swing jframe jpanel jbutton

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;




public class Library {
JFrame frame = new JFrame("Library Management - MENU");
JButton button1 = new JButton();
JButton button2 = new JButton();
JButton button3 = new JButton();
JButton button4 = new JButton();
JButton button5 = new JButton();
JButton button6 = new JButton();
JButton button7 = new JButton();

/**
 */
public Library()
{



        JPanel panel = new JPanel();

        panel.setLayout(new FlowLayout());



        JLabel label = new JLabel("MENU");

        panel.add(label);



        button1.setText("ISSUE a BOOK");
            button1.setBounds(100,100,200,30);
        panel.add(button1);







        button2.setText("RETURN a BOOK");
            button2.setBounds(200,200,200,30);
            panel.add(button2);



        button3.setText("UPDATE/SEARCH RECORD");
            button3.setBounds(300,300,200,30);
            panel.add(button3);



        frame.add(panel);
            button1.addActionListener((ActionEvent e) -> {
                frame.setTitle("ISSUE");

                JPanel panel1 = new JPanel();

                panel1.setLayout(new FlowLayout());

                button6.setText("ISSUE a BOOK on CARD1");
                button6.setBounds(100,100,200,30);
                panel1.add(button6);

                button7.setText("ISSUE a BOOK on CARD2");
                button7.setBounds(100,100,200,30);
                panel1.add(button7);
                frame.add(panel1);

                frame.setVisible(true);
            });
            button2.addActionListener((ActionEvent e) -> {
                frame.setTitle("RETRUN");

                JPanel panel1 = new JPanel();

                panel1.setLayout(new FlowLayout());

                button4.setText("RETURN a BOOK on CARD1");
                button4.setBounds(100,100,200,30);
                panel1.add(button4);

                button5.setText("RETURN a BOOK on CARD2");
                button5.setBounds(100,100,200,30);
                panel1.add(button5);
                frame.add(panel1);

                frame.setVisible(true);
            });
        frame.setSize(500,500);

        frame.setLocationRelativeTo(null);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        frame.setVisible(true);
  }



 public static void main(String[] args) {


    Library obj=new Library();
 }




}

我正在创建一个图书馆管理应用程序,我已经在一个框架中创建了多个jpanel,但是当我离开时 面板到另一个面板波动,以前使用的按钮重叠 当前按钮。即使更改了setBounds参数,即使按钮也没有在适当的位置移动。

1 个答案:

答案 0 :(得分:2)

尝试使用CardLayout,这是您使用卡片布局的代码...注意:您在按钮上缺少一些操作以返回MAIN屏幕,我会留给您! : - )

import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Library {

    JFrame frame = new JFrame("Library Management - MENU");
    JButton button1 = new JButton();
    JButton button2 = new JButton();
    JButton button3 = new JButton();
    JButton button4 = new JButton();
    JButton button5 = new JButton();
    JButton button6 = new JButton();
    JButton button7 = new JButton();

    public Library() {

        JPanel cards = new JPanel(new CardLayout());

        JPanel firstPanel = new JPanel();
        JPanel secondPanel = new JPanel();
        JPanel thirdPanel = new JPanel();

        //Init some components...
        JLabel label = new JLabel("MENU");
        button1.setText("ISSUE a BOOK");
        button2.setText("RETURN a BOOK");
        button3.setText("UPDATE/SEARCH RECORD");
        button4.setText("RETURN a BOOK on CARD1");
        button5.setText("RETURN a BOOK on CARD2");
        button6.setText("ISSUE a BOOK on CARD1");
        button7.setText("ISSUE a BOOK on CARD2");

        //First panel setup
        firstPanel.setLayout(new FlowLayout());
        firstPanel.add(label);
        firstPanel.add(button1);
        firstPanel.add(button2);
        firstPanel.add(button3);

        //Second panel setup
        secondPanel.setLayout(new FlowLayout());
        secondPanel.add(button6);
        secondPanel.add(button7);

        //Third panel setup
        thirdPanel.setLayout(new FlowLayout());
        thirdPanel.add(button4);
        thirdPanel.add(button5);

        //Show ISSUE on click of button1 
        button1.addActionListener((ActionEvent e) -> {
            //Change cards to ISSUE panel
            frame.setTitle("ISSUE");
            CardLayout cl = (CardLayout) (cards.getLayout());
            cl.show(cards, "ISSUE");
        });

        //Show RETURN on click of button2
        button2.addActionListener((ActionEvent e) -> {
            frame.setTitle("RETRUN");
            CardLayout cl = (CardLayout) (cards.getLayout());
            cl.show(cards, "RETRUN");

        });

        //Add content to cardlayout JPanel
        cards.add(firstPanel, "MENU");
        cards.add(secondPanel, "ISSUE");
        cards.add(thirdPanel, "RETURN");

        frame.add(cards);

        //Initial card to show...
        CardLayout cl = (CardLayout) (cards.getLayout());
        cl.show(cards, "MENU");

        //Frame constraints
        frame.setSize(500, 500);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void main(String[] args) {

        Library obj = new Library();
    }

}