问题
In my program the 2 panels log and Img are layouts which are not constant they keep changing i want to create a layout where all buttons and textview reamins constant irrespective of the size of frame. can anyone help me? here is my code
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);
}
}
回答1:
You can set the layout to null
for this.
This also means that you have to set the position and size for every element yourself.
来源:https://stackoverflow.com/questions/24662716/how-to-create-layout-in-swing-which-does-not-change-with-frame-size