gpt4 book ai didi

java - 显示 java GUI 时出现问题。它不会显示。我还是个新人,也许包类有问题或者其他什么问题

转载 作者:太空宇宙 更新时间:2023-11-04 09:03:08 24 4
gpt4 key购买 nike

所以我正在制作这个用于巴士预订的 GUI,但它似乎没有运行。我对 Java 编程还是个新手。

不知道是类包的问题还是其他的问题。

从 netbeans 收到错误消息:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Illegal static declaration in inner class gui2.MyFrame.frm modifier 'static' is only allowed in constant variable declarations at gui2.MyFrame$frm.main(Student.java:100)

package gui2;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Student implements ActionListener{
private JFrame frame;
private JButton no;
private JButton sub;
private JLabel l1;
private JLabel l2;
private JLabel l3;
private TextField name;
private TextField cate;
private TextField desti;
private JTextArea tout;//ouput interface
public Student(){
frame=new JFrame("TRAIN TICKET");
frame.setSize(800,600);
frame.setLayout(null);
frame.setVisible(true);
no=new JButton("WELCOME TO TRAIN TICKET PADANG BESAR");
no.setBounds(0,0,800,40); //(,,lebar,tinggi)
frame.add(no,BorderLayout.NORTH);
sub = new JButton("Submit");
frame.add(sub);
sub.setBounds(350,500,100,20);
sub.addActionListener(this);
l1 = new JLabel("NAME :");
frame.add(l1);
l1.setBounds(50,45,100,60);
l2 = new JLabel("CATEGORY :");
l2.setBounds(50,60,150,90);;
frame.add(l2);
l3 = new JLabel("DESTINATION :");
frame.add(l3);
l3.setBounds(50,70,150,120);
name = new TextField ();
frame.add(name);
name.setBounds(200,60,200,20);
cate= new TextField ();
frame.add(cate);
cate.setBounds(200,90,200,20);
desti= new TextField ();
frame.add(desti);
desti.setBounds(200,125,200,20);
tout = new JTextArea();
frame.add(tout);
tout.setBounds(450,50,300,400);

}

public void actionPerformed(ActionEvent e)
{


if (e.getSource() == sub)
{
double price = 0.0;
double charge = 0.0;
String data1,data2,data3;
data1 = l1.getText();
data2 = l2.getText();
data3 = l3.getText();

if (data1.equals("Alor Setar")) {
price = 5.0;
}
else if (data2.equals("Sungai Petani")) {
price = 5.0;
}
else if (data2.equals("Anak Bukit")) {
price = 6.0;
}
else if (data2.equals("Tasek Gelugor")) {
price = 11.0;
}
else if (data2.equals("Gurun")) {
price = 9.0;
}
if (data3.equals("Children")) {
charge = 0.20;
}
else if (data3.equals("Adult")) {
charge = 0.0;
}
else if (data3.equals("Elder")) {
charge = 0.15;
}
double total = ( price * 0.06 ) + price + (price * charge);
l1.setText(data1);
l2.setText(data2);
l3.setText(String.valueOf(total));
}

}
public static void main(String args[]) {
Student app=new Student();
}
}

最佳答案

不要在线程上运行您的 Swing 应用程序代码,该线程可能会阻塞并锁定您的应用程序。

public static void main(String args[]) {
SwingUtilities.invokeLater(() -> new Student());
}

此外,在向框架添加所有内容之前,不应使其可见。

Working screenshot

关于java - 显示 java GUI 时出现问题。它不会显示。我还是个新人,也许包类有问题或者其他什么问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60538753/

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