简体   繁体   English

添加到JPanel

[英]Adding onto JPanel

I was wondering if it was possible to add dropdown menus to a main JPanel from a different class instead of calling it from that class itself. 我想知道是否可以从其他类向主JPanel添加下拉菜单,而不是从该类本身调用它。 Mainly because a friend and I are working on a personal project trying to create different programs in different tabs. 主要是因为我和一个朋友正在从事一个个人项目,试图在不同的选项卡中创建不同的程序。

Here's our main GUI: 这是我们的主要GUI:

public class GUI extends JFrame {

    public GUI() {
        setTitle("Andy and Jack's favorite programs");
        JTabbedPane jtp = new JTabbedPane();
        getContentPane().add(jtp);

        JPanel jp1 = new JPanel();
        JLabel label1 = new JLabel();

        JPanel jp2 = new JPanel();
        JLabel label2 = new JLabel();

        jp1.add(label1);
        jtp.addTab("Andy - Encryption Program");
        jp2.add(label2);
        jtp.addTab("Andy - Hello World Program");
   }

   public static void main(String[] args) {
       GUI tp = new GUI();
       tp.setVisible(true);
       tp.setMinimumSize(new Dimension(400, 400));
   }

Here's one of our tabs: 这是我们的标签之一:

public class encryptionPrograms extends GUI {
    String[] options = new String[] { "XOR", "RSA" };
    ComboBox optionsList = new JComboBox(options);
    jp1.add(optionsList, BorderLayout.CENTER);
}

I'm not sure if I'm doing it correctly or not. 我不确定我是否做对了。 Just got into Java and we've have been playing around with the GUI buttons and such. 刚接触Java时,我们一直在使用GUI按钮等。

There is a lot of "wrong" here and without you saying what your intention is with adding a comboBox to your jPanel it's hard to tell you the right way to do it but yes it can be done. 这里有很多“错误”,并且无需您说出向jPanel添加comboBox的意图,很难告诉您正确的方法,但是可以做到。

But first of: Always declare your variables before you initialize them so that you can access them for other methods in the class: 但是首先:在初始化变量之前,务必先声明变量,以便可以在类中的其他方法中访问它们:

public class GUI extends JFrame{
    private JPanel jp1,jp2;
    private JLabel label1,label2;
    private JTabbedPane jtp;

    public GUI() {
        setTitle("Andy and Jack's favorite programs");
        jtp = new JTabbedPane();

        jp1 = new JPanel();
        label1 = new JLabel();

        jp2 = new JPanel();
        label2 = new JLabel();

        jp1.add(label1);
        jtp.addTab("Andy - Encryption Program", jp1);
        jp2.add(label2);
        jtp.addTab("Andy - Hello World Program",jp2);

        getContentPane().add(jtp);
   }

If you need to access a variable from another class you can write a get method for it. 如果您需要从另一个类访问变量,则可以为其编写get方法。

For example: 例如:

public JPanel getMainJPanel(){
       return jp1;
}

Now you can call the getMainJPanel() from another class in order to, for instance, add components to it. 现在,您可以从另一个类调用getMainJPanel(),例如,向其添加组件。 Just remember to .revalidate() and .repaint() the main frame after adding more components. 只要记住在添加更多组件之后对.revalidate().repaint()框架进行操作即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM