本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

EclipseWindow Builder - JFrame 无法缩放

发布于2023-09-21 20:50     阅读(685)     评论(0)     点赞(3)     收藏(3)


以下代码是使用 eclispe gui 生成器生成的:

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.UnsupportedLookAndFeelException;

import org.apache.log4j.Logger;

public class MainWindow {

    /**
     * Logger
     */
    private static final Logger log = Logger.getLogger(MainWindow.class);

    /**
     * main JFrame
     */
    private JFrame frame;

    /**
     * JTextPane
     */
    private JTextPane textPane; 
    private JTextField textField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    setLookAndFeel();

                    MainWindow window = new MainWindow();

                    //                  window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public MainWindow() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setSize(450,368);
//      frame.setBounds(100, 100, 580, 261);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JMenuBar menuBar = new JMenuBar();
        frame.setJMenuBar(menuBar);

        JMenu mnMain = new JMenu("Main");
        menuBar.add(mnMain);

        JMenuItem about = new JMenuItem("About");
        mnMain.add(about);

        about.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                new HelpWindow();

            }
        });

        JMenuItem mntmExit = new JMenuItem("Exit");
        mnMain.add(mntmExit);

        mntmExit.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(-1);

            }
        });

        textPane = new JTextPane();

        JButton btnRun = new JButton("Run");

        btnRun.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                String text = textPane.getText();
                log.info(text);

                //TODO the text gets parsed and read in HERE!!!

            }
        });


        JLabel lblNewLabel = new JLabel("Enter your excel formula:");

        JButton btnConvertToPdf = new JButton("Convert to PDF");
        btnConvertToPdf.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                //TODO Fill in Action Listener
            }
        });

        JButton btnClear = new JButton("Clear");
        btnClear.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                log.info("Clear Text!");
                textPane.setText("");

            }
        });

        JSeparator separator = new JSeparator();

        textField = new JTextField();
        textField.setColumns(10);

        JLabel lblRowOfTable = new JLabel("Row of table");

        JButton btnParse = new JButton("Parse");
        btnParse.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            }
        });

        GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
        groupLayout.setHorizontalGroup(
            groupLayout.createParallelGroup(Alignment.LEADING)
                .addGroup(groupLayout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
                        .addGroup(groupLayout.createSequentialGroup()
                            .addComponent(lblNewLabel)
                            .addContainerGap(302, Short.MAX_VALUE))
                        .addGroup(groupLayout.createSequentialGroup()
                            .addComponent(separator, GroupLayout.PREFERRED_SIZE, 340, GroupLayout.PREFERRED_SIZE)
                            .addContainerGap(84, Short.MAX_VALUE))
                        .addGroup(groupLayout.createSequentialGroup()
                            .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                .addGroup(groupLayout.createSequentialGroup()
                                    .addComponent(lblRowOfTable)
                                    .addPreferredGap(ComponentPlacement.RELATED)
                                    .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(ComponentPlacement.RELATED, 127, Short.MAX_VALUE)
                                    .addComponent(btnParse, GroupLayout.PREFERRED_SIZE, 71, GroupLayout.PREFERRED_SIZE))
                                .addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
                                    .addComponent(btnConvertToPdf, GroupLayout.PREFERRED_SIZE, 119, GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(ComponentPlacement.RELATED, 112, Short.MAX_VALUE)
                                    .addComponent(btnClear)
                                    .addPreferredGap(ComponentPlacement.UNRELATED)
                                    .addComponent(btnRun))
                                .addComponent(textPane, GroupLayout.DEFAULT_SIZE, 349, Short.MAX_VALUE))
                            .addGap(75))))
        );
        groupLayout.setVerticalGroup(
            groupLayout.createParallelGroup(Alignment.LEADING)
                .addGroup(groupLayout.createSequentialGroup()
                    .addGap(5)
                    .addComponent(lblNewLabel)
                    .addPreferredGap(ComponentPlacement.RELATED)
                    .addComponent(textPane, GroupLayout.PREFERRED_SIZE, 85, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(ComponentPlacement.UNRELATED)
                    .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                        .addComponent(btnRun)
                        .addComponent(btnClear)
                        .addComponent(btnConvertToPdf))
                    .addPreferredGap(ComponentPlacement.UNRELATED)
                    .addComponent(separator, GroupLayout.PREFERRED_SIZE, 9, GroupLayout.PREFERRED_SIZE)
                    .addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
                        .addGroup(groupLayout.createSequentialGroup()
                            .addPreferredGap(ComponentPlacement.RELATED)
                            .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                                .addComponent(lblRowOfTable)
                                .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                            .addContainerGap(22, Short.MAX_VALUE))
                        .addGroup(groupLayout.createSequentialGroup()
                            .addPreferredGap(ComponentPlacement.RELATED)
                            .addComponent(btnParse))))
        );

        frame.getContentPane().setLayout(groupLayout);
        frame.pack();

    }

    /**
     * Sets the look and feel of the application
     */
    private static void setLookAndFeel(){
        try {
            javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(MainWindow.class).error(ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(MainWindow.class).error(ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(MainWindow.class).error(ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(MainWindow.class).error(ex);
        }
    }
}

然而,当回到 GUI 构建器来扩展我的规模时,JFrame这是无法完成的。尽管我使用 来设置大小,但缩放会恢复到正常事件frame.setSize(...)

有什么建议吗?为什么会出现这种情况?

我很欣赏你的回答!


解决方案


  • 你错过了设置的代码JFrame visible,例如frame.setVisible(true);

  • 作为构造函数中的最后一个代码行,afterframe.pack();被调用

编辑:

那么它的回报

在此输入图像描述



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:http://www.javaheidong.com/blog/article/677403/10c7684209eff75d5162/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

3 0
收藏该文
已收藏

评论内容:(最多支持255个字符)