gpt4 book ai didi

java - Intellij使用GUI表单问题

转载 作者:行者123 更新时间:2023-12-02 12:09:27 25 4
gpt4 key购买 nike

我正在尝试使用 Intellij 的 GUI 表单设计器来为类(class)项目设计 GUI。我从未真正使用过 swing,所以事实证明它比我最初想象的要困难。

基本上,我已经创建了 GUI 表单并将其绑定(bind)到我的类,然后我根据在线指令生成了主要方法。当这不起作用时,我尝试使用 .add 来添加它。

这就是我现在所在的位置。 https://pastebin.com/u0gcs15u

    public static void main(String[] args) {
JFrame frame = new JFrame("TicketGenie");
frame.setContentPane(new TicketGenie().panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}

这就是表单的样子。 /image/slYh3.jpg

我还应该提到,我在 main 方法中遇到了空指针异常。另外,我目前正在学习的类(class)是编程基础知识,这超出了我们正在学习的内容,但我一直在尝试自学如何使用 java 制作一个还算不错的 GUI。

谢谢。

最佳答案

Intellij using GUI form issue

这与 Intellij 使用 GUI 表单问题无关,您收到空指针异常,因为在构造函数 TicketGenie() 上的 TicketGenie java 类中您尝试添加空容器和组件。

您刚刚声明了 JPanel 容器和其他组件,但是忘记初始化它们

这就是您收到空指针异常的原因。

public class TicketGenie {

private JPanel panel;
private JTextField firstname;
private JTextField lastname;
private JTextField address;
private JTextField city;
private JTextField state;
private JTextField zipcode;
private JTextField phonenumber;
private JTextField email;
private JTextField numberoftickets;
private JButton submitButton;
private JLabel firstnamelbl;
private JLabel lastnamelbl;
private JLabel addresslbl;
private JLabel citylbl;
private JLabel statelbl;
private JLabel zipcodelbl;
private JLabel phonenumberlbl;
private JLabel emaillbl;
private JLabel ticketlbl;

public TicketGenie() {
panel.add(firstname);
panel.add(lastname);
panel.add(address);
panel.add(city);
panel.add(state);
panel.add(zipcode);
panel.add(phonenumber);
panel.add(email);
panel.add(numberoftickets);

panel.add(firstnamelbl);
panel.add(lastnamelbl);
panel.add(addresslbl);
panel.add(citylbl);
panel.add(statelbl);
panel.add(zipcodelbl);
panel.add(phonenumberlbl);
panel.add(emaillbl);
panel.add(ticketlbl);

submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {

}
});
}

}

Below solution java class

    public class TicketGenie {

//Initialized and declared static JPanel Container
static JPanel panel=new JPanel();

//Components declared
private JTextField firstname;
private JTextField lastname;
private JTextField address;
private JTextField city;
private JTextField state;
private JTextField zipcode;
private JTextField phonenumber;
private JTextField email;
private JTextField numberoftickets;
private JButton submitButton;
private JLabel firstnamelbl;
private JLabel lastnamelbl;
private JLabel addresslbl;
private JLabel citylbl;
private JLabel statelbl;
private JLabel zipcodelbl;
private JLabel phonenumberlbl;
private JLabel emaillbl;
private JLabel ticketlbl;

public TicketGenie() {

//Components initialized
firstname=new JTextField();
lastname=new JTextField();
address=new JTextField();
city=new JTextField();
state=new JTextField();
zipcode=new JTextField();
phonenumber=new JTextField();
email=new JTextField();
numberoftickets=new JTextField();
submitButton=new JButton();
firstnamelbl=new JLabel();
lastnamelbl=new JLabel();
addresslbl=new JLabel();
citylbl=new JLabel();
statelbl=new JLabel();
zipcodelbl=new JLabel();
phonenumberlbl=new JLabel();
emaillbl=new JLabel();
ticketlbl=new JLabel();

//Add Components to JPanel Container
panel.add(firstname);
panel.add(lastname);
panel.add(address);
panel.add(city);
panel.add(state);
panel.add(zipcode);
panel.add(phonenumber);
panel.add(email);
panel.add(numberoftickets);

panel.add(firstnamelbl);
panel.add(lastnamelbl);
panel.add(addresslbl);
panel.add(citylbl);
panel.add(statelbl);
panel.add(zipcodelbl);
panel.add(phonenumberlbl);
panel.add(emaillbl);
panel.add(ticketlbl);

submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {

}
});
}
}

Foo Class JFrame框架设置TicketGenie Class静态JPanel面板的ContentPane

public class Foo {

public static void main(String[] args) {
JFrame frame = new JFrame("TicketGenie");
frame.setContentPane(new TicketGenie().panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}

注意:您需要设置组件的高度、重量等,并设置 UI 外观和感觉的布局属性

关于java - Intellij使用GUI表单问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46654761/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com