有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java Swing UI在运行时不显示

我不知道这是否是一个重复的问题,但当我运行JavaSwingGUI代码时,即使所有可见性都设置为true,也没有显示任何内容。我不熟悉JavaSwingGUI的创建,因此非常感谢您的帮助。这是我的课堂

class Pages {
    public static void Welcome_Screen() {
//         Create the main screen for adding the other panels and buttons
        JPanel Main_Screen = new JPanel(new GridBagLayout());
        GridBagConstraints Main_Screen_Constraints = new GridBagConstraints();
//         Set the size of the Main_Screen
        Main_Screen.setSize(1280, 720);

//         Create the three panels needed to make this work
//         Set the layouts as GridBagLayout and create the constraints needed
        JPanel Side_Panel = new JPanel();
        Side_Panel.setLayout(new GridBagLayout());
        GridBagConstraints Side_Panel_Constraints = new GridBagConstraints();

        JPanel Profile_Panel = new JPanel();
        Profile_Panel.setLayout(new GridBagLayout());
        GridBagConstraints Profile_Panel_Constraints = new GridBagConstraints();

        JPanel Button_Panel = new JPanel();
        Button_Panel.setLayout(new GridBagLayout());
        GridBagConstraints Button_Panel_Constraints = new GridBagConstraints();

        JPanel About_Panel = new JPanel();
        About_Panel.setLayout(new GridBagLayout());
        GridBagConstraints About_Panel_Constraints = new GridBagConstraints();

//         Change the sides of the panels to fit them correctly
        Side_Panel.setPreferredSize(new Dimension(100, 720));
        Side_Panel.setMinimumSize(new Dimension(100, 720));
        Side_Panel.setMaximumSize(new Dimension(150, 1080));
        Profile_Panel.setPreferredSize(new Dimension(100, 100));
        Button_Panel.setPreferredSize(new Dimension(100, 620));
        About_Panel.setPreferredSize(new Dimension(1180, 720));

//        Change the border types to null
        Side_Panel.setBorder(null);
        Profile_Panel.setBorder(null);
        Button_Panel.setBorder(null);
        About_Panel.setBorder(null);

//        Create the Profile_Panel items
        ImageIcon Profile_Icon = new ImageIcon("Images/Profile_Icon.png");
        JButton Profile_Button = new JButton(Profile_Icon);
        Profile_Button.setSize(100, 100);
        Profile_Panel_Constraints.gridx = 0;
        Profile_Panel_Constraints.gridy = 0;
        Profile_Panel.add(Profile_Button, Profile_Panel_Constraints);

//        Create the Buttons for the Button_Panel
        ImageIcon Java_Icon = new ImageIcon("Images/Java_Icon.png");
        JButton Java_Button = new JButton(Java_Icon);
        Java_Button.setSize(100, 155);
        Button_Panel_Constraints.gridx = 0;
        Button_Panel_Constraints.gridy = 0;
        Button_Panel.add(Java_Button, Button_Panel_Constraints);

        ImageIcon Python_Icon = new ImageIcon("Images/Python_Icon.png");
        JButton Python_Button = new JButton(Python_Icon);
        Python_Button.setSize(100, 155);
        Button_Panel_Constraints.gridy = 1;
        Button_Panel.add(Python_Button, Button_Panel_Constraints);

        ImageIcon CPP_Icon = new ImageIcon("Images/CPP_Icon.png");
        JButton CPP_Button = new JButton(CPP_Icon);
        CPP_Button.setSize(100, 155);
        Button_Panel_Constraints.gridy = 2;
        Button_Panel.add(CPP_Button);

        ImageIcon CS_Icon = new ImageIcon("Images/CS_Icon.png");
        JButton CS_Button = new JButton(CS_Icon);
        CS_Button.setSize(100, 155);
        Button_Panel_Constraints.gridy = 3;
        Button_Panel.add(CS_Button);

//        About panel set up
        About_Panel.setLayout(new BorderLayout());
        JButton About_Panel_PlaceHolder = new JButton();
        About_Panel_PlaceHolder.setSize(1180, 720);
        About_Panel_Constraints.gridx = 0;
        About_Panel_Constraints.gridy = 0;
        About_Panel.add(About_Panel_PlaceHolder);

//        Add the panels to the Main_Screen
        Side_Panel_Constraints.gridx = 0;
        Side_Panel_Constraints.gridy = 0;
        Side_Panel.add(Profile_Panel, Side_Panel_Constraints);
        Side_Panel_Constraints.gridy = 1;
        Side_Panel.add(Button_Panel, Side_Panel_Constraints);
        Main_Screen_Constraints.gridx = 0;
        Main_Screen_Constraints.gridy = 0;
        Main_Screen.add(Side_Panel, Main_Screen_Constraints);
        Main_Screen_Constraints.gridx = 1;
        Main_Screen.add(About_Panel, Main_Screen_Constraints);

//        Set the Main_Screen visibility to true
        Side_Panel.setVisible(true);
        About_Panel.setVisible(true);
        Main_Screen.setVisible(true);

    }
}

我知道代码可能不是正统的编程,因为我还在学习如何做。但是如果用户界面没有显示,我们将非常感谢您的帮助


共 (1) 个答案

  1. # 1 楼答案

    您的问题是所有JavaSwing应用程序都在一个框架中。面板位于框架内

    您的解决方案是:

    class Pages extends JFrame {
        public Pages() {
    //         Create the main screen for adding the other panels and buttons
            JPanel Main_Screen = new JPanel(new GridBagLayout());
            GridBagConstraints Main_Screen_Constraints = new GridBagConstraints();
    //         Set the size of the Main_Screen
            Main_Screen.setSize(1280, 720);
    
    //         Create the three panels needed to make this work
    //         Set the layouts as GridBagLayout and create the constraints needed
            JPanel Side_Panel = new JPanel();
            Side_Panel.setLayout(new GridBagLayout());
            GridBagConstraints Side_Panel_Constraints = new GridBagConstraints();
    
            JPanel Profile_Panel = new JPanel();
            Profile_Panel.setLayout(new GridBagLayout());
            GridBagConstraints Profile_Panel_Constraints = new GridBagConstraints();
    
            JPanel Button_Panel = new JPanel();
            Button_Panel.setLayout(new GridBagLayout());
            GridBagConstraints Button_Panel_Constraints = new GridBagConstraints();
    
            JPanel About_Panel = new JPanel();
            About_Panel.setLayout(new GridBagLayout());
            GridBagConstraints About_Panel_Constraints = new GridBagConstraints();
    
    //         Change the sides of the panels to fit them correctly
            Side_Panel.setPreferredSize(new Dimension(100, 720));
            Side_Panel.setMinimumSize(new Dimension(100, 720));
            Side_Panel.setMaximumSize(new Dimension(150, 1080));
            Profile_Panel.setPreferredSize(new Dimension(100, 100));
            Button_Panel.setPreferredSize(new Dimension(100, 620));
            About_Panel.setPreferredSize(new Dimension(1180, 720));
    
    //        Change the border types to null
            Side_Panel.setBorder(null);
            Profile_Panel.setBorder(null);
            Button_Panel.setBorder(null);
            About_Panel.setBorder(null);
    
    //        Create the Profile_Panel items
            ImageIcon Profile_Icon = new ImageIcon("Images/Profile_Icon.png");
            JButton Profile_Button = new JButton(Profile_Icon);
            Profile_Button.setSize(100, 100);
            Profile_Panel_Constraints.gridx = 0;
            Profile_Panel_Constraints.gridy = 0;
            Profile_Panel.add(Profile_Button, Profile_Panel_Constraints);
    
    //        Create the Buttons for the Button_Panel
            ImageIcon Java_Icon = new ImageIcon("Images/Java_Icon.png");
            JButton Java_Button = new JButton(Java_Icon);
            Java_Button.setSize(100, 155);
            Button_Panel_Constraints.gridx = 0;
            Button_Panel_Constraints.gridy = 0;
            Button_Panel.add(Java_Button, Button_Panel_Constraints);
    
            ImageIcon Python_Icon = new ImageIcon("Images/Python_Icon.png");
            JButton Python_Button = new JButton(Python_Icon);
            Python_Button.setSize(100, 155);
            Button_Panel_Constraints.gridy = 1;
            Button_Panel.add(Python_Button, Button_Panel_Constraints);
    
            ImageIcon CPP_Icon = new ImageIcon("Images/CPP_Icon.png");
            JButton CPP_Button = new JButton(CPP_Icon);
            CPP_Button.setSize(100, 155);
            Button_Panel_Constraints.gridy = 2;
            Button_Panel.add(CPP_Button);
    
            ImageIcon CS_Icon = new ImageIcon("Images/CS_Icon.png");
            JButton CS_Button = new JButton(CS_Icon);
            CS_Button.setSize(100, 155);
            Button_Panel_Constraints.gridy = 3;
            Button_Panel.add(CS_Button);
    
    //        About panel set up
            About_Panel.setLayout(new BorderLayout());
            JButton About_Panel_PlaceHolder = new JButton();
            About_Panel_PlaceHolder.setSize(1180, 720);
            About_Panel_Constraints.gridx = 0;
            About_Panel_Constraints.gridy = 0;
            About_Panel.add(About_Panel_PlaceHolder);
    
    //        Add the panels to the Main_Screen
            Side_Panel_Constraints.gridx = 0;
            Side_Panel_Constraints.gridy = 0;
            Side_Panel.add(Profile_Panel, Side_Panel_Constraints);
            Side_Panel_Constraints.gridy = 1;
            Side_Panel.add(Button_Panel, Side_Panel_Constraints);
            Main_Screen_Constraints.gridx = 0;
            Main_Screen_Constraints.gridy = 0;
            Main_Screen.add(Side_Panel, Main_Screen_Constraints);
            Main_Screen_Constraints.gridx = 1;
            Main_Screen.add(About_Panel, Main_Screen_Constraints);
    
    //        Set the Main_Screen visibility to true
            this.setLayout(new FlowLayout())//use te layout you want;
            this.add(Main_Screen);
            this.setTitle("My Java Swing App");
            this.setSize(500, 500);//you can also use .pack() to automatically set the size required
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//this one is very important
            this.setVisible(true);
        }
    
        public static openMainWindow(){
            Pages frame = new Pages();
        }
    
        public static void main(String args[]){
            openMainWindow();
        }
    }