【问题标题】:Java GUI: button controls not VisibleJava GUI:按钮控件不可见
【发布时间】:2012-11-03 17:03:16
【问题描述】:

我在屏幕上显示我的主菜单时遇到问题。我看不出问题出在哪里。它所显示的只是一个空白的 JFrame 窗口。它没有显示带有按钮的面板。

主类:

public class Main {

public static void main(String[] args) {
    GUIView gui = new GUIView();

}
}

GUIView 类:

import javax.swing.*;
import java.awt.*;

public class GUIView {
protected JFrame frame;
    public GUIView() {


    frame = new JFrame("Test");
        frame.setVisible(true);
        frame.setSize(500,500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

MainMenu 类:

import javax.swing.*;
import java.awt.*;

public class MainMenu extends GUIView {
    private JButton b1, b2, b3;
    private JPanel panel;
    public MainMenu() {
        GridBagLayout gridbag = new GridBagLayout();
        b1 = new JButton();
        b2 = new JButton();
        b3 = new JButton();

    //Button Settings;
    b1.setText("Administrator");
    b2.setText("Program Leader");
    b3.setText("Lecturer");

    //Panel Settings
    panel = new JPanel();
    panel.setLayout(gridbag);
    panel.add(b1);
    panel.add(b2);
    panel.add(b3);
    panel.setVisible(true);
    super.frame.add(panel);
}

}

【问题讨论】:

  • 你正在构建 GuiView 但不是 MainMenu

标签: java user-interface jframe jpanel jbutton


【解决方案1】:

您永远不会创建MainMenu 的实例。要修复,您可以这样做:

public static void main(String[] args) {
   GUIView gui = new MainMenu();
}

【讨论】:

    【解决方案2】:

    试试这个方法............

    public class Test1 extends JFrame {
    
    
    
        int count;
    
         public Test1(){
    
             this.setSize(400,400);
             MyCompo m = new MyCompo();
             this.add(BorderLayout.CENTER,m);
             this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         }
    
    
    
    
    
         class MyCompo extends JPanel{
    
    
             public MyCompo() {
                 this.setSize(300,300);
                setComponents();
                 //setHandlers();
    
             }
    
            public void paintComponent(Graphics g) {
    
                 //setComponents();
    
             }
             public void setComponents() {
    
                 this.setLayout(new GridLayout(5,4));
                 this.add(new Button("1"));
                 this.add(new Button("2"));
                 this.add(new Button("3"));
                 this.add(new Button("4"));
                 this.add(new Button("5"));
                 this.add(new Button("6"));
                 this.add(new Button("7"));
                 this.add(new Button("8"));
             }
    
         }
    
         public static void main(String[] args){
    
             EventQueue.invokeLater(new Runnable() {
    
                 public void run() {
                     Test1 t = new Test1();
    
                     t.setVisible(true);
    
                 }
    
             });
    
         }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2021-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-07
      • 1970-01-01
      • 2012-06-15
      • 1970-01-01
      相关资源
      最近更新 更多