gpt4 book ai didi

java - jbuttons的波动

转载 作者:行者123 更新时间:2023-11-30 08:14:09 25 4
gpt4 key购买 nike

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();
}

}

关于java - jbuttons的波动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29647620/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com