有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java WindowBuilder从主窗口打开JFrame

我有一个Main.java,在这里我只会启动(设置visible true)我的JFrame窗口。 但它只打开了一扇小窗户,什么也没有。 然后我会按Highscorebutten,然后会显示另一个JFrame(highscore)

我该怎么办

(这不是完整的代码)

主要的

public class Main {
    private static MyWindow window = null;

    public static void main(String[] args) {
         window = new MyWindow();
          window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          // Frame sichtbar machen
          window.setVisible(true);
    }

弗温多

public class FWindow extends JFrame{    

private JFrame Menue;

public FWindow() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    Menue = new JFrame();
    Menue.setBounds(100, 100, 469, 741);
    Menue.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Menue.getContentPane().setLayout(null);

    JPanel pMenue = new JPanel();

    pMenue.setLayout(null);

    JLabel lblHighscore = new JLabel();
    lblHighscore.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
             Highscore highscore = new Highscore();         
                getContentPane().add(highscore);
                System.out.println();
                highscore.setVisible(true);

        }
    });
//........

高分

public class Highscore extends JFrame {
/**
 * 
 */
private static final long serialVersionUID = 1L;
private JTable table;
TableModel model;

public Highscore() {


    table = new JTable(model);
    table.setShowGrid(false);
    table.setShowHorizontalLines(false);
    table.setShowVerticalLines(false);
    table.setForeground(Color.WHITE);
    table.setBackground(new Color(113,197,208));
    table.setFont(new Font("Comic Sans MS", Font.PLAIN, 30));
    table.setRowHeight(40);
    table.setAutoCreateRowSorter(true);



    table.setModel(new DefaultTableModel(
        new Object[][] {
            //.....
        },
        new String[] {
            "1", "2", "3"
        }

    ){

    });


共 (1) 个答案

  1. # 1 楼答案

    FWindow扩展了JFrame,但它正在创建JFrameMenue)的第二个实例,这意味着您只显示了FWindow,它上面没有任何内容

    FWindow中删除extends JFrame,它不会添加任何有用的功能

    FWindow添加一个方法,它将准备并显示Menue实例

    public class FWindow {
    
        private JFrame Menue;
    
        public FWindow() {
            initialize();
        }
    
        /**
         * Initialize the contents of the frame.
         */
        private void initialize() {
            Menue = new JFrame();
            Menue.setBounds(100, 100, 469, 741);
            Menue.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Menue.getContentPane().setLayout(null);
    
            JPanel pMenue = new JPanel();
    
            pMenue.setLayout(null);
    
            JLabel lblHighscore = new JLabel();
            lblHighscore.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    Highscore highscore = new Highscore();
                    getContentPane().add(highscore);
                    System.out.println();
                    highscore.setVisible(true);
    
                }
            });
            //........
        }
    
        public void prepareAndShow() {
            //...
            Menue.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Menue.setVisible(true);
        }
    

    然后,从main方法中,确保调用它

    public class Main {
        private static MyWindow window = null;
    
        public static void main(String[] args) {
            window = new MyWindow();
            // Frame sichtbar machen
            window.prepareAndShow();
        }
    

    举个例子

    咆哮警告

    通常,我会说避免使用null布局,像素完美的布局在现代ui设计中是一种错觉。有太多的因素会影响零部件的单个尺寸,而这些因素都是您无法控制的。Swing的设计初衷是与布局经理一起工作,抛弃它们将导致问题层出不穷,你将花费越来越多的时间来纠正这些问题

    但这会导致人们对WindowBuilder的利弊大肆抨击

    基本上,你最好学会手工编写你的UI,它们会让你更好地理解布局管理器是如何工作的,以及你如何实现复杂的、解耦的UI,以及一个在不同平台上看起来像你期望的那样的UI