Java JDialog-“;换行“;在组件之间

Java JDialog-“;换行“;在组件之间,java,swing,layout,jpanel,jdialog,Java,Swing,Layout,Jpanel,Jdialog,我有一个JDialog,看起来像这样: JDialog myDialog = new JDialog(); myDialog.setLocationRelativeTo(parent); // parent is a JPanel. // I want the Dialog to appear in the middle of the parent JPanel. myDialog.setModal(true); myDialog.setLayout(new FlowLayout());

我有一个JDialog,看起来像这样:

JDialog myDialog = new JDialog();

myDialog.setLocationRelativeTo(parent); 
// parent is a JPanel. 
// I want the Dialog to appear in the middle of the parent JPanel.

myDialog.setModal(true);
myDialog.setLayout(new FlowLayout());
myDialog.add(new JLabel("my text", SwingConstants.CENTER));
myDialog.add(new JButton("button 1"));
myDialog.add(new JButton("button 2"));
myDialog.pack();
myDialog.setVisible(true);
结果是一个对话框,其中JLabel和jbutton相邻显示

1) 在
JLabel
之后进行“换行”最方便的方法是什么,以便
JButton
显示在
JLabel
下方,而不使用
setSize()
?我希望自动确定尺寸,以便部件完全适合,就像
pack()
所做的那样

2) 当我设置自定义大小时,对话框会出现在我想要的地方:
parent
myDialog
的中间部分匹配。但是,如果改用
pack()
,则
myDialog
的左上角位于父对话框的中间。什么是最好的方法,使中锋匹配呢

  • 嵌套JPanel,每个都使用自己的布局管理器

  • 在调用
    pack()
    之后调用
    setLocationRelativeTo(父项)
    。渲染后需要定位窗口
  • 例如:

    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.GridLayout;
    import java.awt.BorderLayout;
    import java.awt.Dialog.ModalityType;
    import java.awt.event.ActionEvent;
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class SimpleGuiPanel extends JPanel {
        private static final String TITLE = "This is my Dialog Title";
    
        public SimpleGuiPanel() {
            JLabel titleLabel = new JLabel(TITLE, SwingConstants.CENTER);
            titleLabel.setFont(titleLabel.getFont().deriveFont(Font.BOLD, 16f));
    
            JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 5, 5));
            buttonPanel.add(new JButton("Button 1"));
            buttonPanel.add(new JButton("Button 2"));
    
            setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
            setLayout(new BorderLayout(5, 5));
            add(titleLabel, BorderLayout.PAGE_START);
            add(buttonPanel, BorderLayout.CENTER);
        }
    
        private static void createAndShowGui() {
            JPanel mainFramePanel = new JPanel();
            mainFramePanel.setPreferredSize(new Dimension(500, 400));
            final JFrame mainFrame = new JFrame("Main Frame");
            mainFrame.add(mainFramePanel);
            mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            SimpleGuiPanel simpleGuiPanel = new SimpleGuiPanel();
            final JDialog myDialog = new JDialog(mainFrame, "Dialog", ModalityType.APPLICATION_MODAL);
            myDialog.getContentPane().add(simpleGuiPanel);
            myDialog.pack();
    
            mainFramePanel.add(new JButton(new AbstractAction("Show Dialog") {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    myDialog.setLocationRelativeTo(mainFrame);
                    myDialog.setVisible(true);
                }
            }));
    
            mainFrame.pack();
            mainFrame.setLocationRelativeTo(null);
            mainFrame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(() -> createAndShowGui());
        }
    }
    
  • 嵌套JPanel,每个都使用自己的布局管理器

  • 在调用
    pack()
    之后调用
    setLocationRelativeTo(父项)
    。渲染后需要定位窗口
  • 例如:

    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.GridLayout;
    import java.awt.BorderLayout;
    import java.awt.Dialog.ModalityType;
    import java.awt.event.ActionEvent;
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class SimpleGuiPanel extends JPanel {
        private static final String TITLE = "This is my Dialog Title";
    
        public SimpleGuiPanel() {
            JLabel titleLabel = new JLabel(TITLE, SwingConstants.CENTER);
            titleLabel.setFont(titleLabel.getFont().deriveFont(Font.BOLD, 16f));
    
            JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 5, 5));
            buttonPanel.add(new JButton("Button 1"));
            buttonPanel.add(new JButton("Button 2"));
    
            setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
            setLayout(new BorderLayout(5, 5));
            add(titleLabel, BorderLayout.PAGE_START);
            add(buttonPanel, BorderLayout.CENTER);
        }
    
        private static void createAndShowGui() {
            JPanel mainFramePanel = new JPanel();
            mainFramePanel.setPreferredSize(new Dimension(500, 400));
            final JFrame mainFrame = new JFrame("Main Frame");
            mainFrame.add(mainFramePanel);
            mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            SimpleGuiPanel simpleGuiPanel = new SimpleGuiPanel();
            final JDialog myDialog = new JDialog(mainFrame, "Dialog", ModalityType.APPLICATION_MODAL);
            myDialog.getContentPane().add(simpleGuiPanel);
            myDialog.pack();
    
            mainFramePanel.add(new JButton(new AbstractAction("Show Dialog") {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    myDialog.setLocationRelativeTo(mainFrame);
                    myDialog.setVisible(true);
                }
            }));
    
            mainFrame.pack();
            mainFrame.setLocationRelativeTo(null);
            mainFrame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(() -> createAndShowGui());
        }
    }
    

    使用多个嵌套JPanel,每个都使用自己的布局管理器。调用
    pack()
    (有意义,否?)后调用
    setLocationRelativeTo(父项)
    。使用多个嵌套JPanel,每个都使用自己的布局管理器。调用
    pack()
    (有意义,否?)后调用
    setLocationRelativeTo(父项)
    。非常感谢,这正是我所需要的:)因为我对这类东西还不熟悉,我需要仔细阅读你的代码,并确保我理解所有代码,再加上一点文档浏览/谷歌搜索。非常感谢,这正是我所需要的:)因为我对这类东西还不熟悉,我需要仔细阅读您的代码,并确保我理解所有代码,同时浏览/谷歌一些文档。