我正在尝试使用流布局来制作一个单个列的菜单屏幕,但每当我添加一个按钮时,它都会将它添加到一行。
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();
}
}
答案 0 :(得分:2)
问题:
答案 1 :(得分:1)
首先使用能够取得结果的布局管理器。
首先查看Laying Out Components Within a Container,How to Use GridLayout和How to Use GridBagLayout,了解一些想法。
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Proj {
JPanel card1, card2;
ActionListener listener;
JButton menu1, addOrTake, cata, payd, showd;
//JButton
JFrame menu;
public Proj() {
menu = new JFrame("Card Layout");
//final Container contentPane = menu.getContentPane();
final CardLayout layout = new CardLayout();
menu.setLayout(layout);
card1 = new JPanel(new GridBagLayout());
card2 = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
menu1 = new JButton("Mneu");
menu1.setVisible(true);
addOrTake = new JButton("Add or take");
cata = new JButton("Cata");
showd = new JButton("ShowD");
payd = new JButton("payd");
card1.add(menu1, gbc);
card1.add(addOrTake, gbc);
card1.add(cata, gbc);
card1.add(showd, gbc);
card1.add(payd, gbc);
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
JButton poo = new JButton("Poo");
card2.add((poo), gbc);
card2.add(new JButton("Pee"), gbc);
card2.add(new JButton("Per"), gbc);
card2.add(new JButton("POt"), gbc);
menu.add(card2, "Card2");
menu.add(card1, "Card1");
layout.show(menu.getContentPane(), "Card1");
menu.pack();
menu.setLocationRelativeTo(null);
menu.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
Proj poop = new Proj();
}
});
}
}
我还要仔细查看How to Use CardLayout,了解有关其工作原理的详细信息,因为您使用错了