我想这样做,所以只有在cardPanel上没有的动作完成后才能看到cardPanels。例如,当您在该窗口上选择某个Jradiobutton时,会打开一个窗口。我想使用setVisible(boolean)来做到这一点。但是,setVisible由于某种原因不起作用。我有什么遗失的吗?
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class MainFrame extends JFrame {
private JFrame frame = new JFrame("Swing Refresh Bug?");
private Container contentPane = frame.getContentPane();
private JPanel cardPanel = new JPanel();
private CardLayout cardLayout = new CardLayout();
private Component currentComponent;
private JButton next;
MainFrame() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// properties of the cardPanel
cardPanel.setLayout(cardLayout);
cardPanel.add(new JLabel("One"), "One");
cardPanel.add(new JLabel("Two"), "Two");
cardPanel.add(new JLabel("Three"), "Three");
cardPanel.setVisible(false);
// Create a radio button
JRadioButton addNext = new JRadioButton("Add next");
// Add the radio buttons listener
addNext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cardLayout.show(cardPanel, "One");
}
});
// Set the layout of the content pane.
contentPane.setLayout(new BorderLayout());
contentPane.add(cardPanel, BorderLayout.CENTER);
contentPane.add(addNext, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
public MainFrame(String title) {
MainFrame mf = new MainFrame();
}
}
答案 0 :(得分:0)
你的类不需要扩展JFrame,你已经在类中创建了JFrame,如果你想在卡片上移动然后按如下方式更改ActionListener
addNext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cardLayout.next(cardPanel);
}
});
答案 1 :(得分:-1)
在ActionListener中初始化它,而不是在构造函数中。在此之后你还必须说setVisible(true)。我认为你可以制作扩展JPannel的CardPannel类。这样做更有意义。而不是在MainFrame构造函数中添加JLabel,而是在CardPannel构造函数中执行它。我在手机上,所以我现在无法向您显示代码。我希望我能帮忙。