gpt4 book ai didi

Java 可以使用 GridBagLayout 来组织 CardLayout 中使用的 JPanel

转载 作者:行者123 更新时间:2023-11-29 03:44:46 25 4
gpt4 key购买 nike

我是布局方面的新手,我现在使用的是 CardLayout,它有两张不同的卡片,目前绘制了背景图像和一个按钮。如下所示,该按钮位于屏幕顶部,我想将其放置在靠近底部的位置。我的理解是 GridBagLayout 将是实现该目标的最佳方式。所以我的第一个问题是,这是真的吗?而且,如果我想放置 GridBagLayout 的 JPanel 是 CardLayout 中的卡片,是否可以这样做。随着我进一步深入项目,我想将许多其他对象放入 GridBagLayout(如果这是最好的方法并且可能的话),因此非常感谢任何关于正确方向的建议。

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;

public class Cards implements ActionListener {
private JPanel cards;
private JButton button1;
private JButton button2;
private Image backgroundImage;

public void addComponentToPane(Container pane) throws IOException {
try {
backgroundImage = ImageIO.read(getClass().getResource("resources/background.jpg"));
} catch (IOException ex) {
ex.printStackTrace(System.err);
}

//create cards
JPanel card1 = new JPanel()
{
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(backgroundImage, 0, 0, null);
}

@Override
public Dimension getPreferredSize() {
return new Dimension(
backgroundImage.getWidth(null),
backgroundImage.getHeight(null));
}
};
JPanel card2 = new JPanel()
{
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(backgroundImage, 0, 0, null);
}

@Override
public Dimension getPreferredSize() {
return new Dimension(
backgroundImage.getWidth(null),
backgroundImage.getHeight(null));
}
};
//create buttons
button1 = new JButton("Button 1");
button2 = new JButton("Button 2");
button1.addActionListener(this);
button2.addActionListener(this);
//add buttons to cards
card1.add(button1);
card2.add(button2);
//create panel that contains cards
cards = new JPanel(new CardLayout());
cards.add(card1, "Card 1");
cards.add(card2, "Card 2");
pane.add(cards, BorderLayout.SOUTH);
}

public void itemStateChanged(ItemEvent evt) {
CardLayout cl = (CardLayout)(cards.getLayout());
cl.show(cards, (String)evt.getItem());
}

public static void createAndShowGUI() throws IOException {
//create and setup window
JFrame frame = new JFrame("Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//create and setup content pane
Cards main = new Cards();
main.addComponentToPane(frame.getContentPane());
//display window
frame.pack();
//frame.setResizable(false);
frame.setVisible(true);
}

public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == button1) {
CardLayout cl = (CardLayout) (cards.getLayout());
cl.show(cards, "Card 2");
} else if (ae.getSource() == button2) {
CardLayout cl = (CardLayout) (cards.getLayout());
cl.show(cards, "Card 1");
}
}

public static void main(String[] args) {
//set look and feel
try {
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}

//schedule job for the event dispatch thread creating and showing GUI
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
createAndShowGUI();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}

最佳答案

一个 GridBagLayout 会做你想做的,是的。它是可用的最灵活(但也是最复杂的)LayoutManager 之一。这是否是“最佳”方法取决于您要添加到面板的其他控件,是否要调整它们的大小,父 JPanel 时您希望发生什么调整大小和其他因素。

回复:嵌套具有不同布局的 JPanel,是的。在 JPanel 中使用 JPanelGridBagLayoutJPanel 中使用 CardLayout 非常好一个 BorderLayout [等等...]

关于Java 可以使用 GridBagLayout 来组织 CardLayout 中使用的 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11511864/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com