我正在尝试进行卡片布局。现在我甚至无法在两个屏幕之间切换。我希望第一个屏幕有三个顶部按钮。当点击添加问题时,我希望显示第二个屏幕,一旦我此问题,我就会编码。现在没有任何按钮出现。
public class BookQuizGUI extends JFrame implements ActionListener
{
private JFrame f;
private JPanel card;
private JPanel home;
private JPanel contentPane;
private JPanel pnlButtons;
private JButton addQs;
private JButton takeQuiz;
private JButton quit;
private CardLayout cardLayout;
private JPanel addQ;
private JPanel pnlButtons2;
private JButton add;
private JButton writeAll;
private JButton done;
private JButton quit2;
/**
*
*/
public BookQuizGUI()
{
//The main screen for when the program starts
f = new JFrame();
f.setTitle("Book Quiz");
f.setSize(800, 400);
f.setLocation(400, 250);
f.setDefaultCloseOperation(EXIT_ON_CLOSE);
contentPane = (JPanel) f.getContentPane();
card = new JPanel();
cardLayout = new CardLayout();
card.setLayout(cardLayout);
home = new JPanel();
pnlButtons = new JPanel();
pnlButtons.setLayout(new GridLayout(1, 3));
addQs = new JButton("Add Questions");
takeQuiz = new JButton("Take Quiz");
quit = new JButton("Quit");
pnlButtons.add(addQs);
pnlButtons.add(takeQuiz);
pnlButtons.add(quit);
home.add(pnlButtons);
card.add(home);
f.add(card);
cardLayout.show(card, "Home");
addQ = new JPanel();
addQ.setLayout(new GridLayout(1, 2));
pnlButtons2 = new JPanel();
add = new JButton("Add");
writeAll = new JButton("Write All");
done = new JButton("Done");
quit2 = new JButton("Quit");
pnlButtons2.add(add);
pnlButtons2.add(writeAll);
pnlButtons2.add(done);
pnlButtons2.add(quit2);
addQ.add(pnlButtons, BorderLayout.SOUTH);
addQs.addActionListener(this);
takeQuiz.addActionListener(this);
quit.addActionListener(this);
f.setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args)
{
BookQuizGUI gui = new BookQuizGUI();
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == quit)
{
System.exit(0);
}
if(e.getSource() == addQs)
{
}
}
}
请,任何帮助表示赞赏。
答案 0 :(得分:3)
您没有正确地将组件添加到使用CardLayout的容器中。根据我知道您有链接的教程,您需要使用键字符串添加组件。所以这个:
card.add(home);
f.add(card);
cardLayout.show(card, "Home");
应该是:
card.add(home, "Home");
f.add(card);
cardLayout.show(card, "Home");
我自己,我更喜欢使用字符串常量而不是字符串文字,以减少错误。
你还要将pnlButtons添加到多个容器中 - 不要这样做,因为它会让你的gui起来。您可以将组件添加到一个且仅一个容器中。
所以改变
addQ.add(pnlButtons, BorderLayout.SOUTH);
到
addQ.add(pnlButtons2, BorderLayout.SOUTH);
例如:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class BookQuizGUI extends JFrame implements ActionListener {
private static final String HOME = "home";
private static final String ADD_Q = "add q";
private JFrame f;
private JPanel card;
private JPanel home;
private JPanel contentPane;
private JPanel pnlButtons;
private JButton addQs;
private JButton takeQuiz;
private JButton quit;
private CardLayout cardLayout;
private JPanel addQ;
private JPanel pnlButtons2;
private JButton add;
private JButton writeAll;
private JButton done;
private JButton quit2;
public BookQuizGUI() {
f = new JFrame();
f.setTitle("Book Quiz");
// f.setSize(800, 400);
f.setLocation(400, 250);
f.setDefaultCloseOperation(EXIT_ON_CLOSE);
contentPane = (JPanel) f.getContentPane();
home = new JPanel();
pnlButtons = new JPanel();
pnlButtons.setLayout(new GridLayout(1, 3));
addQs = new JButton("Add Questions");
takeQuiz = new JButton("Take Quiz");
quit = new JButton("Quit");
pnlButtons.add(addQs);
pnlButtons.add(takeQuiz);
pnlButtons.add(quit);
home.add(pnlButtons);
addQ = new JPanel();
addQ.setLayout(new GridLayout(1, 2));
pnlButtons2 = new JPanel();
add = new JButton("Add");
writeAll = new JButton("Write All");
done = new JButton("Done");
quit2 = new JButton("Quit");
pnlButtons2.add(add);
pnlButtons2.add(writeAll);
pnlButtons2.add(done);
pnlButtons2.add(quit2);
addQ.add(pnlButtons2, BorderLayout.SOUTH);
addQs.addActionListener(this);
takeQuiz.addActionListener(this);
quit.addActionListener(this);
card = new JPanel();
cardLayout = new CardLayout();
card.setLayout(cardLayout);
card.add(home, HOME);
card.add(addQ, ADD_Q);
cardLayout.show(card, HOME);
f.add(card);
f.pack();
f.setVisible(true);
}
public static void main(String[] args) {
new BookQuizGUI();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == quit) {
System.exit(0);
}
if (e.getSource() == addQs) {
cardLayout.show(card, ADD_Q);
}
}
}