简体   繁体   English

为什么我的JSlider或JLabel不会出现在框架的任何位置?

[英]Why won't my JSlider or JLabel appear anywhere on my frame?

Need to build a GUI for my second programming class. 需要为第二个编程类构建一个GUI。 Part of this includes a J Slider in the east of the frame that should sync up with a text field in the north. 其中一部分包括框架东部的J滑块,该滑块应与北部的文本字段同步。 However, at first the text field wouldn't show up anywhere on the frame, but eventually the text field and its labeled appeared. 但是,起初文本字段不会显示在框架的任何位置,但最终会出现文本字段及其标签。 Now nothing I'm trying to put in the East side of the frame is appearing at all. 现在,我试图放入框架东侧的任何东西都没有出现。 I'm very new to Java and especially the swing libraries, please help me understand what I'm doing wrong. 我是Java的新手,尤其是swing库,请帮助我了解我做错了什么。

I'm not getting any error messages, and I know my build East function is at least running since I've used print statements to test that. 我没有收到任何错误消息,并且我知道我的构建East函数至少正在运行,因为我已经使用print语句进行了测试。 At first the north part of the frame wouldn't show anything so I moved the build North function farther up and it started working. 刚开始时,框架的最北端什么都没有显示,因此我将构建北端功能进一步移到了上面并开始工作。 Doing this does nothing for the build East function. 这样做对构建East功能没有任何作用。 I've pasted all of my code below. 我在下面粘贴了所有代码。

    import javax.swing.*;
    import java.awt.BorderLayout;
    import java.awt.*;
    import java.awt.event.*;

public class GUI1 implements ActionListener
{
    //Create window for GUI
    JFrame window;

    public static void main(String [] args)
    {
        GUI1 g = new GUI1();
    }

    public GUI1()
    {
        //Set name and dimensions of window
        window = new JFrame("Program 2");
        window.setBounds(187, 218, 700, 450);

        buildEast();
        buildNorth();

        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setVisible(true); //default is invisible window
    }

    public void buildNorth()
    {
        //Creates panel, and adds panel to north of window
        JPanel north_pane = new JPanel();
        window.add(north_pane, BorderLayout.NORTH);

        //Create and add label to JPanel
        JLabel label = new JLabel ("Year");
        north_pane.add(label);

        //Create and add text field to JPanel in north of window
        JTextField tField = new JTextField(5);
        north_pane.add(tField);
    }

    public void buildEast()
    {
        //Create and add panel to east of window
        JPanel east_pane = new JPanel();

        //Add a title for the slider
        JLabel slideLabel = new JLabel ("Change Year");
        east_pane.add(slideLabel);


        final int YEAR_MIN = 1990;
        final int YEAR_MAX = 2019;
        int selectYear = 2000;
        JSlider yearSlide = new JSlider(JSlider.VERTICAL, YEAR_MIN,    YEAR_MAX, selectYear);

        //Setup for slide labels
        yearSlide.setMajorTickSpacing(4);
        yearSlide.setMinorTickSpacing(2);
        yearSlide.setPaintTicks(true);
        yearSlide.setPaintLabels(true);

        east_pane.add(yearSlide);
    }


    public void actionPerformed(ActionEvent e)
    {
        System.out.println("Click");
    }
}

I expected the J Slider and J Label to at least appear on my frame. 我希望J Slider和J Label至少出现在我的框架上。 No error messages. 没有错误讯息。

In Swing you have to attach any elements you wish to display to a JFrame (often via intermediary JPanels, etc.), which you must then set as visible. 在Swing中,您必须将希望显示的任何元素附加到JFrame(通常通过中间的JPanels等),然后必须将其设置为可见。 As pointed out in comments, you never add the east_pane to the JFrame in your code. 如注释中所指出的,您永远不会在代码中将east_pane添加到JFrame中。

There are two ways you might go about this: 您可以通过两种方法进行此操作:

The quick and easy option would be to add the line 快速简便的选择是添加行

window.add(east_pane, BorderLayout.EAST); 

on the line after the declaration of east_pane. 在east_pane声明之后的行上。

A better option, however, would be to make your buildEast() and buildNorth() methods return JPanel objects and then integrate them into the JFrame inside the GUI1 method like so (note that we can now also scope the window object more narrowly): 但是,更好的选择是使您的buildEast()和buildNorth()方法返回JPanel对象,然后将它们集成到GUI1方法内的JFrame中(请注意,我们现在也可以更狭窄地限制window对象):

public GUI1()
{
    //Set name and dimensions of window
    JFrame window = new JFrame("Program 2");
    window.setBounds(187, 218, 700, 450);

    window.add(buildEast(), BorderLayout.EAST);
    window.add(buildNorth(), BorderLayout.NORTH);

    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setVisible(true); //default is invisible window
}
public JPanel buildEast()
{
    //Create and add panel to east of window
    JPanel east_pane = new JPanel();
    //Add a title for the slider
    JLabel slideLabel = new JLabel ("Change Year");
    east_pane.add(slideLabel);


    final int YEAR_MIN = 1990;
    final int YEAR_MAX = 2019;
    int selectYear = 2000;
    JSlider yearSlide = new JSlider(JSlider.VERTICAL, YEAR_MIN, YEAR_MAX, selectYear);

    //Setup for slide labels
    yearSlide.setMajorTickSpacing(4);
    yearSlide.setMinorTickSpacing(2);
    yearSlide.setPaintTicks(true);
    yearSlide.setPaintLabels(true);

    east_pane.add(yearSlide);
    return east_pane;
}

Going this route will make your UI much easier to deal with as it grows in size. 遵循此路线将使您的UI随着大小的增加而变得更加易于处理。

The reason Swing works this way (and by extension, the reason you didn't get any errors but were able to use those print statements) is that Swing allows you to dynamically swap out UI elements on the fly. Swing以这种方式工作的原因(以及扩展,您没有遇到任何错误但能够使用这些打印语句的原因)是,Swing允许您动态地动态换出UI元素。 While this may be beyond the scope of your assignment, consider a tabbed application. 尽管这可能超出您的分配范围,但请考虑使用选项卡式应用程序。 It may not be (and likely isn't) desirable to destroy a tab each time it becomes hidden, just to have to re-create it moments later (as this takes time, it may contain data or processing logic, etc.). 每次(隐藏)一个选项卡时都可能不希望(也可能不希望)破坏该选项卡,而只是稍后需要重新创建它(因为这会花费时间,它可能包含数据或处理逻辑等)。 And, obviously, you wouldn't want it to be visible all the time. 而且,显然,您不希望它始终可见。 The answer then is to simply not display that tab until you want to do so. 答案是根本不显示该选项卡,除非您要这样做。

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

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