如何在swing中创建不随帧大小变化的布局



在我的程序中,2个面板log和Img是布局,它们不是恒定的,它们不断变化我想创建一个布局,其中所有按钮和textview保持不变,无论框架的大小。有人能帮我吗?这是我的代码

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class CLayout {
    JFrame frame = new JFrame("CardLayout demo");
    JPanel panelCont = new JPanel();
    JPanel log = new LoginView();
    JPanel Img= new ImageGallery();

    CardLayout cl = new CardLayout();
    public CLayout() {
        panelCont.setLayout(cl);
        log.setLayout(new BoxLayout(log, BoxLayout.PAGE_AXIS));
        Img.setLayout(new BoxLayout(Img,BoxLayout.PAGE_AXIS));
        JButton loginButton = new JButton("login");
        JButton registerButton = new JButton("register");
        loginButton.setBounds(10, 80, 80, 25);
        log.add(loginButton);
        registerButton.setBounds(180, 80, 80, 25);
        log.add(registerButton);
        panelCont.add(log, "1");
        panelCont.add(Img, "2");
        cl.show(panelCont, "1");
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                cl.show(panelCont, "2");
            }
        });
        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                cl.show(panelCont, "1");
            }
        });
        frame.add(panelCont);
        frame.setSize(800,600);
        frame.setTitle("     Image Gallery    ");
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new CLayout();
            }
        });
    }
}

class ImageGallery extends JPanel
{
   private ImageIcon myImage1 = new ImageIcon ("Chrysanthemum.jpg");
   private ImageIcon myImage2 = new ImageIcon ("Desert.jpg");
   private ImageIcon myImage3 = new ImageIcon ("Jellyfish.jpg");
   private ImageIcon myImage4 = new ImageIcon ("principal.jpg");
   private ImageIcon myImage5 = new ImageIcon ("student.jpg");
   private ImageIcon myImage6 = new ImageIcon ("library.jpg");
   private ImageIcon myImage7 = new ImageIcon ("sports.jpg");
   private ImageIcon myImage8 = new ImageIcon ("class.jpg");
   JPanel ImageGallery = new JPanel();
   private ImageIcon[] myImages = new ImageIcon[8];
   private int curImageIndex=0;
   public ImageGallery ()
     {   
        ImageGallery.add(new JLabel (myImage1));
        myImages[0]=myImage1;
        myImages[1]=myImage2;
        myImages[2]=myImage3;
        myImages[3]=myImage4;
        myImages[4]=myImage5;
        myImages[5]=myImage6;
        myImages[6]=myImage7;
        myImages[7]=myImage8;

        add(ImageGallery, BorderLayout.CENTER);
        JButton PREVIOUS = new JButton ("Previous");
        JButton FILE = new JButton ("File");
        JButton EDIT = new JButton ("Edit");
        JButton NEXT = new JButton ("Next");
           JButton HELP = new JButton ("Help");
        JPanel Menu = new JPanel();
        Menu.setLayout(new GridLayout(1,5));
        Menu.add(FILE);
        Menu.add(EDIT);
        Menu.add(PREVIOUS);

        Menu.add(NEXT);
        Menu.add(HELP);
        add(Menu, BorderLayout.NORTH);
        //register listener
        PreviousButtonListener PreviousButton = new PreviousButtonListener ();
        NextButtonListener NextButton = new NextButtonListener ();
        //add listeners to corresponding componenets 
        PREVIOUS.addActionListener(PreviousButton);
        NEXT.addActionListener(NextButton);
    }

 class PreviousButtonListener implements ActionListener 
 {
    public void actionPerformed(ActionEvent e)
        {
            if(curImageIndex>0 && curImageIndex <=5)
                {   ImageGallery.remove(0);
                    curImageIndex=curImageIndex-1;
                    ImageIcon TheImage= myImages[curImageIndex];
                    ImageGallery.add(new JLabel (TheImage));
                    ImageGallery.validate();
                    ImageGallery.repaint(); 
                }
            else 
                {   
                    ImageGallery.remove(0);
                    ImageGallery.add(new JLabel (myImage6));
                    curImageIndex=5;
                    ImageGallery.validate();
                    ImageGallery.repaint();
                }
        }
}

 class NextButtonListener implements ActionListener 
 {

    public void actionPerformed(ActionEvent e)
    {
        if(curImageIndex>=0 && curImageIndex <5)
            {   ImageGallery.remove(0);
                curImageIndex = curImageIndex + 1;
                ImageIcon TheImage= myImages[curImageIndex];
                ImageGallery.add(new JLabel (TheImage));
                ImageGallery.validate();
                ImageGallery.repaint(); 
            }
        else 
            {   
                ImageGallery.remove(0);
                ImageGallery.add(new JLabel (myImage1));
                curImageIndex=0 ;
                ImageGallery.validate();
                ImageGallery.repaint();
            }
        }
    }
}

 class LoginView extends JPanel{
    JLabel userLabel = new JLabel("User");    
    JTextField userText = new JTextField(20);
     JLabel passwordLabel = new JLabel("Password");
     JPasswordField passwordText = new JPasswordField(20);
      public LoginView(){
        userLabel.setBounds(10, 10, 80, 25);
        add(userLabel);

        userText.setBounds(100, 10, 160, 25);
        add(userText);
        passwordLabel.setBounds(10, 40, 80, 25);
        add(passwordLabel);
        passwordText.setBounds(100, 40, 160, 25);
        add(passwordText);
    }
}

您可以将布局设置为null

这也意味着你必须自己设置每个元素的位置和大小。

相关内容

  • 没有找到相关文章

最新更新