有 Java 编程相关的问题?

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

java如何更改JFrame的大小?

我正在尝试设置包含MVC的框架的大小:

chartModel = new ChartModel();
chartModel.setChartData(dataName, data);

pieChart = new PieChart();
barChart = new BarChart();

setLayout(new java.awt.GridLayout(1, 2));

pieChart.setModel(chartModel);
add(pieChart);

barChart.setModel(chartModel);
add(barChart); 

public static void main(String[] args) {
Chart chart = new Chart();
JFrame frame = new JFrame();

frame.setTitle("Chart");

frame.setSize(600,420);
//frame.setSize(400,320);

frame.add(chart, BorderLayout.CENTER);

chart.init();
chart.start();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null); // Center the frame
frame.setVisible(true);
}

然而,无论我如何更改setSize的参数,这都不会产生任何效果

我错过什么了吗?请帮忙


共 (1) 个答案

  1. # 1 楼答案

    嗯,我建议使用不同的方式来编程swing。。。更容易 您可以使用另一个类来声明windows电动机,例如:

    public class Console {
    
    public static void run (final JFrame f, final int width, final int height) {
    
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                f.setTitle(f.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setSize(width, height);
                f.setVisible(true);
                f.setLocationRelativeTo(null);
            }
        });
    
    }
    

    现在,您有了一个类控制台,当您创建这个对象时,您应该传递一个Jframe和de windows维度

    要创建Jframe,应在另一个类中创建,例如:

        public class nameClass extends JFrame{
    
        //code
    
        public nameClass(){ // constructor
           Chart chart = new Chart();
           setTitle("Chart");
           add(chart, BorderLayout.CENTER);
           chart.init();
           chart.start();
        }
    
        public static void main(String[] args) {
              Console.run(new nameClass(),300,400);
        }
    

    这个创建窗口的表单是最好的,因为它使用了另一个执行线程,并且没有重载

    我希望你能帮忙 祝你好运