我正在嘗試使用流佈局來製作一個菜單屏幕定位與單列,但每當我添加一個按鈕,它將其添加到單個行。桂按鈕佈局的問題
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Proj
{
JPanel card1,card2;
ActionListener listener;
JFrame menu;
JFrame catagories;
JButton menu1,addOrTake,cata,payd,showd;
//JButton
public Proj(){
card2=new JPanel();
menu = new JFrame("Card Layout");
catagories = new JFrame();
//final Container contentPane = menu.getContentPane();
final CardLayout layout = new CardLayout();
menu.setLayout(layout);
card1=new JPanel();
menu1 = new JButton("");
menu1.setIcon(new ImageIcon("C:/Users/sabar/Menu.jpg"));
menu1.setSize(60,600);
menu1.setVisible(true);
addOrTake = new JButton();
addOrTake.setIcon(new ImageIcon("C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg"));
cata = new JButton("");
cata.setIcon(new ImageIcon("C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg"));
showd = new JButton("");
showd.setIcon(new ImageIcon("C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg"));
payd = new JButton("");
payd.setSize(60, 600);
payd.setIcon(new ImageIcon("C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg"));
payd.setVisible(true);
card1.add(menu1);
card1.add(addOrTake);
card1.add(cata);
card1.add(showd);
card1.add(payd);
menu1.addActionListener(listener);
addOrTake.addActionListener(listener);
cata.addActionListener(listener);
showd.addActionListener(listener);
payd.addActionListener(listener);
listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {}};
// TODO Auto-generated method stub
menu.setSize(60, 600);
menu.setVisible(true);
JButton poo=new JButton("Poo");
poo.setSize(60,600);
card2.add((poo));
card2.add(new JButton("Pee"));
card2.add(new JButton("Per"));
card2.add(new JButton("POt"));
card2.setVisible(false);
menu.add(card2);
menu.add(card1);
catagories.pack();
menu.pack();
card1.setVisible(false);
}
public static void main(String[]args)
{
Proj poop =new Proj();
}
}
也許使用'GridBagLayout'代替 – MadProgrammer