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参数后,按钮也不会在正确的位置移动。
最佳答案
尝试使用CardLayout
,这是正在使用卡布局的代码...注意:您缺少按钮上的一些操作来返回主屏幕,我留给您! :-)
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();
}
}