![](/img/trans.png)
[英]Thymeleaf + Spring Boot. I try to make dynamic fields, but when the button is pressed nothing happens at all
[英]I am using Card Layout to switch between panels but when I try to make a specific panel appear after pressing a button nothing happens
我試圖獲取它,以便在按下下一個按鈕時轉到“客戶”頁面,但是當我單擊該按鈕時,什么都沒有發生。
這是我的代碼包javaapplication1;
import javax.swing.*;
import java.lang.*;
import java.awt.*;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
public class Test extends javax.swing.JPanel implements ActionListener{
JButton firstButton, lastButton, nextButton, previousButton;
JPanel cardPanel;
String cardNames[] = new String[3];
int cardCounter = 0;
public JPanel createContentPane (){
JPanel totalGUI = new JPanel();
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
buttonPanel.add(Box.createRigidArea(new Dimension(10,0)));
previousButton = new JButton("<- Previous");
previousButton.addActionListener(this);
buttonPanel.add(previousButton);
buttonPanel.add(Box.createHorizontalGlue());
firstButton = new JButton("First");
firstButton.addActionListener(this);
buttonPanel.add(firstButton);
buttonPanel.add(Box.createHorizontalGlue());
lastButton = new JButton("Last");
lastButton.addActionListener(this);
buttonPanel.add(lastButton);
buttonPanel.add(Box.createHorizontalGlue());
nextButton = new JButton("Next ->");
nextButton.addActionListener(this);
buttonPanel.add(nextButton);
buttonPanel.add(Box.createRigidArea(new Dimension(10,0)));
JPanel Shop = new JPanel();
Shop panel1 = new Shop();
Shop.add(panel1);
JPanel Items = new JPanel();
Items panel2 = new Items();
Items.add(panel2);
JPanel Customers = new JPanel();
Customers panel3 = new Customers();
Customers.add(panel3);
cardPanel = new JPanel(new CardLayout(150, 50));
cardNames[0] = "Component.CENTER";
cardNames[1] = "Component.BOTTOM_ALIGNMENT";
cardNames[2] = "Component.TOP_ALIGNMENT";
cardPanel.add(Shop, cardNames[0]);
cardPanel.add(Items, cardNames[1]);
cardPanel.add(Customers, cardNames[2]);
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new BorderLayout());
bottomPanel.add(cardPanel, BorderLayout.CENTER);
bottomPanel.add(buttonPanel, BorderLayout.PAGE_START);
totalGUI.add(bottomPanel);
return totalGUI;
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == nextButton)
{
CardLayout cl = (CardLayout)(cardPanel.getLayout());
cl.show(cardPanel, "Customers");
}
/*
if(e.getSource() == firstButton)
{
cl.first(cardPanel);
cardCounter = 0;
}
else if(e.getSource() == lastButton)
{
cl.last(cardPanel);
cardCounter = 2;
}
else
}
else if(e.getSource() == previousButton)
{
cl.previous(cardPanel);
if(cardCounter > 0)
{
cardCounter--;
}
else
{
cardCounter = 2;
}*/
}
protected static void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("");
Test demo = new Test();
frame.setContentPane(demo.createContentPane());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
為什么會這樣呢? 我想念什么嗎? 我只是使用Cardlayout的新手,不知道它是如何工作的
如有疑問,請查閱文檔 。
從CardLayout.addLayoutComponent :
constraints
指定的對象必須是字符串。 卡布局將此字符串存儲為鍵值對,可用於隨機訪問特定卡。 通過調用show
方法,應用程序可以顯示具有指定名稱的組件。
換句話說,傳遞給CardLayout.show的String 必須與將組件添加到CardLayout時作為約束條件提供的字符串之一相同。
您使用的約束是:
這些都不符合您傳遞給CardLayout.show的內容:
cl.show(cardPanel, "Customers");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.