【问题标题】:Creating a window - but buttons layout error创建一个窗口 - 但按钮布局错误
【发布时间】:2013-08-25 20:29:55
【问题描述】:

我做了一个有两个按钮的小程序。我标记 按钮一是退出程序,二是导入文件。

当有人按下程序时,我实际上让他们俩都退出程序 问题是按钮占据了所有窗口,为什么?

我尝试GridBagConstraints 调整按钮的大小,但无论如何都没有运气,这是没有导入的完整类..

public class Window2  extends JFrame{
   private static final long serialVersionUID = 1L;

   public Window2(){

       super ("ALANAZ imagtor");
       setSize(600,400);
       setDefaultCloseOperation(EXIT_ON_CLOSE);
       JPanel pnl1 = new JPanel(new GridLayout());
       JPanel pnl2 = new JPanel();

       //button
       JButton butn1 = new JButton("EXIT");
       JButton butn2 =new JButton("IMPORT");

       butn1.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            JOptionPane.showMessageDialog(null, "exiting ... bye...");
            System.exit(0);
        }
    });     
       butn2.addActionListener(new ActionListener(){

           public void actionPerformed(ActionEvent ae){
               JOptionPane.showMessageDialog(null, "can't import now exiting");
               System.exit(0);
           }
       });

       GridBagConstraints gb1 = new GridBagConstraints();
       gb1.insets = new Insets(15,15,15,15);
       //Jlabel
       JLabel lbl1 = new JLabel("exit or import an image");
       pnl1.add(butn1);
       pnl1.add(butn2);

       pnl2.add(lbl1);
       add(pnl2, BorderLayout.SOUTH);
       add(pnl1, BorderLayout.CENTER);
}}

【问题讨论】:

    标签: java swing window layout-manager gridbaglayout


    【解决方案1】:

    您在滥用布局管理器。您的 pnl1 JPanel 使用 GridLayout(没有任何行或列常量?!),如果您只向其中添加一个组件,它将占用整个 JPanel。您的代码中似乎有 GridBagConstraints,但没有 GridBagLayout,这让我很困惑。

    解决方案是阅读并了解如何使用布局管理器。请看教程链接:Laying Out Components Within a Container

    关键是要记住,您可以将 JPanel 嵌套在 JPanel 中。例如:

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    public class Window2 extends JFrame {
       private static final long serialVersionUID = 1L;
       private static final int PREF_W = 600;
       private static final int PREF_H = 400;
    
       public Window2() {
    
          super("ALANAZ imagtor");
          setDefaultCloseOperation(EXIT_ON_CLOSE);
          int gap = 3;
          JPanel buttonPanel = new JPanel(new GridLayout(1, 0, gap, 0));
          buttonPanel.setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap));
          JPanel pnl2 = new JPanel();
    
          JButton butn1 = new JButton("EXIT");
          JButton butn2 = new JButton("IMPORT");
    
          butn1.addActionListener(new ActionListener() {
    
             @Override
             public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "exiting ... bye...");
                System.exit(0);
             }
          });
          butn2.addActionListener(new ActionListener() {
    
             public void actionPerformed(ActionEvent ae) {
                JOptionPane.showMessageDialog(null, "can't import now exiting");
                System.exit(0);
             }
          });
    
          JLabel lbl1 = new JLabel("exit or import an image");
          buttonPanel.add(butn1);
          buttonPanel.add(butn2);
    
    
          JPanel centerPanel = new JPanel(new BorderLayout());
          centerPanel.add(buttonPanel, BorderLayout.SOUTH);
    
          pnl2.add(lbl1);
          add(pnl2, BorderLayout.SOUTH);
          add(centerPanel, BorderLayout.CENTER);
       }
    
       @Override
       public Dimension getPreferredSize() {
          return new Dimension(PREF_W, PREF_H);
       }
    
       public static void main(String[] args) {
          Window2 win2 = new Window2();
          win2.pack();
          win2.setLocationRelativeTo(null);
          win2.setVisible(true);
       }
    }
    

    【讨论】:

      【解决方案2】:

      如果您使用 BorderLayout 来初始化面板,并使用 EAST、NORTH、WEST、SOUTH 添加按钮,您将有一个快速修复 - 但是我也建议您阅读布局管理器

      JPanel pnl1 = new JPanel(new BorderLayout());
      pnl1.add(new JButton(), BorderLayout.SOUTH);   
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-04-29
        • 1970-01-01
        • 2016-06-12
        • 2016-11-30
        • 1970-01-01
        • 2016-11-19
        • 1970-01-01
        相关资源
        最近更新 更多