有人可以告诉我为什么当我点击StartScreen
面板中的创建按钮时没有任何反应?基本上,我有一个JFrame
,而JFrame
我有JPanel
使用CardLayout
。我还有另外两个JPanel
,称为StartScreen
和CreatePanel
。在StartScreen
面板中,我有两个JButton
,其中一个名为_createBtn
,当我点击该按钮时,我希望CreatePanel
显示在我的{{1}上在我的_panelContainer
课程中使用CardLayout
。我只是不明白为什么它不起作用,有人可以告诉我在我的代码中我做错了什么吗?谢谢。
这是我的MainFrame
课程:
MainFrame
以下是我的package View;
import java.awt.CardLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainFrame extends JFrame{
/**
* generated serial
*/
private static final long serialVersionUID = 1L;
/**
* window's width
*/
protected static final int WIDTH = 1200;
/**
* window's height
*/
protected static final int HEIGHT = 800;
/**
* panel container
*/
private JPanel _panelContainer = new JPanel();
/**
* card layout
*/
private CardLayout _cardLayout = new CardLayout();
public MainFrame() {
setTitle("HighBid");
setSize(WIDTH,HEIGHT);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
addComponents();
}
/**
* Adding components like JPanel here.
*/
private void addComponents() {
StartScreen start = new StartScreen();
CreatePanel create = new CreatePanel();
_panelContainer.setLayout(_cardLayout);
_panelContainer.add(start, "StartScreen");
_panelContainer.add(create, "CreatePanel");
_cardLayout.show(_panelContainer, "StartScreen");
this.setContentPane(_panelContainer);
}
/**
* Switching between panels.
*/
public void swapPanel(String next) {
if("StartScreen".equals(next)) {
_cardLayout.show(_panelContainer, "StartScreen");
} else if ("CreatePanel".equals(next)) {
_cardLayout.show(_panelContainer, "CreatePanel");
}
}
}
小组:
StartScreen
以下是我的package View;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
* Start screen JPanel
*
* @author
* @version 5/19/2015
*/
public class StartScreen extends JPanel {
/**
* default serial
*/
private static final long serialVersionUID = 1L;
/**
* Start screen's label
*/
private JLabel _label;
/**
* create button
*/
private JButton _createBtn;
/**
* open button
*/
private JButton _openBtn;
public StartScreen() {
setSize(MainFrame.WIDTH, MainFrame.HEIGHT);
setLayout(null);
setComponents();
addComponents();
setListeners();
}
private void setListeners() {
_createBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
MainFrame mf = new MainFrame();
JButton src = (JButton) e.getSource();
if(src.equals(_createBtn)) {
mf.swapPanel("CreatePanel");
} else {
mf.swapPanel("StartScreen");
}
}
});
}
/**
* Setting for each component in this panel.
*/
private void setComponents() {
// Set HighBid Label
_label = new JLabel("Welcome to HighBid");
_label.setFont(new Font("Tahoma", 0, 70));
_label.setBounds((int)(MainFrame.WIDTH / 4.5), 100,700,100);
_label.setForeground(Color.BLUE);
// Set create button
_createBtn = new JButton("Create Auction");
_createBtn.setBounds((int)(MainFrame.WIDTH / 4.5), 350, 270, 100);
_createBtn.setFont(new Font("Tahoma", 0, 36));
// Set open button
_openBtn = new JButton("Open Auction");
_openBtn.setBounds((int)(MainFrame.WIDTH / 1.9), 350, 270, 100);
_openBtn.setFont(new Font("Tahoma", 0, 36));
}
/**
* Add the component to the panel.
*/
private void addComponents() {
this.add(_label);
this.add(_createBtn);
this.add(_openBtn);
}
}
小组:
CreatePanel
这是我的主要课程:
package View;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class CreatePanel extends JPanel {
/**
* Default serial.
*/
private static final long serialVersionUID = 1L;
/**
* label for create panel's form
*/
private JLabel _createLabel;
/**
* back button
*/
private JLabel _backBtn;
public CreatePanel() {
setSize(MainFrame.WIDTH, MainFrame.HEIGHT);
setLayout(null);
setListeners();
}
private void setListeners() {
}
}
答案 0 :(得分:1)
您在mnciitbhu 15
mafiya69 20
的{{1}}中创建了MainFrame
的新实例,ActionListener
的此实例与可见的窗口没有任何关系给用户。
您应该拥有某种控制器,负责根据当前视图和用户输入确定应采取的操作过程。然后它将负责更新主视图。
每个子视图都将被赋予该控制器的相同实例,并且需要与它进行通信以了解它想要做什么。
例如: