Gee*_*ali 5 java swing jpanel jframe
我有一个JFrame和Jpanel超过在其中various buttons的placed.so 上的一个按钮的点击我已经被称为new class是also having containers placed in a Jpanel.所以我想show这new class panel在main Jframe panel.如何我能做到这一点?
如果我们在其中使用卡片布局,那么我如何使用它作为点击按钮我已经调用了新类的对象.如
Card layout consider each component in a container as card and i want whole Jpanel as a card so is it possible to do that???
我们可以在其中嵌套Jpanels吗?
请建议我这样做的正确方法?
这是SSCCE:
// this is the main class on which i want to use panel of other class
public class mymain
{
JFrame jframe = new JFrame();
JPanel panel = new JPanel();
BorderLayout borderlayout = new BorderLayout();
public mymain()
{
jframe.setLayout(borderlayout);
JMenuBar menubar = new JMenuBar();
jframe.setJMenuBar(menubar);
JButton home_button = new JButton("HOME");
menubar.add(home_button);
jframe.getContentPane().add(panel,BorderLayout.CENTER);
panel.setLayout(new GridBagLayout());
//here used containers over that frame
and call it from main()
Run Code Online (Sandbox Code Playgroud)
}
here is another class to manage category is
public class manageCategory
{
JPanel panel = new JPanel();
GridBagLayout gridbglayout = new GridBagLayout();
GridBagConstraints gridbgconstraint = new GridBagConstraints();
public manageCategory()
{
panel.setLayout(new BorderLayout());
// i have again here used containers placed with grid bag layout
}
Run Code Online (Sandbox Code Playgroud)
}
所以现在我想要,因为我click on home button在mymain课堂panel上使用的那个manageCategory()应该是displayed在同一个面板上.当我再次点击home按钮然后mymain panel得到显示.我可以这样做吗???
我建议您使用 aCardLayout来完成此任务。
JPanel使用“类”更新了示例:static class MainPanel extends JPanel {
public MainPanel(final Container frame) {
add(new JButton(new AbstractAction("Click to view next") {
@Override
public void actionPerformed(ActionEvent e) {
frame.add(new NextPanel(), "NextPanel");
((CardLayout) frame.getLayout()).show(frame, "NextPanel");
}
}));
}
}
static class NextPanel extends JPanel {
public NextPanel() {
add(new JLabel("Next page in the card layout"));
}
}
public static void main(String[] args) throws Exception {
JFrame frame = new JFrame("Test");
frame.setLayout(new CardLayout());
frame.add(new MainPanel(frame.getContentPane()), "MainPanel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setVisible(true);
}
Run Code Online (Sandbox Code Playgroud)