首页 > 解决方案 > 显示 java GUI 的问题。它不会显示。我还是新手,也许包类有问题或其他什么

问题描述

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

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

从 netbeans 收到一条错误消息:

线程“main”java.lang.RuntimeException 中的异常:无法编译的源代码 - 内部类 gui2.MyFrame.frm 修饰符“static”中的非法静态声明只允许在 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();
  }
      }

标签: javauser-interface

解决方案


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

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

frame此外,在将所有内容添加到其中之前,您不应使其可见。

工作截图


推荐阅读