将所有java swing gui放在一个类中是正常的吗? [英] Is it normal to have all java swing gui in one class?

查看:117
本文介绍了将所有java swing gui放在一个类中是正常的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用摇摆开发并遇到问题。将整个GUI放入单个类是否正常?我正在构建的应用程序有一个单独的JFrame,它显示多个不同的页面。例如,如果用户点击按钮,则他们将被带到具有不同布局的完全不同的页面。我已经配置了卡片布局,到目前为止我构建的一张卡片使用的是GridBag布局。



我所拥有的问题是

1.每个页面是否应该有自己的类?

2.如果他们如何在运行卡片布局的GUI控制器和各个页面之间进行通信?

3.或者我应该将所有GUI放入GUI控制器让它像那样运行。



以下是我到目前为止所拥有的代码,我是新手,并且真的想要擅长这一点,所以如果你发现我错过的任何重大问题请随时指出它们。



单页代码:

  public class HomePage扩展JPanel实现ActionListener {
private GridBagLayout gl;
私人JPanel frm;
JButton newPersonalContact;
首页(){
frm = new JPanel();
gl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
frm.setLayout(gl);

newPersonalContact = new JButton(New Personal Contact);
JButton newBusinessContact = new JButton(New Business Contact);
JButton showAllContacts = new JButton(Show All Contacts);
JButton saveAndQuit = new JButton(Save and Quit);

JPanel top = new JPanel();
top.setBackground(new Color(218,165,32));
top.add(新JLabel(西部管理者大学礼物:));


JPanel middle = new JPanel();
middle.setBackground(new Color(43,47,85));
GridLayout ge = new GridLayout(4,4);
middle.setLayout(ge);

middle.add(new JLabel());
middle.add(new JLabel());
middle.add(new JLabel());
middle.add(new JLabel());
middle.add(new JLabel());
middle.add(newPersonalContact);


middle.add(newBusinessContact);
middle.add(new JLabel());
middle.add(new JLabel());
middle.add(showAllContacts);

middle.add(saveAndQuit);
middle.add(new JLabel()); middle.add(new JLabel());
middle.add(new JLabel());
middle.add(new JLabel());

JPanel bottom = new JPanel();
bottom.setBackground(new Color(218,165,32));

gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1.0;

gbc.weighty = 2.0;
gbc.gridx = 0;
gbc.gridy = 0;
frm.add(top,gbc);

gbc.weighty = 6.0;
gbc.gridx = 0;
gbc.gridy = 1;
frm.add(middle,gbc);

gbc.weighty = 1.0;
gbc.gridx = 0;
gbc.gridy = 2;
frm.add(bottom,gbc);

newPersonalContact.addActionListener(this);
newBusinessContact.addActionListener(this);
showAllContacts.addActionListener(this);
saveAndQuit.addActionListener(this);


}
public void actionPerformed(ActionEvent ae){
if(ae.getSource()== newPersonalContact){
//这里有什么?
}

}
public JPanel getFrame(){
return frm;
}

}

GUI控制器的代码:

  public class GUIController {
JFrame frm;
CardLayout cl;
容器窗格;
GUIController(){
frm = new JFrame();
frm.setSize(800,600);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel card2 = new JPanel();
card2.setBackground(Color.black);

JPanel cards = new JPanel(new CardLayout());
cards.add(new HomePage()。getFrame(),Home);
cards.add(card2,New Personal Contact);
pane = frm.getContentPane();
pane.add(cards,BorderLayout.CENTER);





}
public void start(){
this.frm.setVisible(true);

}
public void showCard(){
cl.show(pane,Card2);
}


}

虽然这是对于学校项目,GUI界面不仅仅是我有兴趣学习的东西。感谢您的帮助。

解决方案

不,我不建议将所有GUI代码放在课堂上 - 至少如果它得到了在这种情况下使用Mediator模式:




  • 将子组件放入JPanel的自定义子类

  • 该子类侦听其子项的事件并更新其他子项

  • 根据需要触发自己的自定义事件



另请注意:如果您要学习GUI编程并且没有特定要求使用Swing,请考虑使用更新版本GUI工具包 JavaFX


I am just starting out with swing development and am having an issue. Is it normal to put the entire GUI into a single class? The application that I am building has one single JFrame that displays multiple different "pages". For example if the user clicks on a button they are taken to an entirely different page with a different layout. I've configured a Card Layout, and the one card that I have build so far uses the GridBag layout.

The question that I have, then, is
1. whether or not each page should have its own class?
2. If they do how do I communicate between the GUI controller which runs the card layout and the individual pages?
3. Or should I just put all of the GUI into the GUI controller and let it run like that.

Below is the code for what I have so far, I am new to this and really would like to get good at it so if you spot any major issues that I missed please feel free to point them out.

Code for the individual page:

public class HomePage extends JPanel implements ActionListener{
    private GridBagLayout gl;
    private JPanel frm;
    JButton newPersonalContact;
    HomePage(){
         frm=new JPanel();
         gl=new GridBagLayout();
         GridBagConstraints gbc=new GridBagConstraints();
         frm.setLayout(gl);

         newPersonalContact=new JButton("New Personal Contact");
         JButton newBusinessContact=new JButton("New Business Contact");
         JButton showAllContacts=new JButton("Show All Contacts");
         JButton saveAndQuit=new JButton("Save and Quit");

         JPanel top=new JPanel();
         top.setBackground(new Color(218,165,32));
         top.add(new JLabel("Western Governers University Presents:"));


         JPanel middle=new JPanel();
         middle.setBackground(new Color(43,37,85));
         GridLayout ge=new GridLayout(4,4);
         middle.setLayout(ge);

         middle.add(new JLabel(""));
         middle.add(new JLabel(""));
         middle.add(new JLabel(""));
         middle.add(new JLabel(""));
         middle.add(new JLabel(""));
         middle.add(newPersonalContact);


         middle.add(newBusinessContact);
         middle.add(new JLabel(""));
         middle.add(new JLabel(""));
         middle.add(showAllContacts);

         middle.add(saveAndQuit);
         middle.add(new JLabel(""));middle.add(new JLabel(""));
         middle.add(new  JLabel(""));
         middle.add(new JLabel(""));

         JPanel bottom=new JPanel();
         bottom.setBackground(new Color(218,165,32));

         gbc.fill=GridBagConstraints.BOTH;
         gbc.weightx=1.0;

         gbc.weighty=2.0;
         gbc.gridx=0;
         gbc.gridy=0;
         frm.add(top,gbc);

         gbc.weighty=6.0;
         gbc.gridx=0;
         gbc.gridy=1;
         frm.add(middle,gbc);

         gbc.weighty=1.0;
         gbc.gridx=0;
         gbc.gridy=2;
         frm.add(bottom,gbc);

         newPersonalContact.addActionListener(this);
         newBusinessContact.addActionListener(this);
         showAllContacts.addActionListener(this);
         saveAndQuit.addActionListener(this);


}
    public void actionPerformed(ActionEvent ae){
    if (ae.getSource()==newPersonalContact){
        //What goes here?
    }

}
public JPanel getFrame(){
    return frm;
}

}

code for the GUI controller:

public class GUIController {
    JFrame frm;
    CardLayout cl;
    Container pane;
GUIController(){
frm=new JFrame();
frm.setSize(800,600);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel card2=new JPanel();
card2.setBackground(Color.black);

JPanel cards=new JPanel(new CardLayout());
cards.add(new HomePage().getFrame(), "Home");
cards.add(card2,"New Personal Contact");
pane=frm.getContentPane();
pane.add(cards,BorderLayout.CENTER);





}
public void start(){
    this.frm.setVisible(true);

}
public void showCard(){
    cl.show(pane, "Card2");
}


}

While this is for a school Project, the GUI interface is not required just something I am interested in learning. Thanks for any help.

解决方案

No, I don't recommend to put all GUI code in class - at least if it gets big and does several things.

In that case use the Mediator pattern:

  • put subcomponents to custom subclasses of JPanel
  • that subclass listens for events of its children and updates other children
  • fires own custom events if needed

Also note: If you're about to learn GUI programming and don't have a specific requirement to use Swing, consider to use the newer GUI toolkit JavaFX.

这篇关于将所有java swing gui放在一个类中是正常的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆