- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试完成一个学校项目,其中涉及将 10 个 jlabel、10 个 jtext 和 10 个 jbutton 放入一个 jpanel 中,并将它们的输出放入另一个 jpanel 中。问题是我不知道如何使用 jpanels,而且我无法弄清楚我遇到的这个问题。我正在尝试将 jtexts 和 jlabels 添加到 jpanel,但我不断收到此错误:
Exception in thread "main" java.lang.NullPointerException
at CP235_A8_Application_Fitzgerald.create_interface(CP235_A8_Application_Fitzgerald.java:89)
at CP235_A8_Application_Fitzgerald.<init>(CP235_A8_Application_Fitzgerald.java:253)
at CP235_A8_Application_Fitzgerald.main(CP235_A8_Application_Fitzgerald.java:263)
我在某处发现这是因为我添加的内容有一个空值,这就是导致它的原因,所以我在将它们添加到 jpanel 之前给出了每一个 jtext 和 jlabel,但这不起作用。我读了一些关于不实例化 jpanel 的其他内容(但我不太明白,所以我不确定这是否适用)无论如何,我很困惑,希望得到一些帮助。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CP235_A8_Application_Fitzgerald extends JFrame
{
private JTextField text1;
private JLabel label1;
private JTextField text2;
private JLabel label2;
private JTextField text3;
private JLabel label3;
private JTextField text4;
private JLabel label4;
private JTextField text5;
private JLabel label5;
private JTextField text6;
private JLabel label6;
private JTextField text7;
private JLabel label7;
private JTextField text8;
private JLabel label8;
private JTextField text9;
private JLabel label9;
private JTextField text10;
private JLabel label10;
private JButton exit;
private JButton sort;
private JButton clear;
private JButton display;
private JPanel inputpane = new JPanel();
private void create_interface()
{
System.out.println("create_interface is running.");
int a;
int b;
int c;
int d;
int e;
int f;
int g;
int h;
int i;
int j;
a = 15;
b = 10;
c = 18;
d = 7;
e = 25;
f = 2;
g = 14;
h = 27;
i = 33;
j = 11;
Container GUI = getContentPane();
GUI.setBackground(Color.red);
System.out.println("still running");
GUI.add(inputpane);
inputpane.setLayout(new BorderLayout());
inputpane.setBackground(Color.green);
System.out.println("still running");
//inputpane.add(sort, BorderLayout.SOUTH);
//inputpane.add(clear, BorderLayout.SOUTH);
//inputpane.add(exit, BorderLayout.SOUTH);
//inputpane.add(display, BorderLayout.SOUTH);
text1.setText(String.valueOf(a));
inputpane.add(text1, BorderLayout.EAST);
text2.setText(String.valueOf(b));
inputpane.add(text2, BorderLayout.EAST);
text3.setText(String.valueOf(c));
inputpane.add(text3, BorderLayout.EAST);
text4.setText(String.valueOf(d));
inputpane.add(text4, BorderLayout.EAST);
text5.setText(String.valueOf(e));
inputpane.add(text5, BorderLayout.EAST);
text6.setText(String.valueOf(f));
inputpane.add(text6, BorderLayout.EAST);
text7.setText(String.valueOf(g));
inputpane.add(text7, BorderLayout.EAST);
text8.setText(String.valueOf(h));
inputpane.add(text8, BorderLayout.EAST);
text9.setText(String.valueOf(i));
inputpane.add(text9, BorderLayout.EAST);
text10.setText(String.valueOf(j));
inputpane.add(text10, BorderLayout.EAST);
label1.setText("Integer 1");
inputpane.add(label1, BorderLayout.WEST);
label2.setText("Integer 2");
inputpane.add(label2, BorderLayout.WEST);
label3.setText("Integer 3");
inputpane.add(label3, BorderLayout.WEST);
label4.setText("Integer 4");
inputpane.add(label4, BorderLayout.WEST);
label5.setText("Integer 5");
inputpane.add(label5, BorderLayout.WEST);
label6.setText("Integer 6");
inputpane.add(label6, BorderLayout.WEST);
label7.setText("Integer 7");
inputpane.add(label7, BorderLayout.WEST);
label8.setText("Integer 8");
inputpane.add(label8, BorderLayout.WEST);
label9.setText("Integer 9");
inputpane.add(label9, BorderLayout.WEST);
label10.setText("Integer 10");
inputpane.add(label10, BorderLayout.WEST);
System.out.println("still running");
System.out.println("still running");
System.out.println("still running");
//exit.setBounds(650, 350, 300, 100);
//inputpane.add(exit);
exit.setText("EXIT NOW!");
exit.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent event)
{
ExitButtonEventHandler(event);
}
}
);
/*
SP1.setBounds(220, 350, 300, 100);
GUI.add(SP1);
SP1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
area51.append("Type stuff here, pal!");
*/
setTitle("Display variables.");
setLocation(100, 100);
setSize(1200, 800);
setVisible(true);
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
System.out.println(e);
System.out.println(f);
System.out.println(g);
System.out.println(h);
System.out.println(i);
System.out.println(j);
System.out.println("create_interface is ending.");
}
private void ExitButtonEventHandler(ActionEvent event)
{
System.exit(0);
}
public CP235_A8_Application_Fitzgerald()
{
System.out.println("Constructor is running.");
create_interface();
System.out.println("Constructor is running.");
}
public static void main(String[] args)
{
System.out.println("Hello from Assignment 2.");
CP235_A8_Application_Fitzgerald Peter = new CP235_A8_Application_Fitzgerald();
System.out.println("Goodbye from Assignment 2.");
}
/*
private void DisplayListJButtonActionPerformed( ActionEvent event)
{
int count = 0
RefillAL();
SortedListJTextArea.setText("");
SortedListJTextArea.append("Sorted Integer List\n");
for (IntegersArrayList )
if ( count == 0)
{
SortedListJTextArea.append("No Integers to sort\n");
}
}
RefillAL()
{
Integer i;
String str;
IntegersArrayList.clear();
str = text1.getText();
if ( ! str.equals(""))
{
try
{
i = Integer.parseInt(str);
IntegersArrayList.add(i);
}
catch (NumberFormatException exception)
{
}
}
*/
}
附注
正如你所看到的,很多代码都被注释掉了并且不完整,我现在不太关心这一点,现在我专注于 JPanel 不工作(当然,除非它是部分原因) jpanel 不工作)。预先感谢所有帮助我的人。
最佳答案
您声明了一堆组件,但在使用它们之前没有初始化它们。您必须初始化变量,在尝试使用变量之前创建 WhatNot whatnot = new WhatNot()
。否则您将使用空变量。
或者更具体地说,针对您的情况,您必须在使用它们之前为 text1 以及所有其他文本和标签组件分配一个有效的 JTextField 对象。
例如,更改:
private JTextField text1;
到
// 10 just represents whatever column size you prefer
private JTextField text1 = new JTextField(10);
// ...
// the same with all the other components.
这与 Swing 无关,而与基本 Java 有关,在尝试创建 GUI 之前,您需要先阅读 Java 文本简介,因为所有这些都在前几章中得到了很好的解释。
<小时/>根据我的评论,您需要学习如何调试 NPE (NullPointerException) 的一般概念。您应该仔细检查抛出它的行,找出哪个变量为空,然后追溯到您的代码以查看原因。你会一次又一次地遇到这些,相信我。
关于java - 无法向我的 JPanel 添加任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23306218/
我正在尝试使用 NetBeans 在 Java 中制作类似幻灯片的应用程序。 我有一个 JFrame(主窗口),里面有两个用于导航的按钮(后退和下一步),还有一个 JPanel(mainPanel),
新代码 package test; import javax.swing.*; import java.awt.*; public class TestWindow extends JFrame{ /
我是 Java swing 编码新手。我正在尝试将 JPanel 内容复制到一个新的 JPanel,它使用原始 JPanel 的内容来显示。此外,原始 JPanel 内容随着记录的变化而变化。我尝试了
我正在尝试创建一个带有另外两个 JPanel 的 JPanel,但是,当时,当我要显示主要内容时JPanel,它只显示主Jpanel上的第一个Jpanel。 通过以下示例,您将更好地理解这个问题: 我
我正在尝试学习如何使用编码风格在 Java 中完成 GUI 的工作这就是我写的: import java.awt.Container; import java.awt.Panel; import ja
我从 Oracle 教程中得到了一个 JPanel 的例子我看到它使用默认方法关闭窗口 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 我想
我在使用 Java JPanel 时遇到问题。我想将 2 个具有不同布局的 JPanel 放入一个也有布局的 JPanel 中。有可能让它发挥作用吗? BibP() setLayout(new
我试图让一个 JPanel 出现在另一个 JPanel 中。目前,JPanel 位于外部 JFrame 中,并与我的其他 JFrame 一起加载。我希望 JPanel 位于另一个 JPanel 内部,
是否可以在 Java 的一个方法中在一个面板中包含一个面板? 就像,如果我在名为 CreatePanel 的方法中创建了一个 JPanel,我可以在其下面添加另一个吗?我正在尝试在一种方法中添加两个或
我对嵌套的 BoxLayouts 有疑问。 我想构建一个由 2 个子面板组成的 DropDownPanel:顶部的标题和底部的主体。 body 最初是隐藏的。通过单击标题,您可以切换正文的可见性并显示
我需要创建一个在 JFrame 或 JPanel 上显示多个矩形的程序。这是我到目前为止提出的代码: import javax.swing.*; import java.util.Random; im
将站点中的一些图像保存到 ArrayList 后,我试图创建一个 jpanel,它将在带有滚动 Pane 的单独 jpanel 中显示所有这些图像,以便我可以向每个图像添加 Action 事件。然
我正在开发一个简单的注册窗口,它会在 Java 应用程序打开时出现。 这是一个 JFrame,里面有一个 JPanel,它有文本字段、标签,另一个面板也包含文本字段和标签。我的问题是外部面板有背景图像
我有这个界面要创建。我有 JScrollPane 的问题: 我声明了一个带有 Gridlayout(8,1,0,2) 的 JPanel,我希望在这个面板中出现 8 行。 一行是一个JPanel,我设置
我已经创建了 JFreeChart 并将其放入图表面板(按照建议)。我还将它添加到 jPanel 中。我正在使用 jFrame。但是运行程序后我的图表不可见。有谁能帮帮我吗? final JFreeC
我在下面附上了我的代码,我的程序的屏幕截图,以及我希望标签看起来像的图形。我需要我的 JPanel textPanel 出现在 LLP 选项卡上的 JButton 下方。我试图将 textPanel
我想从窗口 (JFrame) 中删除旧的 JPanel 并添加一个新的。我该怎么做呢? 我尝试了以下方法: public static void showGUI() { JFrame fram
我刚刚接触 Java,正在为我的大学类(class)开发一个项目。我正在开发一款《百万富翁》游戏,但我陷入困境。 我有一个 JFrame 类,其中有 2 个面板。第一个是由按钮组成的,第二个是我想通过
所以,我正在制作一个绘画程序,我有一个主要的Paint类,它检测鼠标输入和绘画,还有一个Tools类,它是左侧的工具栏,拥有许多工具,例如画笔大小更改和形状更改。因此,我想向 Tools 类添加一个清
我正在尝试制作一个在 JToggleButton 的帮助下激活的弹出面板。我希望在选择 ToggleButton 时将 JPanel 添加到另一个 Jpanel 上,并在取消选择 ToggleButt
我是一名优秀的程序员,十分优秀!