1

我做了一个有两个按钮的小程序。我将按钮标记为退出程序和第二个导入文件。

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

我试图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);
}}
4

2 回答 2

3

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

解决方案是阅读并了解如何使用布局管理器。请查看教程链接:在容器中布局组件

关键是要记住你可以在 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);
   }
}
于 2013-08-25T20:33:03.783 回答
1

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

JPanel pnl1 = new JPanel(new BorderLayout());
pnl1.add(new JButton(), BorderLayout.SOUTH);   
于 2013-08-25T20:42:25.447 回答