【发布时间】:2015-04-15 10:17:39
【问题描述】:
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) 使用逻辑一致的形式缩进代码行和块。缩进是为了让代码流更容易理解! 2) 曾经只需要源代码中的一个空白行。
{之后或}之前的空行通常也是多余的。 3)使用CardLayout,如this answer所示。 -
设置组件的边界是布局管理器的工作。不要打电话给
setBounds(),除非你正在实施。 -
您可以编辑代码并展示如何操作吗? @安德鲁
标签: java swing jframe jpanel jbutton