【发布时间】:2019-09-25 21:02:29
【问题描述】:
需要为我的第二节编程课构建一个 GUI。其中一部分包括框架东部的 J 滑块,该滑块应与北部的文本字段同步。但是,起初文本字段不会出现在框架上的任何位置,但最终文本字段及其标签出现了。现在我试图放在框架东侧的任何东西都没有出现。我对Java非常陌生,尤其是swing库,请帮助我理解我做错了什么。
我没有收到任何错误消息,而且我知道我的 build East 函数至少正在运行,因为我使用了 print 语句来测试它。起初框架的北部不会显示任何东西,所以我将 build North 功能向上移动并开始工作。这样做对 build East 功能没有任何作用。我在下面粘贴了我的所有代码。
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");
}
}
我希望 J 滑块和 J 标签至少出现在我的框架上。没有错误消息。
【问题讨论】:
-
我没有看到任何将
east_pane添加到window的代码。或任何家长。 -
将此作为
buildEast的第二条语句添加:window.add(east_pane, BorderLayout.EAST);